gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:44:50 26 4
gpt4 key购买 nike

我是 Java swing 桌面图形应用程序的新手,我遇到了一个问题,请建议我需要选择一些矩形并填充矩形,我们是否有任何预定义的概念,例如橡皮筋(如果有),请建议我开始一个绘图

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;

@SuppressWarnings("serial")
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);
}
}

最佳答案

你应该从改变开始导入java.awt.event.MouseMotionListener;

导入java.awt.event.MouseListener;

mousePressed 不与 MouseMotionListener 一起使用

关于Java swing图形绘制并用颜色填充选定的矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24804172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com