Hello. Help me figure out how to add an additional animated figure to the panel. Adding an additional drawing method (.drawRect ()) is not a good option, I want the shapes to be implemented through a separate class with their own independent fields. Code:
import javax.swing.*; import java.awt.*; public class Test extends JFrame { Test (String s) { super(s); MyComponent panel = new MyComponent(); panel.setPreferredSize(new Dimension(300, 300)); add(panel); pack(); setVisible(true); this.setLocationRelativeTo(null); } public static void main(String[] args) { Runnable doHelloWorld = new Runnable() { public void run() { JFrame a = new Test("test"); a.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }; SwingUtilities.invokeLater(doHelloWorld); } public class MyComponent extends JComponent implements Runnable{ private long t = System.nanoTime(); public int exct = 0; public boolean updown = false; public MyComponent(){ super(); new Thread(this).start(); } @Override public void run() { while (true) { repaint(); try { Thread.sleep(5); } catch (InterruptedException ex) {} } } @Override protected void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.draw3DRect(120, exct, 40, 40, true); if(exct >= 260) { System.out.println("Yoooooha"); updown = false; } else if (exct <= 0) { updown = true; } if(updown == true) { exct += 1; } else { exct -= 1; } System.out.println(exct + "|" + updown); } } }