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]
# 1
> ...> > 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)I see a red square with JDK 6.
prometheuzza at 2007-7-10 12:08:30 > top of Java-index,Java Essentials,Java Programming...
# 2
You should paint in the paintComponent method.
TuringPesta at 2007-7-10 12:08:30 > top of Java-index,Java Essentials,Java Programming...
# 3
It works for me in J6.
TuringPesta at 2007-7-10 12:08:30 > top of Java-index,Java Essentials,Java Programming...
# 4
Dunno what to say ... I have a Debian Box and in J6 it just show a blank window ... :( J5 and GNU tools show it correctly.Hints ?:(
HayArmsa at 2007-7-10 12:08:30 > top of Java-index,Java Essentials,Java Programming...
# 5

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);

}

*/

}

TuringPesta at 2007-7-10 12:08:30 > top of Java-index,Java Essentials,Java Programming...
# 6
> You should paint in the paintComponent method.He should really add a JPanel (or similar) and override the paintComponent() method of that.
CaptainMorgan08a at 2007-7-10 12:08:30 > top of Java-index,Java Essentials,Java Programming...
# 7

> > 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...

TuringPesta at 2007-7-10 12:08:30 > top of Java-index,Java Essentials,Java Programming...