我怎样才能把行动交给「;MouseListener“;用java绘制形状?

我怎样才能把行动交给「;MouseListener“;用java绘制形状?,java,swing,Java,Swing,这是我的密码 我想在我单击的位置绘制矩形,但它没有绘制任何东西:(,我不知道如何将动作赋予“MouseListener” 谢谢您的帮助。首先,我要说明的是图形g=null;由于您从未分配过任何内容,它很可能会保持null 然后,我将转到一个点,即永远不会调用init,但是您的init方法让我害怕 public void init(){ Transparenttxom panel=new Transparenttxom(); panel.addMouseListener(this)

这是我的密码 我想在我单击的位置绘制矩形,但它没有绘制任何东西:(,我不知道如何将动作赋予“MouseListener”


谢谢您的帮助。

首先,我要说明的是
图形g=null;
由于您从未分配过任何内容,它很可能会保持
null

然后,我将转到一个点,即永远不会调用
init
,但是您的
init
方法让我害怕

public void init(){
    Transparenttxom panel=new Transparenttxom();
    panel.addMouseListener(this);
}//end init
为什么要创建一个新的
Transparenttxom
?简单调用
addMouseListener(this)

但即便如此,这

public void paint(Graphics g){
}
意味着什么也得不到画

首先看一看,了解更多细节

您应该重写
paintComponent
,而不是重写
paintComponent
,确保在您自己进行任何绘制之前调用
super.paintComponent

mouseClicked
事件中,您应该定义要绘制的内容,并简单地调用
repaint
,这将触发一个绘制事件,该事件最终将调用您在其中绘制内容的
paintComponent

更新了一个基本示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Transparenttxom extends JPanel implements MouseListener {

    private Point mousePoint;

    public Transparenttxom() {
        addMouseListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (mousePoint != null) {
            g.drawRect(20, 20, mousePoint.x - 20, mousePoint.y - 20);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }



    public void mouseClicked(MouseEvent e) {
        mousePoint = e.getPoint();
        repaint();
    }//end mouseclicked method

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }
//********************************************************************

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new Transparenttxom());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

添加如下侦听器:

public Transparenttxom() {
    // TODO Auto-generated constructor stub
    super();
    this.addMouseListener(this);
}
您的
init
方法永远不会被调用。 并且
g
为空

试试这个:

public void mouseClicked(MouseEvent e){
    int mousex=e.getX();
    int mousey=e.getY();
    Graphics g = this.getGraphics();
    g.drawRect(20,20,mousex,mousey);

}//end mouseclicked method

整个代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Transparenttxom extends JPanel implements MouseListener {

    Graphics g=null;

    public Transparenttxom() {
        // TODO Auto-generated constructor stub
        super();
        this.addMouseListener(this);
    }
    public void init(){
        Transparenttxom panel=new Transparenttxom();
        System.out.println("no");
        panel.addMouseListener(this);
    }//end init
    //********************************************************************
    public void paint(Graphics g){
    }
    //********************************************************************
    public void mouseClicked(MouseEvent e){
        int mousex=e.getX();
        int mousey=e.getY();
        Graphics g = this.getGraphics();
        g.drawRect(20,20,mousex,mousey);

    }//end mouseclicked method
    //********************************************************************
    public void mouseEntered(MouseEvent e){
    }
    public void mouseExited(MouseEvent e){
    }
    public void mousePressed(MouseEvent e){
    }
    public void mouseReleased(MouseEvent e){
    }
    //********************************************************************
    public static void main(String[] args) {
        Transparenttxom panel=new Transparenttxom();
        JFrame frame = new JFrame("java lover");
        frame.add(panel);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
查看两种常见的自定义绘制方法:

  • 保留要绘制的对象的阵列列表
  • 在缓冲区图像上绘制

  • 在这两个示例中,mouseReleased代码实际上保存了要绘制的对象。在您的情况下,您希望在mousePressed上添加矩形。

    请不要建议人们使用
    getGraphics
    ,这不是Swing中绘制的方式…@MadProgrammer>它是这样工作的,我是这样使用的。请给我一些提示,以改进我的设计方法。-当重新绘制事件发生时,以这种方式绘制到
    图形上的内容将被清除…记住,Swing使用被动绘制引擎,这意味着可以出于任何原因在任何时间进行重新绘制…以您的示例为例,单击面板上的某个位置,然后调整窗口大小…查看所绘制的内容我们希望它消失…这是您不想使用的原因之一,也是我们不想推荐使用
    getGraphics
    作为执行自定义绘制的方法的原因之一…同意@MadProgrammer getGraphics/2D中的任何内容用于打印到打印机,或将实际快照保存到BufferedImage
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Transparenttxom extends JPanel implements MouseListener {
    
        Graphics g=null;
    
        public Transparenttxom() {
            // TODO Auto-generated constructor stub
            super();
            this.addMouseListener(this);
        }
        public void init(){
            Transparenttxom panel=new Transparenttxom();
            System.out.println("no");
            panel.addMouseListener(this);
        }//end init
        //********************************************************************
        public void paint(Graphics g){
        }
        //********************************************************************
        public void mouseClicked(MouseEvent e){
            int mousex=e.getX();
            int mousey=e.getY();
            Graphics g = this.getGraphics();
            g.drawRect(20,20,mousex,mousey);
    
        }//end mouseclicked method
        //********************************************************************
        public void mouseEntered(MouseEvent e){
        }
        public void mouseExited(MouseEvent e){
        }
        public void mousePressed(MouseEvent e){
        }
        public void mouseReleased(MouseEvent e){
        }
        //********************************************************************
        public static void main(String[] args) {
            Transparenttxom panel=new Transparenttxom();
            JFrame frame = new JFrame("java lover");
            frame.add(panel);
            frame.setSize(300, 300);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        }
    }