Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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

Report this thread to moderator Post Follow-up to this message
Old Post
Stefan Schulz
10-26-04 01:57 AM


Re: Swing Problem
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/

Report this thread to moderator Post Follow-up to this message
Old Post
John B. Matthews
10-26-04 08:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:54 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.