没有看到代码的这一部分,这只是一个猜测,但我在想你正在ShapetoResize.resizeSE中创建一个新的形状(int x,int y);方法,从而创建两个形状.
我假设它是你自己的方法,因为在Rectangle或接口Shape(两个内置形状)上没有resizeSE可用.当然,接口Shape上没有paint方法,所以你可能也使用了自定义接口(由于你没有为这个Shape接口提供代码,这会让人感到困惑).如果您发布这些方法的代码,我们可以确认.
以下是以工作方式(和SSCCE)将代码放在一起的示例.我仅限于矩形(因为它们是唯一内置的Shape)并且剥离了许多与调整大小无关的东西.如果您仍遇到问题,请尝试使用此示例重新创建问题:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class ShapeResizer extends Box{
Dimension preferredSize = new Dimension(400,300);
public ArrayList myShapes = new ArrayList();
//Shape that's targeted for editing
Shape currentShape;
public ShapeResizer(){
super(BoxLayout.X_AXIS);
//Shapes (because I don't want to make write code for the user to make shapes)
myShapes.add(new Rectangle(100, 100, 20, 20));
myShapes.add(new Rectangle(200, 200, 30, 30));
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
//deals with identifying shape to resize
int x = e.getX();
int y = e.getY();
boolean foundShape = false;
for (int i = myShapes.size() - 1; i >= 0; i--) {
Shape s = (Shape) myShapes.get(i);
if (s.contains(e.getPoint())) {
//We found a shape to target
currentShape = s;
foundShape = true;
}
}
if(!foundShape){
//Reset the shape and cursor only if needed
if(currentShape != null){
currentShape = null;
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
} else {
setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
}
}
@Override
public void mouseDragged(MouseEvent event) {
if (currentShape != null) {
resizeShape(currentShape, event.getPoint());
}
repaint();
}
});
}
public void resizeShape(Shape s, Point p){
if(s instanceof Rectangle){
Rectangle r = (Rectangle)s;
r.setSize(p.x - r.x, p.y - r.y);
}
}
public void drawShape(Graphics g, Shape s){
if(s instanceof Rectangle){
Rectangle r = (Rectangle)s;
g.drawRect(r.x, r.y, r.width, r.height);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int length = myShapes.size();
for (int i = 0; i < length; i++) {
drawShape(g, myShapes.get(i));
}
}
public Dimension getPreferredSize(){
return preferredSize;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new ShapeResizer());
frame.validate();
frame.pack();
frame.setVisible(true);
}
}