首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java "Paint“应用程序

Java "Paint“应用程序
EN

Stack Overflow用户
提问于 2021-05-06 12:11:31
回答 1查看 46关注 0票数 0

我正在设计这个Java程序,它的作用类似于绘画,虽然它快完成了,但我需要一些帮助。我的问题是:程序应该能够使用剪贴板进行复制和粘贴。正如你所看到的,我还没有弄清楚如何使用剪贴板,所以我做了一个替代品,它不能100%正确地工作。你能帮我弄一下剪贴板吗?我写的代码只显示一个复制的形状...but没有显示很多形状...我已经尝试了LinkedList,矢量,ArrayList,没有任何工作,请建议!

**显示由形状组成的场景的组件。**

代码语言:javascript
运行
复制
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.
    }

  
}
EN

回答 1

Stack Overflow用户

发布于 2021-05-07 08:18:29

您的问题似乎是由创建按钮时的拼写错误引起的:

代码语言:javascript
运行
复制
JButton copyButton = new JButton("Copy");
selectAllButton.addActionListener(new ActionListener() {
//...

注意如何创建一个copyButton,然后向selectAllButton添加一个操作侦听器。要解决此问题,您应该使用:

代码语言:javascript
运行
复制
JButton copyButton = new JButton("Copy");
//Now creating an action listener for the correct button
copyButton.addActionListener(new ActionListener() {
//...

调试此类问题的一种方法是在每个步骤中向控制台输出更多信息,例如,下面的te将向我们显示从未调用过copySelectedShapes方法,向后工作,您将能够找到上面的拼写错误:

代码语言:javascript
运行
复制
public void copySelectedShapes() {
    //Ad print statement outside the loop so it works even if there are zero shapes
    System.out.println("Copy method called for " + shapes.size() + "shapes");
    for (SceneShape s : shapes) {
        copiedShape = s.copy();
        copiedShapesList.add(copiedShape);
        //Print out the shape toString ID
        System.out.println("Copied shape " + s.toString());
    }
}

注意:在您的SceneComponent类中,您可以覆盖add方法,但是,JPanel类已经有一个用于添加新swing组件的默认add方法,您应该将add方法重命名为addShape,如下所示:

代码语言:javascript
运行
复制
//Method name changed to avoid issues
public void addShape(SceneShape s) {

    shapes.add(s);
    s.setSelected(false);
    repaint();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67411740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档