I'm studying Canvas objects. There was a problem with the paintboard, so I made it once.
But no matter how hard I try, both erasing and erasing are not possible, and they are painted in the color that I originally drew.
in actionPerformed
if(obj==erase){
cd.color = cd.getBackground();
}else if(obj==eraseAll){
cd.sw=1;
cd.repaint();
This seems to be a problem, so you should not try to create and modify the Color object. I don't think there's a grammatical problem because there's no error or warning window I couldn't erase it, so I'm asking you this question.
Thank you. Have a good day~
package Graphics;
import java.awt.*;
import java.awt.event.*;
public class paintSampleEx extends Frame {
Panel pLeft, p1, p2, pRight;
Button blue, orange, green, plus, minus, erase, eraseAll;
CanvasDemo cd;
public paintSampleEx() {
super("paint Application Test");
pLeft = new Panel(new BorderLayout());
pLeft.setBackground(Color.lightGray);
add(pLeft,"West");
p1 = new Panel(new GridLayout(3,1)){
public Insets getInsets(){
return new Insets (10, 10, 10, 10); // margin
}
};
pLeft.add(p1,"North");
// Create p1 button
blue = new Button();
blue.setBackground(Color.blue);
orange = new Button();
orange.setBackground(Color.orange);
green = new Button();
green.setBackground(Color.green);
p1.add(blue); p1.add(orange); p1.add(green);
// Create p2 button
plus = new Button("+");
minus = new Button("-");
erase = new Button ("erase");
eraseAll = new Button ("Clear All");
p2 = new Panel(new GridLayout(4,1)){
public Insets getInsets(){
return new Insets(20, 10, 220, 10);
}
};
p2.add(plus); p2.add(minus); p2.add(erase); p2.add(eraseAll);
pLeft.add(p2, "Center");
pRight = new Panel(){
public Insets getInsets(){
return new Insets(70, 20, 20, 20);
}
};
pRight.setBackground(Color.gray);
add(pRight, "Center");
cd = new CanvasDemo();
cd.setSize(300, 300);
pRight.add(cd);
cd.setBackground(Color.WHITE);
blue.addActionListener(new EventHandler());
orange.addActionListener(new EventHandler());
green.addActionListener(new EventHandler());
plus.addActionListener(new EventHandler());
green.addActionListener(new EventHandler());
minus.addActionListener(new EventHandler());
erase.addMouseMotionListener(new EventHandler());
eraseAll.addMouseMotionListener(new EventHandler());
cd.addMouseMotionListener(new EventHandler());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
} // end of paintSampleEx() constructor
class EventHandler extends MouseMotionAdapter implements ActionListener{
public void mouseDragged(MouseEvent me){
cd.x = me.getX();
cd.y = me.getY();
cd.repaint();
}
public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
Color btnCol= ((Button)obj).getBackground();
//System.out.println(btnCol);
cd.sw= 0;
if(btnCol==Color.blue){
cd.color = btnCol;
}else if(btnCol==Color.orange){
cd.color = btnCol;
}else if(btnCol==Color.green){
cd.color = btnCol;
}else if(obj==plus){ // Resize
cd.w++; cd.h++;
}else if(obj==minus){
If(cd.w > 3){ // Set Minimum
cd.w--; cd.h--;
}
}else if(obj==erase){
cd.color = cd.getBackground();
}else if(obj==eraseAll){
cd.sw=1;
cd.repaint();
}
}
}
public static void main(String[] args) {
paintSampleEx ps = new paintSampleEx();
ps.setBounds(300,100,600,500);
ps.setVisible(true);
}
}
class CanvasDemo extends Canvas{
int x= -10, y= -10, w=10, h=10;
int sw= 0;
Color color = Color.blue;
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(sw == 0){
g.setColor(color);
g.fillOval(x, y, w, h); // original output
}else if(sw == 1){
g.clearRect(0, 0, 300, 300);
}
}
}
The ActionListener was not running when the Clear and Clear All button was pressed.
In this case, it is recommended to check if you recognize that the button is pressed by inserting an output statement as shown below.
}else if(obj==erase){
System.out.println("Erase");
cd.color = cd.getBackground();
}
If you find the part with the code below and change addMouseMotionListener to addActionListener, it works fine.
erase.addMouseMotionListener(new EventHandler());
eraseAll.addMouseMotionListener(new EventHandler());
© 2024 OneMinuteCode. All rights reserved.