【问题标题】:How do I continue to paint on an image after redrawing it?重绘后如何继续在图像上绘画?
【发布时间】:2015-05-10 19:10:05
【问题描述】:

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

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

【问题讨论】:

    标签: java swing jpanel bufferedimage paintcomponent


    【解决方案1】:

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

    【讨论】:

    • @AlexMarkenzon:我同意:不会认为它会有所帮助。但是尝试在ImageIO.read(...)之后调用它,你可能会得到更好的结果。
    • 是的,当我回头看时,我注意到了。阅读后移动它有效。谢谢!
    • 不客气。在我发布之前,您删除了您的评论。如果他们帮助您解决了问题,请考虑 accepting 这个(以及之前问题的答案)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多