UPD2:
这里是你的代码固定。还有很多其他问题,例如:每次拖动鼠标时,您都会在列表中添加一个点,等等。 它还不够完美,但已经有足够的变化供您分析。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LinesDemo extends JPanel {
private List linesList = new ArrayList<>();
private Line2D line = new Line2D.Double();
Point pointStart = null;
Point pointEnd = null;
// Width and height of rectangular region around mouse
// pointer to use for hit detection on lines
private static final int HIT_BOX_SIZE = 4;
/**
* Create the panel.
*/
public LinesDemo() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
pointEnd = e.getPoint();
line = new Line2D.Double(pointStart, pointEnd);
linesList.add(line);
}
@Override
public void mouseReleased(MouseEvent e) {
line.setLine(pointStart, pointEnd);
pointStart = null;
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
getClickedLine(x, y);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
pointEnd = e.getPoint();
}
@Override
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
line.setLine(pointStart, pointEnd);
repaint();
}
});
}
private Shape getClickedLine(int x, int y) {
int boxX = x - HIT_BOX_SIZE/2;
int boxY = y - HIT_BOX_SIZE/2;
int width = HIT_BOX_SIZE;
int height = HIT_BOX_SIZE;
for(Line2D selectedLine:linesList) {
if (selectedLine.intersects(boxX, boxY, width, height)) {
System.out.println("intersects!");
removeLine(selectedLine);
return selectedLine;
}
}
return null;
}
private void removeLine(Line2D line) {
Iterator it = linesList.iterator();
while(it.hasNext()) {
Line2D selectedLine = it.next();
if(selectedLine.equals(line)) {
it.remove();
repaint();
}
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.RED);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for(Shape content : linesList){
g2d.draw(content);
}
g2d.dispose();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Moving and Scaling");
LinesDemo m = new LinesDemo();
frame.add(m);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
原文: 我无法运行您的程序,但你可以尝试用
boolean repaintNeeded = false;
while(it.hasNext()) {
Line2D selectedLine = it.next();
if(selectedLine.equals(line)) {
it.remove();
repaintNeeded = true;
}
}
if (repaintNeeded) {
repaint();
}
UPD1更换
while(it.hasNext()) {
Line2D selectedLine = it.next();
if(selectedLine.equals(line)) {
it.remove();
repaint();
}
}
: 我能够运行代码。 实际上有很多事情要改进。 对于你的问题而言,这2件事导致你的线没有被抽:
@Override
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
和
if(pointStart != null) {
所以每次松开鼠标时,你的代码将不画任何东西。