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);
}
});
}
}
Bookmarks