Search code examples
javaappletawtactionlistenermousemotionlistener

Why Does Implementing an ActionListener make the applet shrink(virtually)?


I recently made a MakeDots class in java that when you drag the mouse, it makes dots(yay!). I implemented a MouseMotionListener for that. It worked fine. Then I added some buttons to change the colors of the dots. I added ActionListeners for that. Once I did that, the color-changing worked, but I could only draw in the very tiny borders of the buttons! Why does this happen? How do I fix it? If you need me to post my code just say in the comments, and I will do so.

CODE:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;


public class MakeDots extends Applet implements MouseMotionListener{
    private Graphics graphics = null;
    private Color dotColor = Color.red;
    private Button setRed;
    private Button setPink;
    private Button setPurple;
    private Button clrBtn;
    public void init() {
        setRed = new Button("Red");
        setPink = new Button("Pink");
        setPurple = new Button("Purple");
        clrBtn = new Button("Clear");
        this.addMouseMotionListener(this);
        this.add(setRed);
        this.add(setPink);
        this.add(setPurple);
        this.add(clrBtn);
        setRed.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dotColor = Color.red;
            }
        });
        setPink.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dotColor = Color.pink;
            }
        });
        setPurple.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dotColor = new Color(80, 0, 80);
            }
        });
        clrBtn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                clear();
            }

        });
    }

    public void paint(Graphics g) {
        this.setSize(new Dimension(800, 600));
        graphics = g.create();
        clear();
    }
    public void drawDot(int x, int y) {
        graphics.setColor(dotColor);
        graphics.fillOval(x, y, 10, 10);
    }


    public void clear() {
        Dimension d = this.getSize();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, d.width, d.height);
    }


    @Override
    public void mouseDragged(MouseEvent e) {
            int mouseX = e.getX();
            int mouseY = e.getY();
            drawDot(mouseX, mouseY);
    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
}

Solution

  • There are few things you might want to do.
    Lose the practice of writing actionPerformed(...) everytime you add an actionListener(). Just use getActionCommand() to check which button triggered the event. Also,create a global object of Color() and update it in actionPerformed()