簡體   English   中英

如何在Java中將動作賦予“ MouseListener”以繪制形狀?

[英]how can I give the action to “MouseListener” to draw a shape in java?

這是我的代碼,我想在單擊的位置繪制矩形,但沒有繪制任何東西:(,我不知道如何將動作傳遞給“ MouseListener”

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

Graphics g=null;

public void init(){
    Transparenttxom panel=new Transparenttxom();
    panel.addMouseListener(this);
    }//end init
//********************************************************************
        public void paint(Graphics g){
            }
//********************************************************************
public void mouseClicked(MouseEvent e){
    int mousex=e.getX();
    int mousey=e.getY();

    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);

    }
}

感謝您的幫助。

我將從Graphics g=null;的事實開始Graphics 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){
}

意味着什么都不會畫...

首先查看“ 執行自定義繪畫”以獲取更多詳細信息

而不是覆蓋paint ,您應該覆蓋paintComponent ,確保在執行自己的任何繪畫之前先調用super.paintComponent

mouseClicked事件中,您應該定義要繪制的內容,只需調用repaint即可觸發一次paint事件,該事件最終將在您繪畫對象的位置調用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);

    }
}

查看“ 自定義繪畫方法”中的兩種常見方法來進行自定義繪畫:

  1. 保留要繪制對象的ArrayList
  2. 繪制到BufferedImage上

在兩個示例中,mouseReleased代碼實際上都保存了要繪制的對象。 在您的情況下,您想在mousePressed上添加Rectangle。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM