2017-06-15 90 views
0

這是我的代碼。我想保存我在墊上繪製的圖像。有 是一個'保存'按鈕,所以每當我點擊它時,我都希望它保存 繪圖板的圖像。我對java有點了解。請幫助我做到這一點。如何在Java中保存繪圖面板中的圖像?

package paint; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class paint{ 
public static void main(String[] args){ 
    Icon iconB = new ImageIcon("blue.gif"); 

    Icon iconM = new ImageIcon("magenta.gif"); 

    Icon iconR = new ImageIcon("red.gif"); 

    Icon iconBl = new ImageIcon("black.gif"); 

    Icon iconG = new ImageIcon("green.gif"); 


    JFrame frame = new JFrame("Paint It"); 
    //Creates a frame with a title of "Paint it" 

    Container content = frame.getContentPane(); 
    //Creates a new container 
    content.setLayout(new BorderLayout()); 
    //sets the layout 

    final PadDraw drawPad = new PadDraw(); 
    //creates a new padDraw, which is pretty much the paint program 

    content.add(drawPad, BorderLayout.CENTER); 
    //sets the padDraw in the center 

    JPanel panel = new JPanel(); 
    //creates a JPanel 
    panel.setPreferredSize(new Dimension(32, 68)); 
    panel.setMinimumSize(new Dimension(32, 68)); 
    panel.setMaximumSize(new Dimension(32, 68)); 
    //This sets the size of the panel 

    JButton clearButton = new JButton("Clear"); 
    //creates the clear button and sets the text as "Clear" 
    clearButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      drawPad.clear(); 
     } 
    }); 
    //this is the clear button, which clears the screen. This pretty 
    //much attaches an action listener to the button and when the 
    //button is pressed it calls the clear() method 

      JButton saveButton = new JButton("save"); 

    JButton redButton = new JButton(iconR); 
    //creates the red button and sets the icon we created for red 
    redButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      drawPad.red(); 
     } 

    }); 
    //when pressed it will call the red() method. So on and so on =] 

    JButton blackButton = new JButton(iconBl); 
    //same thing except this is the black button 
    blackButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      drawPad.black(); 
     } 
    }); 

    JButton magentaButton = new JButton(iconM); 
    //magenta button 
    magentaButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      drawPad.magenta(); 
     } 
    }); 

    JButton blueButton = new JButton(iconB); 
    //blue button 
    blueButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      drawPad.blue(); 
     } 
    }); 

    JButton greenButton = new JButton(iconG); 
    //green button 
    greenButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      drawPad.green(); 
     } 
    }); 

    blackButton.setPreferredSize(new Dimension(16, 16)); 
    magentaButton.setPreferredSize(new Dimension(16, 16)); 
    redButton.setPreferredSize(new Dimension(16, 16)); 
    blueButton.setPreferredSize(new Dimension(16, 16)); 
    greenButton.setPreferredSize(new Dimension(16,16)); 
    //sets the sizes of the buttons 

    panel.add(greenButton); 
    panel.add(blueButton); 
    panel.add(magentaButton); 
    panel.add(blackButton); 
    panel.add(redButton); 
    panel.add(clearButton); 
      panel.add(saveButton); 
    //adds the buttons to the panel 

    content.add(panel, BorderLayout.SOUTH); 
    //sets the panel to the left 

    frame.setSize(300, 300); 
    //sets the size of the frame 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //makes it so you can close 
    frame.setVisible(true); 
    //makes it so you can see it 
} 
} 


class PadDraw extends JComponent{ 
Image image; 
//this is gonna be your image that you draw on 
Graphics2D graphics2D; 
//this is what we'll be using to draw on 
int currentX, currentY, oldX, oldY; 
//these are gonna hold our mouse coordinates 

//Now for the constructors 
public PadDraw(){ 
    setDoubleBuffered(false); 
    addMouseListener(new MouseAdapter(){ 
     public void mousePressed(MouseEvent e){ 
      oldX = e.getX(); 
      oldY = e.getY(); 
     } 
    }); 
    //if the mouse is pressed it sets the oldX & oldY 
    //coordinates as the mouses x & y coordinates 
    addMouseMotionListener(new MouseMotionAdapter(){ 
     public void mouseDragged(MouseEvent e){ 
      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(); 
} 
//this is the clear 
//it sets the colors as white 
//then it fills the window with white 
//thin it sets the color back to black 
public void red(){ 
    graphics2D.setPaint(Color.red); 
    repaint(); 
} 
//this is the red paint 
public void black(){ 
    graphics2D.setPaint(Color.black); 
    repaint(); 
} 
//black paint 
public void magenta(){ 
    graphics2D.setPaint(Color.magenta); 
    repaint(); 
} 
//magenta paint 
public void blue(){ 
    graphics2D.setPaint(Color.blue); 
    repaint(); 
} 
//blue paint 
public void green(){ 
    graphics2D.setPaint(Color.green); 
    repaint(); 
} 
//green paint 

}

+0

這是一種奇怪的方式(可能不是最好的),但可以使用['Robot'](https://docs.oracle.com/javase/7/docs/) api/java/awt/Robot.html)截取窗口的截圖。 – CodingNinja

+0

在圖像上繪製線條,使用'ImageIO'保存圖像。 –

+0

您在'Icon iconB = new ImageIcon(「blue.gif」)後遺忘了分號。 – CodingNinja

回答

1

創建任何Swing組件的圖像的基礎是使用:

BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB); 
Graphics2D g2d = image.createGraphics(); 
component.print(g2d); 
g2d.dispose(); 

然後你可以保存 「圖像」 使用ImageIO

或者您可以使用具有額外的功能到Screen Image

  1. 手柄透明組件
  2. 創建組件
  3. 的區域的圖像創建一個不顯示部件的圖像可見窗口
  4. 將圖像寫入文件
3

我寫了save()方法將其複製到PadDraw類並在需要保存圖像時調用save()。

public void save(){ 
    try { 
     BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); 

     // Draw the image on to the buffered image 
     Graphics2D bGr = bimage.createGraphics(); 
     bGr.drawImage(image, 0, 0, null); 
     javax.imageio.ImageIO.write(bimage , "PNG", new File("test.png")); 
     bGr.dispose(); 
    } catch (Exception ex) { 
     Logger.getLogger(PadDraw.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
+0

感謝Piyush。它正在工作。但似乎每當我保存一張圖片時,它就會替換第一張圖片,因爲它的名稱爲'test.png'。有沒有解決方法? – Sajib

+0

您必須將「test.png」替換爲圖片的路徑:) –