paint 그림
코드
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyPanel extends JPanel{
JButton[] bt = new JButton[8];
int ximg = 250;
int yimg = 150;
int rsize = 20;
int ssize = 20;
int xposition = 30;
int yposition = 350;
int i;
public MyPanel(){
// 버튼 생성
for(i =0; i<bt.length;i++){
bt[i] = new JButton();
bt[i].setBounds(xposition,yposition,80,20);
xposition += 100;
if(i == 3) {
xposition = 30;
yposition += 50;
}
add(bt[i]);
}
setLayout(null);
// 버튼 이름
bt[0].setText("왼");
bt[1].setText("오른");
bt[2].setText("위");
bt[3].setText("아래");
bt[4].setText("x확대");
bt[5].setText("x축소");
bt[6].setText("y확대");
bt[7].setText("y축소");
// 버튼 기능
bt[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ximg = ximg - 10;
repaint(); // paintcompornent 호출해주는 메소드
}
});
bt[1].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ximg = ximg + 10;
repaint(); // paintcompornent 호출해주는 메소드
}
});
bt[2].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
yimg = yimg - 10;
repaint(); // paintcompornent 호출해주는 메소드
}
});
bt[3].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
yimg = yimg + 10;
repaint(); // paintcompornent 호출해주는 메소드
}
});
bt[4].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ximg = ximg - 10;
rsize = rsize + 20;
repaint(); // paintcompornent 호출해주는 메소드
}
});
bt[5].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ximg = ximg + 10;
rsize = rsize - 20;
repaint(); // paintcompornent 호출해주는 메소드
}
});
bt[6].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
yimg = yimg - 10;
ssize = ssize + 20;
repaint(); // paintcompornent 호출해주는 메소드
}
});
bt[7].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
yimg = yimg + 10;
ssize = ssize - 20;
repaint(); // paintcompornent 호출해주는 메소드 - 그림을 새로 그림
}
});
}
// paintcompornent
// 자동호출
public void paintComponent(Graphics g){
// 부모들 가리키는 키워드는 super
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(ximg,yimg,rsize,ssize); // 네모
//g.fillOval(ximg,yimg,rsize,ssize); // 원
}
}
class J1220_Paint extends JFrame{
public J1220_Paint(){
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("패널에 그림그리기");
MyPanel p1 = new MyPanel();
add(p1);
setVisible(true);
}
public static void main(String args[]){
new J1220_Paint();
}
}
| cs |
댓글
댓글 쓰기