簡體   English   中英

當我繪制另一個矩形時,如何保留我在 Jpanel 上繪制的矩形?

[英]How to keep the rectangle that I drew on the Jpanel when I draw another rectangle?

我在 JAVA 中有這段代碼,當我在面板上拖動鼠標時,它會像繪圖應用程序一樣繪制一個矩形。 每次我單擊並拖動以創建一個新矩形時,前一個矩形就會消失。 我想知道是否有辦法讓它留在面板上。 對於有多個矩形,就像 windows 上的繪圖應用程序一樣。


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

    public class DrawRect extends JPanel {

        int x, y, x2, y2;

        public static void main(String[] args) {
            JFrame f = new JFrame("Draw Box Mouse 2");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(new DrawRect());
            f.setSize(300, 300);
            f.setVisible(true);
        }

        DrawRect() {
            x = y = x2 = y2 = 0; // 
            MyMouseListener listener = new MyMouseListener();
            addMouseListener(listener);
            addMouseMotionListener(listener);
        }

        public void setStartPoint(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public void setEndPoint(int x, int y) {
            x2 = (x);
            y2 = (y);
        }

        public void drawPerfectRect(Graphics g, int x, int y, int x2, int y2) {
            int px = Math.min(x,x2);
            int py = Math.min(y,y2);
            int pw=Math.abs(x-x2);
            int ph=Math.abs(y-y2);
            g.drawRect(px, py, pw, ph);
        }

        class MyMouseListener extends MouseAdapter {

            public void mousePressed(MouseEvent e) {
                setStartPoint(e.getX(), e.getY());
            }

            public void mouseDragged(MouseEvent e) {
                setEndPoint(e.getX(), e.getY());
                repaint();
            }

            public void mouseReleased(MouseEvent e) {
                setEndPoint(e.getX(), e.getY());
                repaint();
            }
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            drawPerfectRect(g, x, y, x2, y2);
        }

    }

編寫 Swing 圖形程序時要記住一些有用的事情:

  • paintComponent每次調用時都會重新繪制整個組件,因此您需要繪制您想要出現的所有內容。 您對Graphics object 執行的操作只會持續到下一次調用paintComponent
  • Graphics2DGraphics更有能力。 它用浮點坐標繪制了很好的抗鋸齒形狀,各種線條 styles 等等。 paintComponentGraphics參數實際上始終是一個Graphics2D實例,您可以將其轉換為一個而不必擔心。
  • 組件的 inheritance 層次結構也是繪畫層次結構,因此您需要在您的paintComponent方法的開頭調用super.paintComponent(g) 有關更多詳細信息,請參閱此問題

所以我對你的程序做了一些改變:

  • 添加了一個字段List<Rectangle2D> rectangles以記住用戶創建的矩形。 每次抬起鼠標按鈕時,都會在此列表中添加一個矩形。
  • 添加了對super.paintComponent的調用
  • 使用Graphics2D繪制矩形,線條樣式更有趣( Stroke
  • 更改了您的paintComponent以繪制列表中的每個矩形,以及用戶當前正在繪制的“橡皮筋”矩形。

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

public class DrawRect extends JPanel {

    int x, y, x2, y2;
    // this is the list of all the rectangles the user has drawn so far
    List<Rectangle2D> rectangles = new ArrayList<>();

    public static void main(String[] args) {
        JFrame f = new JFrame("Draw Box Mouse 2");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(new DrawRect());
        f.setSize(300, 300);
        f.setVisible(true);
    }

    DrawRect() {
        x = y = x2 = y2 = 0; //
        MyMouseListener listener = new MyMouseListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);
    }

    public void setStartPoint(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setEndPoint(int x, int y) {
        x2 = (x);
        y2 = (y);
    }

    // this creates a new rectangle object instead of drawing one
    public Rectangle2D makePerfectRect(int x, int y, int x2, int y2) {
        int px = Math.min(x,x2);
        int py = Math.min(y,y2);
        int pw=Math.abs(x-x2);
        int ph=Math.abs(y-y2);
        return new Rectangle2D.Float(px, py, pw, ph);
    }

    class MyMouseListener extends MouseAdapter {

        public void mousePressed(MouseEvent e) {
            setStartPoint(e.getX(), e.getY());
        }

        public void mouseDragged(MouseEvent e) {
            setEndPoint(e.getX(), e.getY());
            repaint();
        }

        public void mouseReleased(MouseEvent e) {
            setEndPoint(e.getX(), e.getY());
            // when the mouse is released, we add the current rectangle to our list
            rectangles.add(makePerfectRect(x,y,x2,y2));
            repaint();
        }
    }

    public void paintComponent(Graphics g) {
        // use Graphics2D, you can draw much nicer lines
        Graphics2D g2d = (Graphics2D) g;
        super.paintComponent(g);
        g.setColor(Color.RED);
        // draw the rectangle the user is currently drawing
        g2d.draw(makePerfectRect(x,y,x2,y2));
        g.setColor(Color.BLUE);

                Stroke oldStroke = g2d.getStroke();
        g2d.setStroke(new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0.0f));
        for (Rectangle2D r : rectangles) {
            g2d.draw(r);
        }
        g2d.setStroke(oldStroke); // restore the original Stroke
    }

}

暫無
暫無

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

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