개초보입니다.
밑에 코드 보면 빨강사각형, 노랑사각형 두개가 있는데
회전버튼을 누르면 90도 회전하게 만들려고 하거든요?
근데 밑에 있는 코드로 실행하면 전체 모양 유지를 못하고 사각형 두개가 따로따로 놀게되는데
어떻게 해야할까요?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ButtonEventTest extends JFrame implements ActionListener {
static int xx = 50;
static int yy = 80;
static int width = 80;
static int height = 30;
private JLabel result = new JLabel("");
public ButtonEventTest() {
JButton E = new JButton("오른쪽");
JButton W = new JButton("왼쪽");
JButton S = new JButton("회전");
Container ct = getContentPane();
MyDrawPanel drawPanel = new MyDrawPanel();
ct.add(BorderLayout.CENTER, drawPanel);
ct.add(BorderLayout.EAST, E);
ct.add(BorderLayout.WEST, W);
ct.add(BorderLayout.SOUTH, S);
E.addActionListener(this);
W.addActionListener(this);
S.addActionListener(this);
setTitle("ButtonEvent Test");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand()=="오른쪽")
if (xx < 90)
ButtonEventTest.xx = ButtonEventTest.xx + 10;
if(ae.getActionCommand()=="왼쪽")
if (xx > 0)
ButtonEventTest.xx = ButtonEventTest.xx - 10;
repaint();
if(ae.getActionCommand()=="회전"){
int temp = 0;
temp = ButtonEventTest.width;
ButtonEventTest.width = ButtonEventTest.height;
ButtonEventTest.height = temp;
}
repaint();
}
}
public class EventTest1 {
public static void main(String args[]) {
new ButtonEventTest();
}
}
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g){
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.red);
g.fillRect(ButtonEventTest.xx, ButtonEventTest.yy,
ButtonEventTest.width, ButtonEventTest.height);
g.setColor(Color.yellow);
g.fillRect(ButtonEventTest.xx+80, ButtonEventTest.yy+30,
ButtonEventTest.width, ButtonEventTest.height);
}
}
댓글 0