Problems with drawing methods in "Graphics" class
Hi every one, I have a weird problem.
I'm trying this code for training :
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
class Proveextends JFrame{
private Color color;
public Prove()
{
super("Prove" );
}
publicvoid paint(Graphics g)
{
super.paint(g);
color =new Color(255,0,0);
g.setColor(color);
g.fillRect(100,100,20, 20);
}
publicstaticvoid main(String args[])
{
Prove app =new Prove();
app.setSize(640, 480);
app.setVisible(true);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
This works OK for GCJ/GIJ and Java 5 (draws a red square in a big windows) , but doesn't work in Java 6 (draws nothing, just an empty window)
What am I doing wrong?
Thank you :-)
Marcello
Message was edited by:
HayArms
[1763 byte] By [
HayArmsa] at [2007-11-26 22:48:36]

Try:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class Prove extends JFrame{
public static void main(String args[]){
Prove app = new Prove();
app.setContentPane( new JComponent(){
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D gfx = (Graphics2D)g;
Color color = new Color(255,0,0);
gfx.setColor(color);
gfx.fillRect(100,100,20, 20);
}});
app.setSize(640, 480);
app.setVisible(true);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Prove(){
super("Prove");
}
/*
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D gfx = (Graphics2D)g;
Color color = new Color(255,0,0);
gfx.setColor(color);
gfx.fillRect(100,100,20, 20);
}
*/
}
> > You should paint in the paintComponent method.
>
> He should really add a JPanel (or similar) and
> override the paintComponent() method of that.
You mean just like the code i posted ; )
Obviously he wouldnt seriously add it as an anon. inner class
like that though...