Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-15-2007, 05:03 PM
Member
 
Join Date: Aug 2007
Posts: 3
rjevans2000 is on a distinguished road
Repaint fails when using threads
Hi, I'm trying to get to grips with threads and am creating a test application that carries out several tasks simultaneously. My problem is that when using a thread, the repaint() method doesn't seem to update the panel that I would expect it to. Below is the fragment of code in question:

class ColourPanel extends JPanel implements Runnable {

public void run() {
try {
for (int i=0;i<100000;i++) {

randomNumber = Math.random();
if ((randomNumber >= 0) && (randomNumber < 0.25))
setBackground(Color.RED);
if ((randomNumber >= 0.25) && (randomNumber < 0.5))
setBackground(Color.BLUE);
if ((randomNumber >= 0.5) && (randomNumber < 0.75))
setBackground(Color.MAGENTA);
if ((randomNumber >= 0.75) && (randomNumber <= 1))
setBackground(Color.ORANGE);

repaint();
setVisible(true);
Thread.sleep(200);
}
}
catch (InterruptedException e) {
}
}

public ColourPanel() {

setLayout(new BorderLayout());

colourButton = new JButton("Change Background");
add(colourButton, BorderLayout.SOUTH);

colourButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {

Runnable q = new ColourPanel();
Thread s = new Thread(q);
s.start();

}
});
}

private JButton colourButton;
private double randomNumber;

}

I would expect the repaint() method to update the panel with the new background colour. However, this does not work and I can't see why. I have tried calling getParent().repaint() and getRootPane().repaint() but these also don't work. Am I barking up the wrong tree? Any help would be greatly appreciated!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-15-2007, 05:28 PM
Member
 
Join Date: Aug 2007
Posts: 13
Bojevnik is on a distinguished road
instead of
Runnable q = new ColourPanel();
Thread s = new Thread(q);

try
Thread s = new Thread(this);

(im not 100% on this).
__________________
I HATE SMURFS!!!!!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-15-2007, 05:37 PM
Member
 
Join Date: Aug 2007
Posts: 3
rjevans2000 is on a distinguished road
Thread s = new Thread(this) fails to compile with the following error message:

symbol : constructor Thread(<anonymous java.awt.event.ActionListener>)
location: class java.lang.Thread
Thread s = new Thread(this);

Any ideas?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-15-2007, 06:42 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
Runnable q = new ColourPanelRx(); Thread s = new Thread(q); s.start();
This is okay to do but it creates and starts up a new instance, not this enclosing class, but another that doesn't do anything because it isn't shown in a gui. To do this with the enclosing class you can build some state (member variables: thread and animate) and some methods to make things happen (start and stop).
To check this idea out, ie, "a new instance not getting shown", you could see what happens with this:
Code:
Runnable q = new ColourPanelRx(); Thread s = new Thread(q); s.start(); // JPanel default size is 10,10. ((JComponent)q).setPreferredSize(new Dimension(100,100)); JOptionPane.showMessageDialog(null, q, "separate instance", -1);
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ColourPanelRx extends JPanel implements Runnable { Thread thread; boolean animate = false; public void run() { try { for (int i=0;i<100000;i++) { randomNumber = Math.random(); if ((randomNumber >= 0) && (randomNumber < 0.25)) setBackground(Color.RED); if ((randomNumber >= 0.25) && (randomNumber < 0.5)) setBackground(Color.BLUE); if ((randomNumber >= 0.5) && (randomNumber < 0.75)) setBackground(Color.MAGENTA); if ((randomNumber >= 0.75) && (randomNumber <= 1)) setBackground(Color.ORANGE); repaint(); // setVisible(true); Thread.sleep(1000); if(!animate) break; } } catch (InterruptedException e) { stop(); } } private void start() { if(!animate) { animate = true; thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } } private void stop() { animate = false; if(thread != null) thread.interrupt(); thread = null; } public ColourPanelRx() { setLayout(new BorderLayout()); colourButton = new JButton("Change Background"); add(colourButton, BorderLayout.SOUTH); colourButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.printf("this = %s%nthis.enclosingClass = %s%n", this.getClass().getName(), this.getClass().getEnclosingClass().getName()); // Runnable q = new ColourPanelRx(); // Thread s = new Thread(q); // s.start(); start(); } }); } private JButton colourButton; private double randomNumber; public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new ColourPanelRx()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in repaint Preethi AWT / Swing 16 03-18-2008 09:10 PM
Repaint problem swimberl Java 2D 1 02-16-2008 10:12 PM
Repaint problem swimberl Java 2D 0 01-06-2008 04:28 AM
repaint validate doLayout Gajesh Tripathi AWT / Swing 1 10-27-2007 07:53 PM
Repaint fails when using threads rjevans2000 Threads and Synchronization 1 09-22-2007 12:22 AM


All times are GMT +3. The time now is 01:05 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org