For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2004 > Swing Problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Swing Problem
Stefan Schulz

2004-10-25, 8:57 pm

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.
John B. Matthews

2004-10-26, 3:57 am

In 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/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com