首頁 >Java >java教程 >如何使用滑鼠點擊和拖曳在 JPanel 中繪製線條?

如何使用滑鼠點擊和拖曳在 JPanel 中繪製線條?

Barbara Streisand
Barbara Streisand原創
2024-12-24 16:12:10727瀏覽

How to Draw Lines in a JPanel Using Mouse Clicks and Dragging?

透過點擊按鈕在JPanel 中繪製線條

問題:

問題:

有人想要畫畫點擊按鈕時JPanel 中的線條但由於誤解而面臨困難繪製過程。

解:
  1. 要在JPanel 中繪製線條,建議採用不同的方法:
  2. 使用以下指令標記第一個端點單擊一下。
拖曳滑鼠即可在進度。

釋放滑鼠標記第二個端點。

實現:



以下代碼演示了實現:


導入java.awt.BasicStroke;
導入java.awt.Color;
導入java.awt.Dimension;
導入java.awt.Graphics;
導入java.awt.Graphics2D;
導入java.awt.Point;
導入java.awt.RenderingHints;
導入java.awt.event.ActionEvent;
導入java.awt.event.KeyEvent;
導入java.awt.event.MouseAdapter;
導入java.awt.event.MouseEvent;
導入javax. swing.AbstractAction;

導入javax.swing. JButton;

導入javax.swing.JFrame;

導入javax.swing.JPanel;
private MouseHandler mouseHandler = new MouseHandler();
private Point p1 = new Point(100, 100);
private Point p2 = new Point(540, 380);
private boolean drawing;

public LinePanel() {
    this.setPreferredSize(new Dimension(640, 480));
    this.addMouseListener(mouseHandler);
    this.addMouseMotionListener(mouseHandler);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.blue);
    g2d.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setStroke(new BasicStroke(8,
        BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
    g.drawLine(p1.x, p1.y, p2.x, p2.y);
}

private class MouseHandler extends MouseAdapter {

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

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

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

private class ControlPanel extends JPanel {

    private static final int DELTA = 10;

    public ControlPanel() {
        this.add(new MoveButton("\u2190", KeyEvent.VK_LEFT, -DELTA, 0));
        this.add(new MoveButton("\u2191", KeyEvent.VK_UP, 0, -DELTA));
        this.add(new MoveButton("\u2192", KeyEvent.VK_RIGHT, DELTA, 0));
        this.add(new MoveButton("\u2193", KeyEvent.VK_DOWN, 0, DELTA));
    }

    private class MoveButton extends JButton {

        KeyStroke k;
        int dx, dy;

        public MoveButton(String name, int code,
                final int dx, final int dy) {
            super(name);
            this.k = KeyStroke.getKeyStroke(code, 0);
            this.dx = dx;
            this.dy = dy;
            this.setAction(new AbstractAction(this.getText()) {

                @Override
                public void actionPerformed(ActionEvent e) {
                    LinePanel.this.p1.translate(dx, dy);
                    LinePanel.this.p2.translate(dx, dy);
                    LinePanel.this.repaint();
                }
            });
            ControlPanel.this.getInputMap(WHEN_IN_FOCUSED_WINDOW)
                .put(k, k.toString());
            ControlPanel.this.getActionMap()
                .put(k.toString(), new AbstractAction() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    MoveButton.this.doClick();
                }
            });
        }
    }
}

private void display() {
    JFrame f = new JFrame("LinePanel");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(this);
    f.add(new ControlPanel(), BorderLayout.SOUTH);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new LinePanel().display();
        }
    });
}
導入javax.swing.KeyStroke;


公共類LinePanel 擴展JPanel {

}此程式碼提供了繪製線條的功能使用滑鼠點擊、拖曳和釋放,以及用於控制線條移動的按鈕面板。

以上是如何使用滑鼠點擊和拖曳在 JPanel 中繪製線條?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn