이것은 특정 x 및 y 좌표로 원 모양을 만드는 내 클래스입니다. 마찬가지로 다른 클래스가 있지만 x와 y 좌표 위치가 다릅니다. 각 클래스의 오브젝트를 1 개 작성해 JFrame상의 특정의 위치에 표시하고 싶습니다. JFrmae에 2 번째의 오브젝트를 추가하면 1 번째의 오브젝트를 덮어 씁니다. JFrmae의 다른 레이아웃으로 시도했지만 작동하지 않았습니다. 당신이 JFrame의 널 (null)의 레이아웃을 만들 경우특정 위치의 JFrame에 2 개의 객체를 추가합니다.
class Ballbewegung2 extends JPanel implements Runnable {
int x_pos = 10; int y_pos = 100; int radius = 20;
public void init() {
setBackground (Color.blue); }
public void start() {
Thread th = new Thread (this);
th.start(); }
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
x_pos ++; if(x_pos >= 400) x_pos = 10;
repaint();
try { Thread.sleep (20); }
catch (InterruptedException ex) {} thread.currentThread().setPriority(Thread.MAX_
PRIORITY); } } public void paint (Graphics g) { g.setColor (Color.red); g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); } }
그래서 별도의 스레드에서 다시 칠할 수 있습니까? 다른 서클 클래스가 어떻게 보이나요? 나는 조금 혼란 스럽다. – Austin
다른 클래스는 정확하게 동일하지만 y 좌표의 다른 위치를가집니다. 다른 클래스에서는 y_pos의 값만 변경하고 나머지는 동일합니다. – user1717353
"Swing 프로그램은'paint()'를 오버라이드하는 대신'paintComponent()'를 덮어 써야합니다."- [AWT와 Swing의 페인팅 : 페인트 메소드 *] (http://www.oracle.com/technetwork/java/) painting-140037.html # callbacks) – trashgod