Results 1 to 5 of 5

Thread: Java swing help with repaint()

  1. #1
    Join Date
    May 2008

    Java swing help with repaint()


    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?

  2. #2
    Join Date
    Apr 2008

    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.

  3. #3
    Join Date
    May 2008

    Re: Java swing help with repaint()


    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.

  4. #4
    Join Date
    May 2008

    Re: Java swing help with repaint()

    Swing and Thread: repaint
    Here is an example

    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() {
        setFont(new Font("SansSerif", Font.BOLD, 16));
        setText("00000.0"); // use to size component
        timeText = "0.0";
        noStopRequested = true;
        Runnable r = new Runnable() {
          public void run() {
            try {
            } catch (Exception x) {
        internalThread = new Thread(r, "DigitalTimer");
      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() {
        while (noStopRequested) {
          try {
            long currTime = System.currentTimeMillis();
            long elapsedTime = currTime - startTime;
            nextSleepTime = normalSleepTime
                + ((tenths * 100) - elapsedTime);
            if (nextSleepTime < 0) {
              nextSleepTime = 0;
            timeText = fmt.format(elapsedTime / 1000.0);
          } catch (InterruptedException ix) {
            // stop running
          } catch (InvocationTargetException x) {
      public void stopRequest() {
        noStopRequested = false;
      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);

  5. #5
    Join Date
    Jul 2009

    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?

    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) {
    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;
    ArtyThing renderer = new ArtyThing(labelLayoutJPanel.getGraphics());
    //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();
    Last edited by Fenderman; 23-07-2009 at 03:44 PM.

Similar Threads

  1. Applets V/s Swing in JAVA
    By "Dritan" in forum Software Development
    Replies: 3
    Last Post: 14-12-2010, 08:04 AM
  2. Java Swing and MySQL
    By shelton141 in forum Software Development
    Replies: 1
    Last Post: 22-09-2009, 08:50 AM
  3. drawPolygon in java Swing?
    By JagdishP in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 11:33 PM
  4. Message Box example for Java Swing
    By Visala28 in forum Software Development
    Replies: 3
    Last Post: 31-07-2009, 05:30 PM
  5. Help: Java swing JPanel to repaint PaintComponent( Graphics g )
    By grudge in forum Software Development
    Replies: 4
    Last Post: 22-07-2009, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts