简体   繁体   中英

How to combine paintComponent(Graphics g) with MouseListener and MouseMotionListener

So, I'm trying to combine the use of paintComponent() as well as the use of MouseListener and MouseActionListener, but i get a lot of errors when I run it, and it does not work liek i want it. In this code specifically, i want the program to, when you press, drag, and release the button,to get the coordinates of the press, then the coordinates of the release, then measure how big the shape is, and draw the shape specified by the JComboBox. I also have the color chooser with the button at the bottom of the JFrame. I want to know how i can run the paintComponent() method, without it running automatically, so i can give it specifications before it draws, and have it draw on demand. Also, i want to know if there is another way to do this, and I'm completely wrong as to how I'm approaching it. I hope the explanation wasn't too confusing :). Any help is great, thanks!

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

@SuppressWarnings({"unchecked", "rawtypes"})
public class GraphicGUI extends JPanel implements ActionListener  {

    HandlerClass handler = new HandlerClass();

    public int x1, x2, y1, y2, width, height;
    public String event;
    public JButton colorChooserButton;
    public Color color = (Color.WHITE);
    public JComboBox shapeBox;
    public JLabel eventLabel;


    public GraphicGUI(){
        shapeBox = new JComboBox();
        eventLabel = new JLabel();
        colorChooserButton = new JButton("Choose a color");
        colorChooserButton.addActionListener(this);

        shapeBox.addItem("Oval");
        shapeBox.addItem("Rectangle");
        shapeBox.addItem("Line");

        super.addMouseListener(handler);
        super.addMouseMotionListener(handler);
    }

    public void actionPerformed(ActionEvent arg0){
        if(arg0.getSource().equals(colorChooserButton)){
            color = JColorChooser.showDialog(null, "Pick Your Color", color);
            if(color==null){
                color = (Color.BLACK);
            }
        }
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.white);
        if(shapeBox.getSelectedItem() == "Oval")
            if(event.equals("released")){
                width = x1-x2;
                height = y1-y2;
                g.setColor(color);
                g.fillOval(x1, y1, width, height);
            }
    }

    private class HandlerClass implements MouseListener, MouseMotionListener{

            //Mouse Events
            public void mouseClicked(MouseEvent arg0){
                event = "click";
            }
            public void mousePressed(MouseEvent arg0){
                event = "pressed";
                x1 = arg0.getX();
                y1 = arg0.getY();
                eventLabel.setText(String.format("Mouse pressed at %d, %d", x1, y1));
            }
            public void mouseReleased(MouseEvent arg0){
                event = "released";
                x2 = arg0.getX();
                y2 = arg0.getY();
                eventLabel.setText(String.format("Mouse released at %d, %d", x2, y2));
            }
            public void mouseEntered(MouseEvent arg0){
            }
            public void mouseExited(MouseEvent arg0){
            }

            //Mouse Motion Events
            public void mouseDragged(MouseEvent arg0){
            }
            public void mouseMoved(MouseEvent arg0){
            }

    }
}

You should get to draw the shape to get started.

public void paintComponent(Graphics g){
    //Draw the oval
    g.setColor(color);
    g.fillOval(x1, y1, width, height);
}

public void mouseReleased(MouseEvent arg0){
    event = "released";
    x2 = arg0.getX();
    y2 = arg0.getY();       
    if( x2 > x1 ) {
        width = x2-x1;
    } else {
        width = x1-x2;
    }       
    if( y2 > y1 ) {
        height = y2-y1;
    } else {
        height = y1-y2;
    }
    eventLabel.setText(String.format("Mouse released at %d, %d", x2, y2));  
}

Then (if you want to add more shapes) you need a way to add shape (duh) thus you could have a list of shapes and iterate through that list.

And you really should (have to) initialize all your variables.

Ask if you want more details. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM