Code Comments
Programming Forum and web based access to our favorite programming groups.Hi there
I have a problem with the following program:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class SwingTest {
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame();
JComponent comp1 = new Filler(Color.RED);
JComponent comp2 = new Filler(Color.BLUE);
frame.setSize(100, 100);
frame.getContentPane().add(comp1);
frame.setVisible(true);
Thread.sleep(100);
frame.getContentPane().remove(0);
frame.getContentPane().add(comp2);
frame.repaint();
}
}
class Filler extends JComponent {
private Color c;
public Filler(Color c) {
this.c = c;
}
public void paint(Graphics g){
g.setColor(c);
g.fillRect(0, 0, 100, 100);
}
}
I would expect it to display a frame which is initially filled with red,
and changes to blue after some time elapsed. It does change to
background-color, though. What am i doing wrong?
--
Whom the gods wish to destroy they first call promising.
Post Follow-up to this messageIn article <opsgfz1jp5q1fd9p@localhost>,
"Stefan Schulz" <terra@spacetime.de> wrote:
> import java.awt.Color;
> import java.awt.Graphics;
>
> import javax.swing.JComponent;
> import javax.swing.JFrame;
>
> public class SwingTest {
>
> public static void main(String[] args) throws InterruptedException {
> JFrame frame = new JFrame();
> JComponent comp1 = new Filler(Color.RED);
> JComponent comp2 = new Filler(Color.BLUE);
>
> frame.setSize(100, 100);
> frame.getContentPane().add(comp1);
>
> frame.setVisible(true);
>
> Thread.sleep(100);
>
> frame.getContentPane().remove(0);
> frame.getContentPane().add(comp2);
>
> frame.repaint();
> }
>
> }
>
> class Filler extends JComponent {
>
> private Color c;
>
> public Filler(Color c) {
> this.c = c;
> }
>
> public void paint(Graphics g){
> g.setColor(c);
> g.fillRect(0, 0, 100, 100);
> }
> }
The blue rectangle is there, waiting to be drawn; it just needs a
reason: resizing the window, calling frame.setVisible(true); calling
validate(); etc.
See "Painting in AWT and Swing" by Amy Fowler in the Java API
documentation.
--
John
----
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.