Java 如何使油漆复制粘贴剪切重做?

Java 如何使油漆复制粘贴剪切重做?,java,swing,actionlistener,paintcomponent,Java,Swing,Actionlistener,Paintcomponent,我对下面的代码有一个小问题。本例中的任务之一是创建一个简单的绘制程序。基本部分在这里实现:画一条线,画一个点,然后使用所谓的clear()方法重新绘制面板。额外的部分是按下一个按钮,通过按下该按钮,您的图形将高亮显示自己,然后将有复制粘贴功能以及重做和撤消。 目前我不知道从哪里开始额外的作业。请有人引导和帮助我 import java.awt.*; import java.awt.event.*; import java.awt.geom.Point2D; import javax.swing.

我对下面的代码有一个小问题。本例中的任务之一是创建一个简单的绘制程序。基本部分在这里实现:画一条线,画一个点,然后使用所谓的clear()方法重新绘制面板。额外的部分是按下一个按钮,通过按下该按钮,您的图形将高亮显示自己,然后将有复制粘贴功能以及重做和撤消。 目前我不知道从哪里开始额外的作业。请有人引导和帮助我

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;

// Lahingplaan:


public class MAIN{
    private void displayGUI()
    {   // Loon JFrame'i:
        final JFrame frame = new JFrame("GIMP");
        // Addin frame'le windowlisteneri:  
        frame.addWindowListener(new WindowAdapter() {
              // Loon akna, mis hakkab kasutajat tüütama, kui see tahab panna applicationi kinni:
              public void windowClosing(WindowEvent we)
                {
                    int result = JOptionPane.showConfirmDialog(
                                    frame, "Olete kindel, et soovite applikatsioonist väljuda :((((( :OOOOO ?"
                                    , "YOU REALLY WANNA QUIT?", JOptionPane.YES_NO_OPTION);
                    if (result == JOptionPane.YES_OPTION)
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    else if (result == JOptionPane.NO_OPTION) 
                        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                }
            });
            frame.setVisible(true);
            // Escape'i nupp sulgeb programmi:
            frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                    KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "PROGRAMM SULGUB!"); 
            frame.getRootPane().getActionMap().put("PROGRAMM SULGUB!", new AbstractAction(){ 
                        public void actionPerformed(ActionEvent e)
                        {
                            frame.dispose();
                        }
                    });
            // (1) Võtab ContentPane'i ning määrab selle layouti:
            Container content = frame.getContentPane();
            content.setLayout(new BorderLayout());
            // Funktsioon, mis viitab joonistamisele:
            final PadDraw drawPad = new PadDraw();
            content.add(drawPad, BorderLayout.CENTER);
            // JPanel ja tema propertis:
            JPanel panel = new JPanel();
            panel.setPreferredSize(new Dimension(80, 80));
            panel.setMinimumSize(new Dimension(80, 80));
            panel.setMaximumSize(new Dimension(80, 80));
            content.add(panel, BorderLayout.WEST);
            // Joonista nuppu funktsioon + propertis:
                    JButton drawButton = new JButton("DRAW!");
                    drawButton.setPreferredSize(new Dimension(70, 70));
                    panel.add(drawButton);
                    //same thing except this is the black button
                    drawButton.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                            drawPad.black();
                        }
                    });
            //Copy button funktsioon ja propertis:
            //Paste button funtksioon ja propertis:
            //Clear button funktsioon + propertis:
            JButton clearButton = new JButton("CLEAR!");
            clearButton.setPreferredSize(new Dimension(70, 70));
            panel.add(clearButton);
            clearButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    drawPad.clear();
                }
            });
            frame.setSize(600, 480);
            frame.setLocation(100,100);
            frame.setVisible(true);
    }
public static void main(String[] args)
    {   // New runnable, mis paneb tööle EXITI ja loadib funktsiooni, mis loob painti:
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new MAIN().displayGUI();
                helloworld();
            }
         });
    }
     /*
     Võib osutuda vajalikuks, aga ei pruugi:
    public JMenuBar createMenuBar () {
        JMenuItem menuItem = null;
        JMenuBar menuBar = new JMenuBar();
        JMenu mainMenu = new JMenu("Edit");
        mainMenu.setMnemonic(KeyEvent.VK_E);

    menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
    menuItem.setText("Cut");
    menuItem.setMnemonic(KeyEvent.VK_T);
    mainMenu.add(menuItem);

    menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
    menuItem.setText("Copy");
    menuItem.setMnemonic(KeyEvent.VK_C);
    mainMenu.add(menuItem);

    menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
    menuItem.setText("Paste");
    menuItem.setMnemonic(KeyEvent.VK_P);
    mainMenu.add(menuItem);

    menuBar.add(mainMenu);
    return menuBar;
}
    */

    private static void helloworld() {
        String a,b,c,d;
        a = "xxx";
        b = "xxx";
        c = "xxx";
        d = "xxx";  
    }
}


@SuppressWarnings("serial")
class PadDraw extends JComponent{
    Image image;
    Graphics2D graphics2D;
    int currentX, currentY, oldX, oldY;
    Point2D.Double point;

    //Now for the constructors
    public PadDraw(){
        setDoubleBuffered(true);
        // Ainult punkti joonistamine:
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                oldX = e.getX();
                oldY = e.getY();
                if(graphics2D !=null)
                    graphics2D.drawLine(oldX, oldY, oldX, oldY);
                    repaint();
            }
        });
        // Joone joonistamine:
        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                //Draw line:
                currentX = e.getX();
                currentY = e.getY();
                if(graphics2D != null)
                graphics2D.drawLine(oldX, oldY, currentX, currentY);
                repaint();
                oldX = currentX;
                oldY = currentY;
            }
        });
        //while the mouse is dragged it sets currentX & currentY as the mouses x and y
        //then it draws a line at the coordinates
        //it repaints it and sets oldX and oldY as currentX and currentY
    }
    public void paintComponent(Graphics g){
        if(image == null){
            image = createImage(getSize().width, getSize().height);
            graphics2D = (Graphics2D)image.getGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            clear();
        }
        g.drawImage(image, 0, 0, null);
    }
    //this is the painting bit
    //if it has nothing on it then
    //it creates an image the size of the window
    //sets the value of Graphics as the image
    //sets the rendering
    //runs the clear() method
    //then it draws the image


    public void clear(){
        graphics2D.setPaint(Color.white);
        graphics2D.fillRect(0, 0, getSize().width, getSize().height);
        graphics2D.setPaint(Color.black);
        repaint();
    }
    public void black(){
        graphics2D.setPaint(Color.black);
        repaint();
    }
}
要保存和加载绘制图像的(区域)

  • “撤消”正在加载旧图像
  • “重做”正在加载较新的映像
  • 复制粘贴是保存图像的一个区域并将该区域加载到另一个位置
java.awt.Graphics2D
中有一个
copyrea
用于“复制粘贴”

java.awt.Graphics2D
中有一个
create
来复制图形