2016-03-29 17 views
1

Mein Ziel ist es, einen einfachen Wrapper zum Malen beliebiger Pixel in einem Fenster zu erstellen. Ich bekomme immer den FehlerMalpixel in Jython

Leider:

Traceback (most recent call last): 
    File "test.py", line 11, in <module> 
    canvas = window('Test', WINDOW_WIDTH, WINDOW_HEIGHT) 
    File "..\painter.py", line 16, in __init__ 
    my_jframe.getContentPane().add(my_image) 
TypeError: add(): 1st arg can't be coerced to java.awt.Component, java.awt.PopupMenu 

Was ist das Problem ???

Vielen Dank für Ihre Vorschläge!

Lucas.

Als Anrufer verwende ich die folgende:

# (test.py) 
#! /usr/bin/env jython 

import time 
import random 

from painter import window 

WINDOW_WIDTH = 800 
WINDOW_HEIGHT = 600 

canvas = window('Test', WINDOW_WIDTH, WINDOW_HEIGHT) 
print('fuck1') 

for x in range(WINDOW_WIDTH): 
    for y in range (WINDOW_HEIGHT): 
     rgba = [ random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255] 
     print(x, y, rgba) 
     canvas.paint_pixel(x, y, rgba) 
     time.sleep(1) 

Der gerufene Modul sieht wie folgt aus:

# (painter.py) 
#! /usr/bin/env jython 

import javax.swing 
import java.awt 

class window(object): 

    def __init__(self, window_title, window_width, window_height): 
     my_jframe = javax.swing.JFrame(window_title) 

     my_image = java.awt.image.BufferedImage( 
      window_width, 
      window_height, 
      java.awt.image.BufferedImage.TYPE_INT_ARGB 
      ) 
     my_jframe.getContentPane().add(my_image) 

     my_jframe.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE) 
     my_jframe.setSize(window_width, window_height) 
     my_jframe.setLocationRelativeTo(None)        # center window 
     my_jframe.show() 

    def paint_pixel(self, x, y, rgba): 
     c = java.awt.Color(rgba[0], rgba[1], rgba[2], rgba[3]) 
     self.my_image.setRGB(x, y, c) 

Antwort

0

Ich denke, Schaukel erlaubt keine BufferedImage on the fly ändern, aber man könnte etwas verwenden so:

Geändert von test.py:

# (test2.py) 
#! /usr/bin/env jython 

import time 
import random 

from painter2 import window 
from java.awt import Color 
from java.util import Random 

WINDOW_WIDTH = 800 
WINDOW_HEIGHT = 600 

canvas = window('Test', WINDOW_WIDTH, WINDOW_HEIGHT) 

randomNumbers = Random(123456) 

for x in range(WINDOW_WIDTH): 
    for y in range (WINDOW_HEIGHT): 
     color = Color(randomNumbers.nextInt(2 ** 24)) 
     #print(x, y, color) 
     canvas.add_pixel(x, y, color) 
     #time.sleep(1) 

canvas.repaint() 

Modifizierte Version Ihrer painter.py:

# (painter2.py) 
#! /usr/bin/env jython 

import javax.swing 
import java.awt 

from colored_pixel import colored_pixel 

class window(javax.swing.JPanel): 

    def __init__(self, window_title, window_width, window_height): 
     my_jframe = javax.swing.JFrame(window_title) 

     my_jframe.getContentPane().add(self, java.awt.BorderLayout.CENTER) 

     self.pixels = [] 

     my_jframe.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE) 
     my_jframe.setSize(window_width, window_height) 
     my_jframe.setLocationRelativeTo(None) # center window 
     my_jframe.setVisible(True) 

     print self.getWidth() 
     print self.getHeight() 

    def add_pixel(self, x, y, color): 
     self.pixels.append(colored_pixel(x, y, color)) 

    def paintComponent(self, graphics): 
     for pixel in self.pixels: 
      graphics.setColor(pixel.color) 
      graphics.drawRect(pixel.x, pixel.y, 1, 1) 

Kleine Klasse zum Speichern von Pixelposition und Farbe:

class colored_pixel(object): 
    def __init__(self, x, y, color): 
     self.x = x 
     self.y = y 
     self.color = color