Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Java swing help with repaint()

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 22-07-2009
Member
 
Join Date: May 2008
Posts: 24
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?

Reply With Quote
  #2  
Old 22-07-2009
Member
 
Join Date: Apr 2008
Posts: 37
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.
Reply With Quote
  #3  
Old 22-07-2009
Member
 
Join Date: May 2008
Posts: 29
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.
Reply With Quote
  #4  
Old 22-07-2009
Member
 
Join Date: May 2008
Posts: 30
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);
  }
}
Reply With Quote
  #5  
Old 23-07-2009
Member
 
Join Date: Jul 2009
Posts: 1
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);
}
});
}

}

Last edited by Fenderman : 23-07-2009 at 04:44 PM.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java swing help with repaint()"
Thread Thread Starter Forum Replies Last Post
Applets V/s Swing in JAVA "Dritan" Software Development 3 14-12-2010 09:04 AM
Java Swing and MySQL shelton141 Software Development 1 22-09-2009 09:50 AM
drawPolygon in java Swing? JagdishP Software Development 3 08-08-2009 12:33 AM
Message Box example for Java Swing Visala28 Software Development 3 31-07-2009 06:30 PM
Help: Java swing JPanel to repaint PaintComponent( Graphics g ) grudge Software Development 4 22-07-2009 08:05 PM


All times are GMT +5.5. The time now is 06:58 PM.