Results 1 to 2 of 2
Thread: Repaint fails when using threads
- 08-15-2007, 04:02 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 3
- Rep Power
- 0
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!
- 09-21-2007, 11:22 PM #2
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ColourPanel extends JPanel implements Runnable { public void run() { try { for (int i=0; i<10; 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(); System.out.printf("i = %s isDisplayable = %b%n", i, isDisplayable()); Thread.sleep(200); } } catch (InterruptedException e) { return; } } public ColourPanel() { setLayout(new BorderLayout()); colourButton = new JButton("Change Background"); add(colourButton, BorderLayout.SOUTH); colourButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // This first line creates a new instance of ColourPanel // which is not the current instance that has been // instantiated and loaded into a gui. So this new // instance runs just fine but cannot be seen. //Runnable q = new ColourPanel(); // If you get a reference to the current instance // that has been instantiated and added to a gui // you will see the colors change. Since we are inside // another class ("this" == this ActionListener anonymous // class) we use the static form of obtaining a reference. Runnable q = ColourPanel.this; Thread s = new Thread(q); s.start(); } }); } public static void main(String[] args) { ColourPanel panel = new ColourPanel(); panel.setPreferredSize(new Dimension(200,200)); JOptionPane.showMessageDialog(null, panel, "", -1); } private JButton colourButton; private double randomNumber; }
Similar Threads
-
Problem in repaint
By Preethi in forum AWT / SwingReplies: 16Last Post: 03-18-2008, 08:10 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 1Last Post: 02-16-2008, 09:12 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 0Last Post: 01-06-2008, 03:28 AM -
repaint validate doLayout
By Gajesh Tripathi in forum AWT / SwingReplies: 1Last Post: 10-27-2007, 06:53 PM -
Repaint fails when using threads
By rjevans2000 in forum AWT / SwingReplies: 3Last Post: 08-15-2007, 05:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks