Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- c#
- 파이썬
- 자바스크립트
- 스프링부트
- class
- join()
- 객체
- AssertJ
- 배열
- JavaScript
- 프로그래머스스쿨
- docker
- 자바
- Dict
- Swing
- array
- event
- Python
- Java
- GIT
- Docker Desktop
- JS
- 메소드
- SpringBoot
- synchronized
- 클래스
- StringBuilder
- thread
- SSL
- 저장소
Archives
- Today
- Total
정리노트
[자바/java] Thread, Graphics 도형을 그리고 thread로 크기변화 본문
package q4;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class Q4 extends JFrame {
int x, y, width, height;
Q4() {
setSize(500, 300);
x = 50; y = 50; width = 400; height = 200;
MyPanel p = new MyPanel();
add(p);
Thread t = new Thread(p);
t.start();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class MyPanel extends JPanel implements Runnable{
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.PINK);
g.fillRect(x, y, width, height);
}
@Override
public void run() {
while (true) {
if (x < 300) {
while (x < 300) {
repaint();
try {
Thread.sleep(100);
x += 10; width -= 10;
y += 5; height -= 5;
} catch (Exception e) {
e.printStackTrace();
}
}
}
else {
while (x > 50) {
repaint();
try {
Thread.sleep(100);
x -= 10; width += 10;
y -= 5; height += 5;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
public static void main(String[] args) {
Q4 f = new Q4();
}
}
728x90
'프로그래밍 > Java' 카테고리의 다른 글
[자바/java] Thread 활용 두 개 이미지 이동 컨트롤 - 2 / join() (1) | 2023.02.05 |
---|---|
[자바/java] 하나의 list 두 개 스레드 동작 합 구하기 (0) | 2023.02.05 |
[자바/java] Thread 활용 이미지 이동 컨트롤 (2) | 2023.02.04 |
[자바/java] thread 디지털 시계 (0) | 2023.02.04 |
[자바/java] thread 간 동작조정(생산자, 소비자) (0) | 2023.02.04 |