JPanel Graphics
Hallo!
I have for example a simple Java program that creates JFrame and sets JPanel there for drawing purposes.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class test {
public test() {
}
public static void main(String[] args) {
String title = "Frame Title";
JFrame f=new JFrame(title);
JPanel p = new JPanel();
f.setSize(300,200);
f.addWindowListener(
new WindowListener() {
public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
);
f.getContentPane().add(p);
f.setVisible(true);
Graphics g=p.getGraphics();
//p.paint(g);
// g.setColor(Color.black);
// g.fillRect(30, 20, 280, 180);
}
}
if to comment line Graphics g=p.getGraphics(); its all works, but i wanted to draw something to JPanel, and without Graphical content, as i understood, it's not possible ( g.fillRect(30, 20, 280, 180); ).
if to stay program with Graphics g=p.getGraphics();, Error cannot access graphics is produced.
I i'm really dont wan't to create some class, that extends JPanel, because of then it is necesaary to call all drawings from than class, but i'm wanted
to paint to JPanel from others classes, for example to draw one figure at one moment, and other figure at another.
At real program i organized class, and variable JPanel is readed as an object of that class instance, in other words it's not a problem to get JPanel to classes where i want to graw, but
i recieve the same error - can't get Graphical content of JPanel.
Why graphical content can't be accessed?
Method getGraphics() is a method of JPanel.
Thanks,
Urrimus.

