Java 在重画图像后,如何继续在其上绘制?

Java 在重画图像后,如何继续在其上绘制?,java,swing,jpanel,bufferedimage,paintcomponent,Java,Swing,Jpanel,Bufferedimage,Paintcomponent,我的程序旨在让用户在BuffereImage上绘制,并打开/保存要绘制的文件。打开保存的BuffereImage后,无法继续在其上绘制。下面是我的代码。如果您有任何其他关于代码迁移或简化的建议,我们将不胜感激 import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.

我的程序旨在让用户在BuffereImage上绘制,并打开/保存要绘制的文件。打开保存的BuffereImage后,无法继续在其上绘制。下面是我的代码。如果您有任何其他关于代码迁移或简化的建议,我们将不胜感激

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);
}
_

@覆盖
已执行的公共无效操作(操作事件e){
对象源=e.getSource();
对于(int i=0;i
在读取新的BuffereImage后,需要更新Graphics2D对象。否则,你仍在试图画旧的(现在已经消失的)图像。

@AlexMarkenzon:我同意:我认为这根本没有帮助。但是试着在
ImageIO.read(…)
之后调用它,你可能会有更好的结果。是的,我回头看时注意到了这一点。在读取后移动它是有效的。谢谢不客气。你在我的评论发布之前删除了你的评论。如果他们帮助你解决你的问题,考虑一下这个问题(并且回答前面的问题)。
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);
}
}