Java 如何突出显示两个形状之间的重叠区域
关于我的代码,我有3个任务 1==>如何在我的代码中删除所选形状;当我在每个形状上单击鼠标右键时, 2==>我完全不知道如何突出显示重叠的部分 3==>现在,当我单击我的JPanel时,从鼠标单击的点绘制形状,它应该位于鼠标指针的中心 提前谢谢 事实上,我对Java还不熟悉。这是我的密码Java 如何突出显示两个形状之间的重叠区域,java,swing,Java,Swing,关于我的代码,我有3个任务 1==>如何在我的代码中删除所选形状;当我在每个形状上单击鼠标右键时, 2==>我完全不知道如何突出显示重叠的部分 3==>现在,当我单击我的JPanel时,从鼠标单击的点绘制形状,它应该位于鼠标指针的中心 提前谢谢 事实上,我对Java还不熟悉。这是我的密码 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Rectangle; im
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import javax.swing.JPanel;
import javax.swing.JButton;
import Delete.Selection;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.util.ArrayList;
public class MyPanel extends JPanel {
ArrayList<MyRect> list = new ArrayList<MyRect>();
public MyPanel() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
MyRect r = new MyRect(e.getX(), e.getY());
list.add(r);
repaint();
}
else {
list.clear();
repaint();
}
}
}
);
setPreferredSize(new Dimension(600, 400));
setBackground(Color.CYAN);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for (int i = 0; i < list.size(); i++) {
MyRect r = list.get(i);
g.fillRect(r.x, r.y, r.w, r.h);
}
}
class MyRect {
int x, y, w=100, h=100;
Color c = Color.BLACK;
public MyRect(int x, int y, int w, int h, Color color) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.c = color;
}
public MyRect(int x, int y) {
this.x = x;
this.y = y;
}
}}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Rectangle;
导入java.awt.Shape;
导入javax.swing.JPanel;
导入javax.swing.JButton;
导入、删除、选择;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.geom.Area;
导入java.util.ArrayList;
公共类MyPanel扩展了JPanel{
ArrayList=新建ArrayList();
公共事务委员会(){
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效鼠标按下(MouseEvent e){
如果(例如getButton()==MouseEvent.BUTTON1){
MyRect r=新的MyRect(e.getX(),e.getY());
列表。添加(r);
重新油漆();
}
否则{
list.clear();
重新油漆();
}
}
}
);
setPreferredSize(新尺寸(600400));
挫折背景(颜色:青色);
}
公共组件(图形g){
超级组件(g);
对于(int i=0;i
如何突出显示重叠区域
您可以使用Rectangle
类的intersection(…)
方法来获取要绘制的矩形:
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class IntersectingRectangles extends JPanel
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Dimension d = getSize();
int width = d.width * 3 / 4;
int height = d.height * 3 / 4;
Rectangle r1 = new Rectangle(0, 0, width, height);
g2d.setColor( Color.BLUE );
g2d.fill( r1 );
Rectangle r2 = new Rectangle(d.width - width, d.height - height, width, height);
g2d.setColor( Color.YELLOW );
g2d.fill( r2 );
// Specific solution when using Rectangles only
Rectangle r3 = r1.intersection(r2);
g2d.setColor(Color.GREEN);
g2d.fill(r3);
/*
// For a more generic solution using any Shape
Area area = new Area(r1);
area.intersect( new Area(r2) );
g2d.setColor(Color.GREEN);
g2d.fill(area);
*/
g2d.dispose();
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(300, 300);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Intersecting Rectangles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new IntersectingRectangles());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
如何删除我的代码中选定的形状;当我在每个形状上单击鼠标右键时
您需要遍历包含矩形的ArrayList。然后可以使用Rectangle.contains(yourMousePoint)
方法确定单击的矩形。您需要保存对矩形的引用。然后,当循环完成执行时,可以从ArrayList中删除矩形
从鼠标单击的点绘制的形状,它应该位于鼠标指针的中心
然后需要更改矩形的x/y位置。应该是:
int x = mousePoint.x - (width / 2);
int y = mousePoint.y - (height / 2);
其中,宽度/高度表示要绘制的矩形的大小。类似于或?@MadProgrammer。是的先生像这样的,