【发布时间】:2021-05-06 04:11:30
【问题描述】:
我正在设计这个类似于绘画的 Java 程序,虽然它快完成了,但我需要一些帮助。我的问题是: 该程序应该能够使用剪贴板进行复制和粘贴。如您所见,我无法弄清楚如何使用剪贴板进行操作,所以我做了一个替代品,它不能 100% 正确运行。你能帮我用剪贴板吗? 我编写的代码仅显示 1 个复制的形状...但不显示很多形状... 我已经尝试过 LinkedList、vector、ArrayList 并且没有任何效果 请指教!
** 显示由形状组成的场景的组件。 **
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class SceneComponent extends JPanel {
private final ArrayList<SceneShape> shapes;
private SceneShape copiedShape;
private ArrayList<SceneShape> copiedShapesList;
public void add(SceneShape s) {
shapes.add(s);
s.setSelected(false);
repaint();
}
public void copySelectedShapes() {
for (SceneShape s : shapes) {
copiedShape = s.copy();
copiedShapesList.add(copiedShape);
System.out.println("shapes copied number === " + copiedShapesList.size());
}
}
public void pasteSelectedShapes() {
shapes.addAll(copiedShapesList);
System.out.println("shapes size === " + shapes.size());
repaint();
}
void selectAll() {
for (SceneShape s : shapes) {
s.setSelected(true);
}
repaint();
}
public void removeSelected() {
shapes.removeIf(SceneShape::isSelected);
repaint();
}
void deleteAll() {
shapes.clear();
repaint();
}
public SceneComponent() {
shapes = new ArrayList<>();
copiedShapesList = new ArrayList<>();
setBackground(Color.white);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (SceneShape s : shapes) {
s.draw(g2);
if (s.isSelected()) {
s.drawSelection(g2);
}
}
}
}
**
A house shape.
**
import java.awt.*;
import java.awt.geom.*;
/**
* A house shape.
*/
public class HouseShape extends SelectableShape {
private int x, y;
public HouseShape(int x, int y, int width) {
this.x = x;
this.y = y;
this.width = width;
}
public void draw(Graphics2D g2) {
Rectangle2D.Double base
= new Rectangle2D.Double(x, y + width, width, width);
// The left bottom of the roof
Point2D.Double r1
= new Point2D.Double(x, y + width);
// The top of the roof
Point2D.Double r2
= new Point2D.Double(x + width / 2, y);
// The right bottom of the roof
Point2D.Double r3
= new Point2D.Double(x + width, y + width);
Line2D.Double roofLeft
= new Line2D.Double(r1, r2);
Line2D.Double roofRight
= new Line2D.Double(r2, r3);
g2.draw(base);
g2.draw(roofLeft);
g2.draw(roofRight);
}
@Override
public void drawSelection(Graphics2D g2) {
Rectangle2D.Double base = new Rectangle2D.Double(x, y + width, width, width);
g2.fill(base);
}
public boolean contains(Point2D p) {
return x <= p.getX() && p.getX() <= x + width
&& y <= p.getY() && p.getY() <= y + 2 * width;
}
private final int width;
@Override
public SceneShape copy() {
int i=10;i=i+20;
return new HouseShape(i, 100, 50);
}
}
/***
the main program
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SceneEditor {
public static void main(String[] args) {
int i = 0;
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final SceneComponent scene = new SceneComponent();
JButton houseButton = new JButton("House");
houseButton.addActionListener(new ActionListener() {
int i = 0;
@Override
public void actionPerformed(ActionEvent event) {
scene.add(new HouseShape(i, 50, 50) {
});
i = i + 100;
}
});
JButton selectButton = new JButton("delete All");
selectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
scene.deleteAll();
}
});
JButton selectAllButton = new JButton("select All");
selectAllButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
scene.selectAll();
}
});
JButton copyButton = new JButton("Copy");
selectAllButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
scene.copySelectedShapes();
}
});
JButton pasteButton = new JButton("Paste");
pasteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
scene.pasteSelectedShapes();
}
});
JPanel buttons = new JPanel();
buttons.add(houseButton);
buttons.add(selectAllButton);
buttons.add(selectButton);
buttons.add(copyButton);
buttons.add(pasteButton);
frame.setBackground(Color.black);
frame.add(scene, BorderLayout.CENTER);
frame.add(buttons, BorderLayout.NORTH);
frame.setSize(800, 800);
frame.setLocation(300, 100);
frame.setVisible(true);
}
}
the interface for the common methodes that must be implemented
import java.awt.*;
public interface SceneShape
{
abstract SceneShape copy();
void draw(Graphics2D g2);
void drawSelection(Graphics2D g2);
void setSelected(boolean b);
boolean isSelected();
}
import java.awt.*;
import java.awt.geom.*;
public class SelectableShape implements SceneShape {
Rectangle2D rectangle;
Color color;
protected Point upperLeft;
public void setSelected(boolean b) {
selected = b;
}
public boolean isSelected() {
return selected;
}
private boolean selected = false;
@Override
public SceneShape copy() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void draw(Graphics2D g2) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void drawSelection(Graphics2D g2) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
【问题讨论】:
-
你能解释一下“哪个不能 100% 正确运行”是什么意思吗? pasteSelectedShapes 和 copySelectedShapes 方法看起来应该可以正常工作,所以这只是一个选择问题吗?还是需要将形状粘贴到其他位置?
-
我注意到,实际上对 draw 方法执行任何操作的唯一代码是
HouseShape,您制作的所有其他形状对象都有一个空白的draw方法,正如我们在 @987654324 中看到的那样@ 界面。您不应该创建扩展SceneShape的对象并覆盖draw方法,否则SceneShape对象将什么都不做。 -
代码有效,但它只复制 1 个形状!
-
我希望它复制所有选定的形状!
标签: java arraylist copy shapes