1
도형 그리기 및 색상 변경 프로그램을 만들었습니다. 재설정 버튼을 기본 모양과 색상으로 재설정했지만 캔버스에서 모양을 지울 수 없습니다. 어떻게해야합니까?eclipse sdk를 사용하여
// Color Clear choice box
Choice colorChoice;
// the canvas
DrawCanvas canvas;
/**
* Constructor
*/
public Draw() {
super("Java Draw");
setLayout(new BorderLayout());
// create panel for controls
Panel topPanel = new Panel(new GridLayout(3, 0));
add(topPanel, BorderLayout.NORTH);
// create button control
Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(buttonPanel);
circle = new Button("Circle");
buttonPanel.add(circle);
roundRec = new Button("Rounded Rectangle");
buttonPanel.add(roundRec);
threeDRec = new Button("3D Rectangle");
buttonPanel.add(threeDRec);
// add button listener
circle.addActionListener(this);
roundRec.addActionListener(this);
threeDRec.addActionListener(this);
Panel buttonPanel1 = new Panel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(buttonPanel1);
lines = new Button("Lines");
buttonPanel1.add(lines);
squares = new Button("Square");
buttonPanel1.add(squares);
ovals = new Button("Ovals");
buttonPanel1.add(ovals);
lines.addActionListener(this);
squares.addActionListener(this);
ovals.addActionListener(this);
// create panel for color choices
Panel colorPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(colorPanel);
Label label = new Label("Filled Color:");
colorPanel.add(label);
colorChoice = new Choice();
for(int i=0; i<COLOR_NAMES.length; i++) {
colorChoice.add(COLOR_NAMES[i]);
}
colorPanel.add(colorChoice);
colorChoice.addItemListener(this);
// create reset button
Button resetButton = new Button("Reset");
colorPanel.add(resetButton);
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
colorChoice.select(0); // reset color choice box
canvas.setFilledColor(COLORS[0]); // reset color used
canvas.setShape(DrawCanvas.CIRCLE); // reset shape
}
});
// create the canvas
canvas = new DrawCanvas();
add(canvas, BorderLayout.CENTER);
}// end of constructor
/**
* Implementing ActionListener
*/
public void actionPerformed(ActionEvent event) {
if(event.getSource() == circle) { // circle button
canvas.setShape(DrawCanvas.CIRCLE);
}
else if(event.getSource() == roundRec) { // rounded rectangle button
canvas.setShape(DrawCanvas.ROUNDED_RECTANGLE);
}
else if(event.getSource() == threeDRec) { // 3D rectangle button
canvas.setShape(DrawCanvas.RECTANGLE_3D);
}
}
/**
* Implementing ItemListener
*/
public void itemStateChanged(ItemEvent event) {
Color color = COLORS[colorChoice.getSelectedIndex()];
canvas.setFilledColor(color);
}
/**
* the main method
*/
public static void main(String[] argv) {
// Create a frame
Draw frame = new Draw();
frame.setSize(WIDTH, HEIGHT);
frame.setLocation(150, 100);
// add window closing listener
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
// Show the frame
frame.setVisible(true);
}
}
DrawCanvas 란 무엇입니까? 전체 코드를 붙여 주실 수 있습니까? – Drona
http://www.daniweb.com/software-development/java/threads/417811/1782034와 관련있는 것으로 보입니다. – mzjn