鼠标侦听器无法使用JPanel

时间:2016-01-17 05:15:10

标签: java swing jpanel mouselistener

paintcomponent工作正常,图像显示,在该端或JFrame没有问题。我想实现缩放和平移,但没有得到任何运气,因为添加的鼠标监听器没有响应。

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class map extends JPanel {
    public int moz = 100;
    public void map()
    {
        addMouseListener(
                new MouseAdapter() 
                {
                    @Override
                    public void mouseClicked(MouseEvent e)
                    {
                        moz = moz +100;
                        repaint();
                    }
                }
        );
    }
    public void paintComponent(Graphics g){
        .....
        g.drawLine( 0, moz, 100, 0 );
    }
}

1 个答案:

答案 0 :(得分:2)

你的班级没有真正的构造函数,而是有一个"伪"构造函数,因为它有一个返回类型 - 是void计数。因此,通过更改:

来摆脱void返回类型
// this is not a constructor
public void map()

为:

// this is a real constructor
public map()

另外作为附带建议,更改变量和类名以符合Java命名约定:类名全部以大写字母开头,方法/变量名以小写字母开头。

因此,在您的情况下,您可以为您的类Map命名,并且在使用代码时可能会有以下内容:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.*;

public class Map extends JPanel {
    private static final int PREF_W = 800;
    private static final int PREF_H = 650;
    private static final Color DRAW_RECT_COLOR = new Color(200, 200, 255);
    public static final Stroke IMAGE_STROKE = new BasicStroke(3f);
    public static final Color IMAGE_COLOR = Color.RED;
    private BufferedImage image = new BufferedImage(PREF_W, PREF_H, BufferedImage.TYPE_INT_ARGB);
    private Rectangle drawRectangle = null;
    private List<Color> colors = new ArrayList<>();
    private Random random = new Random();

    public Map() {
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
        for (int r = 0; r < 4; r++) {
            int r1 = (r * 255) / 3;
            for (int g = 0; g < 4; g++) {
                int g1 = (g * 255) / 3;
                for (int b = 0; b < 4; b++) {
                    int b1 = (b * 255) / 3;
                    colors.add(new Color(r1, g1, b1));
                }
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
        Graphics2D g2 = (Graphics2D) g;
        if (drawRectangle != null) {
            g.setColor(DRAW_RECT_COLOR);
            g2.draw(drawRectangle);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class MyMouse extends MouseAdapter {
        Point p1 = null;

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

        @Override
        public void mouseDragged(MouseEvent e) {
            if (p1 == null) {
                return;
            }
            Point p2 = e.getPoint();
            drawRectangle = createDrawRect(p2);
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            Rectangle rectangle = createDrawRect(e.getPoint());
            Graphics2D g2 = image.createGraphics();
            g2.setStroke(IMAGE_STROKE);
            Color c = colors.get(random.nextInt(colors.size()));
            g2.setColor(c);
            g2.draw(rectangle);
            g2.dispose();
            p1 = null;
            drawRectangle = null;
            repaint();
        }

        private Rectangle createDrawRect(Point p2) {
            int x = Math.min(p1.x, p2.x);
            int y = Math.min(p1.y, p2.y);
            int w = Math.abs(p1.x - p2.x);
            int h = Math.abs(p1.y - p2.y);
            return new Rectangle(x, y, w, h);
        }

    }

    private static void createAndShowGui() {
        Map mainPanel = new Map();

        JFrame frame = new JFrame("Map");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}