graphics2D gibt "NULL" immer in unterem Code zurück. Aus diesem Grund wird die Methode putPixel() nicht aufgerufen. Ich rufe PictureBox von Formularentwurf auf.Graphics2D liefert "NULL" immer
public class PictureBox extends JPanel {
Graphics2D graphics2D;
static BufferedImage image;
int imageSize = 300;
public PictureBox(){
setDoubleBuffered(false);
this.setBorder(UIManager.getBorder("ComboBox.border"));
this.repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(image == null){
image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_RGB);
graphics2D = (Graphics2D)image.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(image, 0, 0, this);
repaint();
}
public final void putPixel(int x, int y, Color color) {
if(graphics2D != null){
graphics2D.setColor(color);
graphics2D.drawLine(x, y, x, y);
repaint();
}
}
public void clear() {
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, imageSize,imageSize);
repaint();
}
}
putPixel Methode wird von der Haupt genannt, wo ich (x, y) in Point2D Array gespeichert koordinieren.
Warum nennen Sie 'repaint' in' paintComponent'? –
Wo sind 'image',' graphics2D' und 'imageSize' definiert? –
Sie haben repaint() in clear und paintComponent Methoden aufgerufen, das ist falsch. Als Repaint selbst wird paintComponent – Blip