Я хочу переместить объекты Shape, которые я создал ранее. Я не могу понять, как изменить (обновить) расположение фигуры. Теперь я получаю сообщение об ошибке:
java.awt.geom.Ellipse2D$Double cannot be cast to java.awt.Graphics2D
Теперь я могу получить доступ к определенной форме, но, похоже, на ней нет setLocation() или чего-то подобного.
Пожалуйста, дайте мне чаевые, потому что я застрял.
Framepublic class Frame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setTitle("Prosty Paint");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Drawer drawer = new Drawer(frame.getComponents());
Menu menu = new Menu(drawer);
frame.setJMenuBar(menu.menuBar);
frame.add(drawer);
frame.setVisible(true);
}
}
Drawerpublic class Drawer extends JPanel implements MouseListener, MouseMotionListener {
private final int defaultSize = 50;
private Color defaultColor = Color.BLACK;
private int oldX, oldY, currentX, currentY;
public String currentShape = "";
public Boolean editMode = false;
private int activeShape;
private final ArrayList<Shape> shapes = new ArrayList<>();
private final ArrayList<Color> colors = new ArrayList<>();
public Drawer(Component... components) {
for (Component c : components) {
c.addMouseListener(this);
c.addMouseMotionListener(this);
}
colors.add(Color.BLACK);
}
@Override
public void mousePressed(MouseEvent e) {
Point point = this.getMousePosition();
currentX = point.x;
currentY = point.y;
if (editMode) {
for (int i = 0; i < shapes.size(); i++) {
Shape shape = shapes.get(i);
if (shape.contains(e.getPoint())) {
System.out.println("Clicked shape " + i);
activeShape = i;
}
}
} else {
switch (currentShape) {
case "circle":
Ellipse2D ellipse2D = new Ellipse2D.Double(currentX, currentY, defaultSize, defaultSize);
shapes.add(ellipse2D);
colors.add(defaultColor);
break;
case "rec":
Rectangle2D rectangle2D = new Rectangle2D.Double(currentX, currentY, defaultSize, defaultSize);
shapes.add(rectangle2D);
colors.add(defaultColor);
break;
}
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
Point point = this.getMousePosition();
currentX = point.x;
currentY = point.y;
if (editMode){
Shape shape = shapes.get(activeShape);
Graphics2D g = (Graphics2D) shape;
g.translate(currentX,currentY);
}
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < shapes.size(); i++) {
Shape shape = shapes.get(i);
Color color = colors.get(i);
g2.setColor(color);
g2.fill(shape);
}
}
}




Вы не предоставили SSCCE (ваш код не может быть скомпилирован мной), поэтому я не могу протестировать свое решение, но ваш подход в методе mouseReleased неверен. Для перевода фигуры нужно заменить
Graphics2D g = (Graphics2D) shape;
g.translate(currentX,currentY);
от
AffineTransform transform = new AffineTransform();
transform.translate(currentX, currentY);
shape = transform.createTransformedShape(shape);
shapes.set(activeShape, shape);
Если это не поможет, укажите SSCCE.
Спасибо, это решение работает. Я добавил transform.translate (currentX - предыдущий, current - предыдущий); чтобы получить правильное смещение перемещения.
Принято к сведению. В следующий раз доставлю SSCCE