Java swing help with repaint()
Hello,
I want to know why the repaint method is not working properly for mouse event? I have a program to draw lines on every new line draw I want the screen to erase the previous line & draw a new line so I have repaint method on every mouse move. I don't understand why the previous line is not being cleared?
Can anyone help me?
Re: Java swing help with repaint()
I know what you need to do.
Call the package. At the start of the paint method you need to call the super.paintComponent. Now you will be able to clear the screen before drawing new line. If this doesn't work let me know.
I hope this helps.
Re: Java swing help with repaint()
Hi,
I have a similar problem in Java Swing with repaint method. I have a Label & added it to a panel which is added to a frame. Now the problem here is when I call repaint method on panel or frame my GUI is not getting updated. But resizing the window updates it. Do anyone know about this behavior?
I want to know why repaint is not updating th4e GUI? Am I calling the method at wrong place?
Thanks in advance.
Re: Java swing help with repaint()
Swing and Thread: repaint
Here is an example
Code:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class DigitalTimer extends JLabel {
private volatile String timeText;
private Thread internalThread;
private volatile boolean noStopRequested;
public DigitalTimer() {
setBorder(BorderFactory.createLineBorder(Color.black));
setHorizontalAlignment(SwingConstants.RIGHT);
setFont(new Font("SansSerif", Font.BOLD, 16));
setText("00000.0"); // use to size component
setMinimumSize(getPreferredSize());
setPreferredSize(getPreferredSize());
setSize(getPreferredSize());
timeText = "0.0";
setText(timeText);
noStopRequested = true;
Runnable r = new Runnable() {
public void run() {
try {
runWork();
} catch (Exception x) {
x.printStackTrace();
}
}
};
internalThread = new Thread(r, "DigitalTimer");
internalThread.start();
}
private void runWork() {
long startTime = System.currentTimeMillis();
int tenths = 0;
long normalSleepTime = 100;
long nextSleepTime = 100;
DecimalFormat fmt = new DecimalFormat("0.0");
Runnable updateText = new Runnable() {
public void run() {
setText(timeText);
}
};
while (noStopRequested) {
try {
Thread.sleep(nextSleepTime);
tenths++;
long currTime = System.currentTimeMillis();
long elapsedTime = currTime - startTime;
nextSleepTime = normalSleepTime
+ ((tenths * 100) - elapsedTime);
if (nextSleepTime < 0) {
nextSleepTime = 0;
}
timeText = fmt.format(elapsedTime / 1000.0);
SwingUtilities.invokeAndWait(updateText);
} catch (InterruptedException ix) {
// stop running
return;
} catch (InvocationTargetException x) {
x.printStackTrace();
}
}
}
public void stopRequest() {
noStopRequested = false;
internalThread.interrupt();
}
public boolean isAlive() {
return internalThread.isAlive();
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(new DigitalTimer());
f.setSize(250, 100);
f.setVisible(true);
}
}
Re: Java swing help with repaint()
I think you maybe experiencing a similar problem to me. I've been having a ball with this one for a while :)
Here is a cut down of my code, I have to call repaint() inside the paintComponent() method. Which shouldn't be the case, otherwise my graphics only show when I rapidly resize the window...its as if they are being refreshed over immediately. Anyone think this is a threading issue?
Code:
import java.awt.*;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class Artything extends JPanel {
private static final long serialVersionUID = 7747161700374293379L;
public Artything(Graphics g)
{
g2d = (Graphics2D) g;
}
Graphics2D g2d;
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i=10; i<300; i++){
g2d.fillRect(10+i, 10+i, 100, 150);
repaint(); //Without this repaint() Nothing shows,
//except when I rapidly resize the window,
//its slows performance enough
//that I can see the graphics being written.
//Then they are immediately overwritten.
//Its a CPU hog, and in a loop though thats the problem :)
}
}
}
//Here is the class I'm trying to use it inside...or a small part of it
public class NewJFrame extends javax.swing.JFrame {
....
public JPanel labelLayoutJPanel;
....
pack();
ArtyThing renderer = new ArtyThing(labelLayoutJPanel.getGraphics());
labelLayoutJPanel.add(renderer);
....
}
//Any help appreciated. Thanks
//PS Its all running from here:
import java.util.Vector;
import javax.swing.SwingUtilities;
public class RunClient {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DatabaseManager dbm = new DatabaseManager();
Vector<String> list = dbm.returnList();
NewJFrame inst = new NewJFrame();
inst.initGUI(list);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
}