When I press the free button I can draw in the bottom pane but when I press the rectangle button I can draw a rectangle in the panel but I can also draw anything in the same time. My goal is that when I press one of the functions I want the other to stop functioning.
public class MyFrame extends JFrame implements ActionListener, MouseListener , MouseMotionListener{
JPanel top;
JPanel bottom ;
JButton btn1,btn4;
int freebtn,rectanglebtn;
Graphics g;
public MyFrame(){
this.setTitle("car");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700,700);
this.setLayout(null);
this.setResizable(false);
top = new JPanel();
bottom = new JPanel();
top.setBounds(0, 0, 700, 100);
top.setBackground(Color.blue);
bottom.setBounds(0, 100, 700, 600);
bottom.setBackground(Color.white);
btn1 = new JButton("free");
btn4 = new JButton("rectangle");
btn1.addActionListener(this);
btn4.addActionListener(this);
top.add(btn1);top.add(btn4);
this.add(top);
this.add(bottom);
bottom.addMouseMotionListener(this);
bottom.addMouseListener(this);
this.setVisible(true);
g=bottom.getGraphics();
}
// action listener-------------------------------
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() =="free") {
System.out.println("free button");
freebtn=11;
}
else if(e.getActionCommand() =="rectangle") {
System.out.println("rectangle button");
rectanglebtn=12;
}
}
// mouse listener-----------------------------
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(rectanglebtn);
if(rectanglebtn==12) {
g.setColor(Color.BLUE);
g.drawRect(e.getX(),e.getY(), 100, 50);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
// mouse motion listener---------------------------
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(freebtn);
if(freebtn==11) {
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),20,20);
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
I tried to to set one of them to null when I'm using the other but it didnt work