Hi,
I just found this site the other day, and it looks like a great resource! Anyway, I am creating a (simple)
Java drawing program and have as my top-level window a JFrame. Inside the JFrame is a JPanel, upon which the drawings (lines, circles, rectangles) will appear. However, when I go to draw items on my JPanel, it looks like they are appearing on my JFrame (underneath the JPanel), and I can't figure out how to get the drawings to appear in the JPanel. I've posted code below, in case that will help you understand what it is I am trying to do.... Thanks in advance for any help/suggestions you may have...
--- code ---
/* Global Variables */
JFrame F;
JPanel drawingArea;
(later in code)
/* parts of the createWindow method
F = new JFrame();
F.setTitle(title);
F.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Fwidth = Fheight = 500;
F.setBounds(100,200,Fwidth,Fheight); //set the position and size of the frame
menuBar = new JMenuBar();// Create a menu bar object
F.setContentPane(this);
F.setJMenuBar(menuBar); // Add the menu bar to the frame
(later in code .. but still in the createWindow method)
drawingArea = new JPanel();
drawingArea.addMouseListener(this);
drawingArea.addMouseMotionListener(this);
drawingArea.setPreferredSize(new Dimension (Fwidth-20, Fheight-80));
drawingArea.setBackground(Color.white);
drawingArea.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
drawingArea.getBorder()));
F.getContentPane().add(drawingArea, BorderLayout.CENTER);
F.setVisible(true);
/* end of createWindow method */
public void drawingAreaChanged()
{
/* this method gets called whenever the user selects the option to start
* drawing
*/
System.out.println("Drawing Area Changed!");
drawingArea.repaint();
}
public void paintComponent (Graphics g)
{
String type;
int x1, y1, x2, y2;
int count;
Integer tempi;
Vector imageData = myModel.getImageData();
super.paintComponent(g); // repaints the background of JFrame
g.setColor(Color.black);
// type is set in another part of the code
// All of these draw commands appear in the JFrame rather than the
// JPanel
if (type.compareTo("line") == 0)
{
g.drawLine(x1,y1,x2,y2);
}
if (type.compareTo("circle") == 0)
{
g.drawOval(x1,y1,x2,y2);
}
if (type.compareTo("rectangle") == 0)
{
g.drawRect(x1,y1,x2,y2);
}
} // paint method