重绘后如何继续在图像上绘画?

How do I continue to paint on an image after redrawing it?

我的程序旨在让用户在缓冲图像和 open/save 文件上绘画。打开保存的缓冲图像后,无法继续在其上绘画。下面是我的代码。对于移动代码或使其更简单的任何其他建议,我们将不胜感激。

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class MySmallPaint extends JPanel implements MouseListener, MouseMotionListener, ActionListener{

private int myX = -10, myY = -10;
private int radius = 5;

BufferedImage img = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();

private JPanel bucket = new JPanel(new GridLayout(7, 2));
private JPanel northPanel = new JPanel(new GridBagLayout());
private JButton[] buttons = new JButton[11];
private Color[] colorList = {Color.RED, Color.BLUE, Color.ORANGE, Color.CYAN, Color.YELLOW, Color.GREEN, 
                             Color.WHITE, Color.MAGENTA, Color.GRAY, Color.PINK, Color.BLACK};
private Color currentColor = Color.BLACK;
private String[] pencilSize = {"1", "5", "10", "15", "20"};
private JComboBox sizeList = new JComboBox(pencilSize);
private JLabel thickness = new JLabel("Thickness");
private JButton clear = new JButton("Clear");
private JButton save = new JButton("Save");
private JButton open = new JButton("Open");
JFileChooser fc = new JFileChooser();

public MySmallPaint(){
    super();
    setImage();
    setLayout(new BorderLayout());
    addMouseListener(this);
    addMouseMotionListener(this);
    sizeList.setEditable(true);

    for(int i = 0; i < buttons.length; i++){
        buttons[i] = new JButton();
        buttons[i].setBackground(colorList[i]);
        buttons[i].addActionListener(this);
        bucket.add(buttons[i]);
    }

    northPanel.add(thickness);
    northPanel.add(sizeList);
    sizeList.addActionListener(this);
    northPanel.add(open);
    open.addActionListener(this);
    northPanel.add(save);
    save.addActionListener(this);
    northPanel.add(clear);
    clear.addActionListener(this);

    add(bucket, BorderLayout.WEST);
    add(northPanel, BorderLayout.NORTH);
}

绘制组件

public void paintComponent(Graphics g){
    super.paintComponents(g);
    g2d.setColor(currentColor);
    g2d.fillOval(myX - radius, myY - radius, 2 * radius, 2 * radius);
    if(img != null)
        g.drawImage(img, 0, 0, null);
}

_

@Override
public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    for(int i = 0; i < buttons.length; i++)
        if(source == buttons[i])
            currentColor = colorList[i];

    //Clears the panel
    if(source == clear){
        setImage();
    }

    //Changes radius
    else if(source == sizeList)
        radius = Integer.parseInt((String) sizeList.getSelectedItem());

    //open a file
    if(source == open){
        int returnValue = fc.showOpenDialog(null);
        if (returnValue == fc.APPROVE_OPTION) {
            File sf = fc.getSelectedFile();
            try { 
                img = ImageIO.read(sf);
                repaint();
            } 
            catch (IOException e2) { 
                e2.printStackTrace();
            }
        }
    }

    //save a file
    else if(source == save){
        fc.setDialogTitle("Specify a file to save");   

        int userSelection = fc.showSaveDialog(northPanel);

        if (userSelection == JFileChooser.APPROVE_OPTION) {
            File fileToSave = fc.getSelectedFile();
            try{
                ImageIO.write(img, "png", new File("pic.png"));
                repaint();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    }
}

@Override
public void mouseDragged(MouseEvent e) {
    // TODO Auto-generated method stub
    myX = e.getX();
    myY = e.getY();
    repaint();
}

@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

@Override
public void mouseClicked(MouseEvent e) {
    myX = e.getX();
    myY = e.getY();
    repaint();
}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

public void setImage (){
    g2d.setColor(Color.WHITE);
    g2d.fillRect(0, 0, img.getWidth(), img.getHeight());
    g2d.dispose();
    g2d = img.createGraphics();
}

public static void main(String[] args){
     JFrame f = new JFrame("Swing Paint Demo");
     f.setBackground(Color.WHITE);
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(new MySmallPaint());
     f.setVisible(true);
     f.setResizable(false);
     f.setSize(1000,1000);
}
} 

读入新的 BufferedImage 后需要更新 Graphics2D 对象。否则,您仍在尝试绘制旧的(现已消失的)图像。