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

时间:2015-05-10 19:10:06

标签: java swing jpanel bufferedimage paintcomponent

我的程序是让用户在bufferedimage上绘画并打开/保存要绘制的文件。打开我保存的bufferedimage后,我无法继续绘画。以下是我的代码。任何其他有关移动代码或使代码更简单的建议都将受到赞赏。

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

1 个答案:

答案 0 :(得分:3)

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