如何在JPanel上拖动和绘制一条线?

时间:2016-05-08 03:13:53

标签: java swing

我正在尝试使用MouseListeners绘制一条线并覆盖paintComponent方法。我现在设置了paintComponent,它在面板上自行绘制一条线。但现在我需要将其更改为当您单击面板并将鼠标拖动到另一个点以创建一条线时。我一直在阅读所有这些教程,我仍然很丢失。谁可以指导我?

到目前为止,这是我的代码:

package gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Line2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawPanel extends JPanel {

    private static final long serialVersionUID = 1697489704611349844L;

    /** The width of the panel. */
    private static final int WIDTH = 600;

    /** The height of the panel. */
    private static final int HEIGHT = 300;

    /** The stroke width in pixels. */
    private static final int STROKE_WIDTH = 1;

    /** The width for the rectangle. */
    private static final int RECTANGLE_WIDTH = 50;

    /** The height for the rectangle. */
    private static final int RECTANGLE_HEIGHT = 50;

    public DrawPanel() {
        super();
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(WIDTH, HEIGHT));   
    }

class LineListener implements MouseListener, MouseMotionListener {
    @Override
    public void mouseClicked(MouseEvent theEvent) {
        theX = theEvent.getX();
        theY = theEvent.getY();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent theEvent) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }


    @Override
    public void paintComponent(final Graphics theGraphics) {
        super.paintComponent(theGraphics);
        final Graphics2D g2d = (Graphics2D) theGraphics;

    // for better graphics display
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);

        final Shape line = new Line2D.Double(10, 10, 100, 100);
        g2d.setPaint(Color.ORANGE);
        g2d.setStroke(new BasicStroke(STROKE_WIDTH));
        g2d.draw(line);
    }


    public static void main(final String[] theArgs) {
        final DrawPanel panel = new DrawPanel();
        final JFrame frame = new JFrame("Drawing Panel Demo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }


}

2 个答案:

答案 0 :(得分:2)

您正在对线路参数10,10,100,100进行硬编码:

final Shape line = new Line2D.Double(10, 10, 100, 100);

制作一个非常不灵活的结构 - 当用户点击并拖动时,你的代码无法改变这一点。解决方法不是这样做,而是使用硬编码的“魔术”数字来使用int变量,这些变量是在代码的MouseListener中更改的变量。在同一个MouseListener内部,您可以在更改参数值后调用repaint(),这将建议JVM重新绘制GUI,然后重新调用paintComponent方法。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class DrawPanel2 extends JPanel {
    private static final int PREF_W = 600;
    private static final int PREF_H = 300;
    private static final Color BG = Color.WHITE;

    // create fields that can be set in your mouse listeners
    // and used in the painting methods
    private Point p1 = null;
    private Point p2 = null;

    public DrawPanel2() {
        setBackground(BG);

        // create your mouse adapter and add as mouse and mouse motion listeners 
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // if your points have been set,.... draw them!
        if (p1 != null && p2 != null) {
            int x1 = p1.x;
            int y1 = p1.y;
            int x2 = p2.x;
            int y2 = p2.y;

            g2.drawLine(x1, y1, x2, y2);
        }
    }

    // to set the size of the GUI
    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    // my mouse adapter inner class
    private class MyMouse extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            p1 = e.getPoint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            p2 = e.getPoint();
            repaint();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            p2 = e.getPoint();
            repaint();
        }
    }

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

        JFrame frame = new JFrame("Draw Panel 2");
        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(() -> createAndShowGui());
    }
}

关于您发布的代码:

  • 让你的内部类扩展MouseAdapter。这个类已经实现了MouseListener和MouseMotionListener以及轮子监听器,你只需要覆盖你想要改变的方法。
  • 您的最新代码不应该编译,因为它尝试使用两个字段,即X和Y,它们尚未在任何地方声明。我猜你的发布只是草率,你很快就把这段代码扔进了你的问题,而没有先尝试编译它。请不要这样做。我们是志愿者试图帮助我们的空闲时间,而你这样做只会让你更难以帮助。
  • 您应该使用两个Point字段(如上所示)或两组x和y int字段,例如x1,y1,x2,y2,并在鼠标侦听器代码中设置这些字段。
  • 您的简单程序的mouseDragged和mouseReleased将是相同的,只需要设置第二个Point(根据我上面的示例),或第二对x和y int字段,然后调用repaint。
  • 然后,paintComponent方法将使用两个Point值(根据上面的示例)或两个x和y int字段对来绘制线条。

答案 1 :(得分:0)

查看鼠标侦听器方法。您将需要一个鼠标侦听器,用于存储已单击或拖动的点列表。然后在paintComponent()中,您可以用与当前绘图相同的方式绘制这些点。

关于鼠标侦听器的课程:http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html