我正在解决一个关于AWT的Java问题。当您单击鼠标的右键时。它将创建一个椭圆形。如果单击左按钮,它将删除您单击的椭圆形。我已经实现了椭圆形的创建,但我找不到任何关于在 AWT 中删除形状的内置函数。我的代码如下:
package chapter16;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Points extends JFrame{
public Points(){
add(new NewPanel4());
}
public static void main(String[] args){
Points frame = new Points();
frame.setSize(200, 200);
frame.setTitle("Add and Remove Points");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class NewPanel4 extends JPanel{
private boolean Add = false;//add points
private int x,y;//the x,y coordinate
public NewPanel4(){
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getButton()== MouseEvent.BUTTON3){
Add = true;
x = e.getX();
y = e.getY();
repaint();
}
else if(e.getButton() == MouseEvent.BUTTON1){
Add = false;
}
}
});
}
protected void paintComponent(Graphics g){
//super.paintComponent(g);
//Add oval if Add is true
if (Add == true){
g.fillOval(x, y, 10, 10);
}
//remove oval if Add is false
else if(Add == false){
//code to remove ovals
}
}
}
}
我已经重构了您的代码以使用集合来存储椭圆形。希望对您有所帮助。这与安德鲁的建议相同。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Points extends JFrame {
public Points() {
add(new NewPanel4());
}
public static void main(String[] args) {
Points frame = new Points();
frame.setSize(200, 200);
frame.setTitle("Add and Remove Points");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class NewPanel4 extends JPanel {
private List<Point> points = new ArrayList<>();
public NewPanel4() {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
Point center = new Point(e.getX(), e.getY());
points.add(center);
repaint();
} else if (e.getButton() == MouseEvent.BUTTON1) {
removeOval(new Point(e.getX(), e.getY()));
repaint();
}
}
});
}
private void removeOval(Point selectedPoint) {
for (Iterator<Point> iterator = points.iterator(); iterator.hasNext();) {
Point center = iterator.next();
if (isInsideOval(selectedPoint, center)) {
iterator.remove();
break;
}
}
}
private boolean isInsideOval(Point point, Point center) {
int r = 5;
int dx = point.x - center.x;
int dy = point.y - center.y;
return (dx * dx + dy * dy <= r * r);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
super.paintComponent(g);
g2D.setColor(Color.BLUE);
for (Point point : points) {
g2D.fillOval(point.x- (10 / 2) , point.y- (10 / 2), 10, 10);
}
}
}
}