简体   繁体   English

如何将paintComponent(Graphics g)与MouseListener和MouseMotionListener结合

[英]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. 因此,我试图结合使用paintComponent()以及使用MouseListener和MouseActionListener,但是我在运行它时遇到很多错误,但是我不希望它起作用。 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. 具体来说,在此代码中,我希望程序在您按下,拖动和释放按钮时,先获取印刷机的坐标,然后获得释放的坐标,然后测量形状的大小,并绘制指定的形状通过JComboBox。 I also have the color chooser with the button at the bottom of the JFrame. 我在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. 我想知道如何在不自动运行的情况下运行paintComponent()方法,因此我可以在绘制之前给它指定规格,并按需绘制它。 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. 然后(如果您想添加更多形状),您需要一种添加形状(duh)的方法,这样您便可以获得一个形状列表并在该列表中进行迭代。

And you really should (have to) initialize all your variables. 您确实应该(必须)初始化所有变量。

Ask if you want more details. 询问是否需要更多详细信息。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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