Java swing图形绘制并用颜色填充选定的矩形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
 * @Title:
 * @Description:TODO
 * @Company:HF
 * @ClassName:Exam.java  
 * @Author:wushigao
 * @CreateDate:2022年11月29日上午10:39:55
 * @UpdateUser:wushigao
 * @UpdateDate:2022年11月29日 上午10:39:55
 * @Version:1.0
 */
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
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.event.MouseMotionListener;
import java.util.ArrayList;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Exam extends JPanel implements MouseMotionListener {
 
    private static final int recW = 50;
    private static final int MAX = 100;
    private Rectangle[] rect = new Rectangle[MAX];
    private int numOfRecs = 0;
    private int currentSquareIndex = -1;
    private Point startPoint = new Point();
    private Point currentPoint = new Point();
    private int x, y, width, height;
    private Stroke dashedStroke = new BasicStroke(0.0f, BasicStroke.CAP_ROUND,
            BasicStroke.CAP_SQUARE, 5.0f, new float[] { 5f, 5f, 5f, 5f }, 5.0f);
 
    ArrayList list = new ArrayList();
 
    public Exam() {
 
        addRect(50, 50);
        addRect(100, 50);
        addRect(150, 50);
        addRect(200, 50);
        addRect(250, 50);
        addRect(300, 50);
        addRect(350, 50);
        addRect(400, 50);
        addRect(450, 50);
        addRect(500, 50);
        //
        addRect(50, 100);
        addRect(100, 100);
        addRect(150, 100);
        addRect(200, 100);
        addRect(250, 100);
        addRect(300, 100);
        addRect(350, 100);
        addRect(400, 100);
        addRect(450, 100);
        addRect(500, 100);
        //
        addRect(50, 150);
        addRect(100, 150);
        addRect(150, 150);
        addRect(200, 150);
        addRect(250, 150);
        addRect(300, 150);
        addRect(350, 150);
        addRect(400, 150);
        addRect(450, 150);
        addRect(500, 150);
        //
        addRect(50, 200);
        addRect(100, 200);
        addRect(150, 200);
        addRect(200, 200);
        addRect(250, 200);
        addRect(300, 200);
        addRect(350, 200);
        addRect(400, 200);
        addRect(450, 200);
        addRect(500, 200);
 
        addMouseListener(new MouseAdapter() {
 
            @Override
            public void mousePressed(MouseEvent evt) {
                startPoint = evt.getPoint();
            }
 
            public void mouseReleased(MouseEvent e) {
 
                System.out.println(" mouseReleased is" + e.getX() +" y i s"
                        + e.getY());
                Graphics g = getGraphics();
                Graphics2D g2 = (Graphics2D) g;
                g2.setComposite(AlphaComposite.SrcOver.derive(0.8f));
                Color myColour = new Color(255, 0, 0);
                g.setColor(myColour);
                x = Math.min(startPoint.x, currentPoint.x);
                y = Math.min(startPoint.y, currentPoint.y);
                width = Math.abs(startPoint.x - currentPoint.x);
                height = Math.abs(startPoint.y - currentPoint.y);
                rect[numOfRecs] = new Rectangle(x, y, width, height);
                // System.out.println("list size is:"+list.size());
                 Rectangle ggg=new Rectangle(x, y, width, height);
            for(Rectangle rect3d: rect){
                if(ggg.intersects(rect3d)){
                g2.fill(rect3d);
                }
                else
                    System.out.println("doesn't contains");
                }
repaint();
            }
 
        });
 
        addMouseMotionListener(this);
    }
 
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < numOfRecs; i++) {
            ((Graphics2D) g).draw(rect[i]);
            list.add(rect[i]);
        }
 
        x = Math.min(startPoint.x, currentPoint.x);
        y = Math.min(startPoint.y, currentPoint.y);
        width = Math.abs(startPoint.x - currentPoint.x);
        height = Math.abs(startPoint.y - currentPoint.y);
        ((Graphics2D) g).setStroke(dashedStroke);
        g.setColor(Color.BLACK);
        g.drawRect(x, y, width, height);
        // Rectangle dashedRec=new Rectangle(x, y, width, height);
 
    }
 
    public void addRect(int x, int y) {
        if (numOfRecs < MAX) {
            rect[numOfRecs] = new Rectangle(x, y, recW, recW);
            currentSquareIndex = numOfRecs;
            numOfRecs++;
            repaint();
        }
    }
 
    @Override
    public void mouseMoved(MouseEvent event) {
    }
 
    @Override
    public void mouseDragged(MouseEvent event) {
 
        currentPoint = event.getPoint();
        repaint();
    }
 
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setTitle("");
        jFrame.setSize(600, 300);
        Container cPane = jFrame.getContentPane();
        cPane.add(new Exam());
        jFrame.setVisible(true);
    }
}

  

posted @   信铁寒胜  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示