I'am trying to do a simple code that will make my rectangles bigger. So, I decided to do one thread, that will update rectangle's widght, it looks like this:
class CoolThread extends Thread {
MyPanel MLP;
public CoolThread(MyPanel Panel1) {
MLP = Panel1;
}
@Override
public void run() {
super.run();
while (true) {
for (int i = 0; i < MLP.arrForRect.size() - 1; i++) {
MLP.arrForRect.get(i).animation();
MLP.repaint();
}}}}
In debugger it looks fine, width changing its numbers perfectly. So, where i did wrong, guys? Now my problem is the weird input lag when i click on the window. Edited for Ctrl+c, CTRL+v compiling.
public class PaintWindow {
void createGUI() {
JFrame f = new JFrame("My Canvas");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.setSize(800, 400);
f.setResizable(false);
f.setVisible(true);
f.setLocationRelativeTo(null);
}
}
class MyPanel extends JPanel {
public Point mousePos;
Timer animTimer;
ArrayList<ObjRectangle> arrForRect = new ArrayList<ObjRectangle>();
ObjRectangle ObjRect1;
public MyPanel() {
final ActionListener taskPerformer=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i=0;i<arrForRect.size()-1;i++){
arrForRect.get(i).animation();
repaint();
}
}
};
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
System.out.println(getMousePosition());
animTimer=new Timer(100,taskPerformer);
animTimer.start();
mousePos = getMousePosition();
ObjRect1 = new ObjRectangle();
arrForRect.add(ObjRect1);
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
arrForRect.get(arrForRect.size() - 1).drawObject(mousePos);
for (int i = 0; i < arrForRect.size() - 1; i++) {
arrForRect.get(i).paintSquare(g);
}
}
class ObjRectangle extends JPanel {
int x, y = 0;
int width = 50;
int height = 20;
public void drawObject(Point coordinates) {
this.x = coordinates.x;
this.y = coordinates.y;
}
public void animation() {
width++;
}
public void paintSquare(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(x, y, width, height);
}
}
public class MainClass {
PaintWindow window1= new PaintWindow();
window1.createGUI();
}
}
In debugger it looks fine, width changing its numbers perfectly. So, where i did wrong, guys?
all events in Swing GUI must be done on EDT, more read in Concurency in Swing
MLP.repaint();
could not notify EDT in Java7
use Swing Timer instead of plain Thread
override getPreferrredSize
for JPanel
in the class ObjRectangle extends JPanel implements GraphicObject {
for better help sooent post an SSCCE, short, runnable, compilable
EDIT
repaint();
in paintComponent()
, because, then paintComponent()
can be intialized again from itself, its can created endless, unmanagable loop, addept for Task Manager