微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java“绘画”应用程序

如何解决Java“绘画”应用程序

我正在设计这个类似于绘画的 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,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.
    }

  
}

解决方法

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

JButton copyButton = new JButton("Copy");
selectAllButton.addActionListener(new ActionListener() {
//...

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

JButton copyButton = new JButton("Copy");
//Now creating an action listener for the correct button
copyButton.addActionListener(new ActionListener() {
//...

调试此类问题的一种方法是在每一步都将更多信息打印到控制台,例如下面的 te 将向我们展示从未调用过 copySelectedShapes 方法,并且向后工作您将能够找到上面的错字:

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 像这样:

//Method name changed to avoid issues
public void addShape(SceneShape s) {

    shapes.add(s);
    s.setSelected(false);
    repaint();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。