views:

43

answers:

1

Can't seem to figure out how to only show one circle. Was trying to //g.clearRect(0, 0, 400, 400); but that clears the background too. Was also trying to just fill the background with yellow again but couldn't get that working either. Any suggestions?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JMouseFrame extends JFrame
        implements MouseListener {

    Container con = null;
    int x, y;
    int size;

    public JMouseFrame() {
        setTitle("JMouseFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        con = this.getContentPane();
        addMouseListener(this);
    }

    public void mousePressed(MouseEvent e) {
        x = e.getX();
        y = e.getY();
    }

    public void mouseClicked(MouseEvent e) {
        int whichButton = e.getButton();
        if (whichButton == MouseEvent.BUTTON1) {
            size = 15;
        } else if (whichButton == MouseEvent.BUTTON3) {
            size = 4;
        }
        repaint();
    }

    public void mouseEntered(MouseEvent e) {
        con.setBackground(Color.yellow);
    }

    public void mouseExited(MouseEvent e) {
        con.setBackground(Color.black);
    }

    public void paint(Graphics g) {
        //g.clearRect(0, 0, 400, 400);
        g.drawOval(x - size, y - size, size * 3, size * 3);
    }

    public static void main(String[] args) {
        JMouseFrame mFrame = new JMouseFrame();
        mFrame.setSize(400, 400);
        mFrame.setVisible(true);
    }

    public void mouseReleased(MouseEvent e) {
    }
}
+1  A: 

It looks like you're mixing AWT and Swing painting. Instead, override paintComponent(), as suggested in the example below. See Painting in AWT and Swing for details.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/3898775 */
public class MousePanel extends JPanel {

    private static final int SIZE = 20;
    Point p = new Point(Short.MAX_VALUE, Short.MAX_VALUE);

    public MousePanel() {
        this.setPreferredSize(new Dimension(400, 400));
        this.setBackground(Color.yellow);
        this.addMouseListener(new MouseHandler());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawOval(p.x - SIZE, p.y - SIZE, SIZE * 2, SIZE * 2);
    }

    private final class MouseHandler extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            p = e.getPoint();
            repaint();
        }
    }

    private void display() {
        JFrame f = new JFrame("MousePanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MousePanel().display();
            }
        });
    }
}

Addendum:

I can't figure out how to make the code you posted work with what I have...

You can restore your mouse methods to the MouseHandler almost verbatim. The only difference is the need to qualify this, e.g.

    @Override
    public void mouseClicked(MouseEvent e) {
        int whichButton = e.getButton();
        if (whichButton == MouseEvent.BUTTON1) {
            MousePanel.this.size = 15;
        } else if (whichButton == MouseEvent.BUTTON3) {
            MousePanel.this.size = 4;
        }
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        MousePanel.this.setBackground(Color.yellow);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        MousePanel.this.setBackground(Color.black);
    }
trashgod
+1, for the link and the code.
camickr
The program must display a JFrame that does the following: Background turns yellow when moust enters frame, Background turns black when mouse leaves the frame, Large circle at point of left click, Small circle at pont of right click, I can't figure out how to make the code you posted work with what I have to just get the last piece done of only allowing one circle to be painted at a time.
Perd
@Perd: I've elaborated above. My example `extends MouseAdapter`, while yours `implements MouseListener`, but the effect is the same.
trashgod