Overlapping windows? in a Jframe?
Code :
public class Ch5SampleGraphics_UF {
public static void main(String[] args) {
JFrame win;
Container contentPane;
Graphics gra;
win = new JFrame("My First Rectangle");
win.setSize(300, 200);
win.setLocation(100, 100);
win.setVisible(true);
contentPane = win.getContentPane();
contentPane.setBackground(Color.ORANGE);
gra = contentPane.getGraphics();
gra.setColor(Color.blue);
gra.drawRect(50, 50, 100, 30);
}
}
in my screen the frame and the content pane are the only thing that appears...
but the CONTENTS (the geometric shape that is supposed to be in the content pane) doesnt appear.
how can i solved this one?
anyway , what i did (for me to see it somehow) is i'm re-executing it again and again... and in some of the execution of the program, sometimes the shapes appear.. sometimes it doesnt
Re: Overlapping windows? in a Jframe?
Your code now draws to the graphics object of the content pane, but this doesn't ensure that it will be drawn. The standard way to paint to a JComponent (in your case the conentPane) is to override its paintComponent method. In your case you can create a new JPanel, over-ride its paintComponent method, then add that to the content pane
Code :
JPanel panel = new JPanel(){
@Override public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawRect(50,50,100,30);
}
};
getConentPane().add(panel);
Re: Overlapping windows? in a Jframe?
wait a second, i dont know where should i put that code in my program...
can you please add it so i can examine it carefully...
Re: Overlapping windows? in a Jframe?
is it supposed to be like this?
Code :
public class Ch5SampleGraphics_UF {
public static void main(String[] args) {
JFrame win;
Container contentPane;
Graphics gra;
win = new JFrame("My First Rectangle");
win.setSize(300, 200);
win.setLocation(100, 100);
win.setVisible(true);
JPanel panel = new JPanel(){
@Override public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawRect(50,50,100,30);
}
};
panel.setBackground(Color.red);
contentPane = win.getContentPane();
contentPane.add(panel);
gra = contentPane.getGraphics();
gra.setColor(Color.blue);
gra.drawRect(50, 50, 100, 30);
}
}
??
Re: Overlapping windows? in a Jframe?
now the big question is??
why should i add a panel ... so the graphics can be drawn?
is the content pane not enough to make the drawing visible?
so when it comes in making a drawing in frame... i should declare a panel for it?
Re: Overlapping windows? in a Jframe?
Like this
Code :
public class Ch5SampleGraphics_UF {
public static void main(String[] args) {
JFrame win;
Container contentPane;
Graphics gra;
win = new JFrame("My First Rectangle");
win.setSize(300, 200);
win.setLocation(100, 100);
win.setVisible(true);//this line should be called AFTER things are added to the JFrame, otherwise you must valudate the JFrame, also remember its good practice to call pack()
JPanel panel = new JPanel(){
@Override public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawRect(50,50,100,30);
}
};
panel.setBackground(Color.red);
contentPane = win.getContentPane();
contentPane.add(panel);
/**These lines do nothing
gra = contentPane.getGraphics();
gra.setColor(Color.blue);
gra.drawRect(50, 50, 100, 30);
**/
}
}
Quote:
Originally Posted by
chronoz13
now the big question is??
why should i add a panel ... so the graphics can be drawn?
is the content pane not enough to make the drawing visible?
so when it comes in making a drawing in frame... i should declare a panel for it?
The answer is a bit complex and has to do with how things are drawn in Swing. JComponents are drawn using paintComponent, so whenever a window is resized, scrolled, or altered in a way to necessitate redrawing, the paintComponent method is called to redraw the window. Just getting a reference to the Graphics object associated with a JComponent and drawing to it doesn't ensure that it will be drawn (or in more probable terms, erased by the parent paintComponent method next time it is redrawn)
Re: Overlapping windows? in a Jframe?
ahhh so i was asking for a complex answer, hehhee, anyway.. you game me a little clarification of what i want...
tnx for that... sir!