Swing problem, newbie'ish . Perhaps thread related?
Hi- this is working (kind of :) ) , but its instantly refreshing over the boxes Im trying to draw. I can only see the boxes briefly when resizing the window. So its like they are drawn and then instantly drawn over. I've been pulling out small chunks of hair all morning over it...!!! Please someone save me !
Heres some code, its part of a big program I'm writing, so I've tried to cut out lots of the irrelevant stuff.
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);
}
}
}
Here is the class I'm trying to use it inside...or a small part of it
Code :
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:
Code :
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);
}
});
}
}
Tell me its not a threading problem please :) Anyone who helps is welcome to free guitar advice =P
Re: Swing problem, newbie'ish . Perhaps thread related?
Sorry I spotted a mistake in the original post
Hi- this is working (kind of ) , but its instantly refreshing over the boxes Im trying to draw. I can only see the boxes briefly when resizing the window. So its like they are drawn and then instantly drawn over. I've been pulling out small chunks of hair all morning over it...!!! Please someone save me !
Heres some code, its part of a big program I'm writing, so I've tried to cut out lots of the irrelevant stuff.
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);
}
}
}
Here is the class I'm trying to use it inside...or a small part of it
Code :
public class NewJFrame extends javax.swing.JFrame {
....
public JPanel labelLayoutJPanel;
....
pack();
ArtyThing renderer = new ArtyThing(this.labelLayoutJPanel.getGraphics());
labelLayoutJPanel.add(renderer);
....
}
Any help appreciated. Thanks
PS Its all running from here:
Code :
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);
}
});
}
}
Tell me its not a threading problem please Anyone who helps is welcome to free guitar advice =P
Re: Swing problem, newbie'ish . Perhaps thread related?
Ok - repaint(); Inserted into the paint method seems to have fixed it. No idea why. But thats hacking for you :)
SOLVED.
Re: Swing problem, newbie'ish . Perhaps thread related?
Quote:
Originally Posted by
fenderman
Ok - repaint(); Inserted into the paint method seems to have fixed it. No idea why. But thats hacking for you :)
SOLVED.
well that was easy ;) I'm glad you solved it fenderman.
If you ever need to edit your original post, click the EDIT button and you can make whatever changes you wish.
I have marked your thread as SOLVED. Read the tutorial in my signature to see how to do it yourself.