You are viewing our Forum Archives. To view or take place in current topics click here.
java save panel to png image
Posted:
java save panel to png imagePosted:
Status: Offline
Joined: Dec 23, 200915Year Member
Posts: 1,939
Reputation Power: 377
Status: Offline
Joined: Dec 23, 200915Year Member
Posts: 1,939
Reputation Power: 377
I currently have my paint program and its working but i cant get the save feature working.
Currently it saves the blank panel and none of the lines drawn.
current code is under " private void save()"
thanks!
Currently it saves the blank panel and none of the lines drawn.
current code is under " private void save()"
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package my.FreehandDraw;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import java.nio.file.Path;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* @author 10106320
*/
public class MainFrame extends javax.swing.JFrame {
private int red = 0;
private int blue = 0;
private int green = 0;
public MainFrame() {
initComponents();
updateColours();
updateSliders();
}
private void updateSliders()
{
SliderRed.setValue(red);
SliderGreen.setValue(green);
SliderBlue.setValue(blue);
}
private BasicStroke GetStroke()
{
return new BasicStroke(jSlider2.getValue());
}
/**
* Creates new form MainFrame
*/
private Point2D startPoint, endPoint;
private Line2D lineBuffer;
private Graphics2D g2, g3;
//each freehand line is made up of a list of lines of one point in length
private List<Line2D> freeLinePoints;
//list of drawn objects
private List<DrawItem> drawItems = new ArrayList();
private void save()
{
Dimension size = pnlOutputt.getSize();
BufferedImage image = new BufferedImage(
size.width, size.height
, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
pnlOutputt.paint(g2);
pnlOutputt.getGraphics();
drawItems.getClass();
g2.getClip();
try
{
ImageIO.write(image, "png", new File("snapshot.png"));
System.out.println("Panel saved as Image.");
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
pnlOutputt = new javax.swing.JPanel();
btnClear = new javax.swing.JButton();
jSlider2 = new javax.swing.JSlider();
SliderBlue = new javax.swing.JSlider();
SliderGreen = new javax.swing.JSlider();
Panel2 = new javax.swing.JPanel();
pnlColour = new javax.swing.JPanel();
SliderRed = new javax.swing.JSlider();
btnUndo1 = new javax.swing.JButton();
btnSave = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
pnlOutputt.setBackground(new java.awt.Color(255, 255, 255));
pnlOutputt.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
pnlOutputtMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
pnlOutputtMouseReleased(evt);
}
});
pnlOutputt.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
pnlOutputtMouseDragged(evt);
}
});
javax.swing.GroupLayout pnlOutputtLayout = new javax.swing.GroupLayout(pnlOutputt);
pnlOutputt.setLayout(pnlOutputtLayout);
pnlOutputtLayout.setHorizontalGroup(
pnlOutputtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
pnlOutputtLayout.setVerticalGroup(
pnlOutputtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 244, Short.MAX_VALUE)
);
btnClear.setText("Clear");
btnClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
jSlider2.setMaximum(10);
jSlider2.setValue(0);
jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider2StateChanged(evt);
}
});
SliderBlue.setMaximum(255);
SliderBlue.setValue(0);
SliderBlue.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
SliderBlueStateChanged(evt);
}
});
SliderGreen.setMaximum(255);
SliderGreen.setValue(0);
SliderGreen.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
SliderGreenStateChanged(evt);
}
});
Panel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
Panel2.setForeground(new java.awt.Color(0, 51, 153));
javax.swing.GroupLayout Panel2Layout = new javax.swing.GroupLayout(Panel2);
Panel2.setLayout(Panel2Layout);
Panel2Layout.setHorizontalGroup(
Panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 179, Short.MAX_VALUE)
);
Panel2Layout.setVerticalGroup(
Panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
pnlColour.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
pnlColour.setForeground(new java.awt.Color(0, 51, 153));
javax.swing.GroupLayout pnlColourLayout = new javax.swing.GroupLayout(pnlColour);
pnlColour.setLayout(pnlColourLayout);
pnlColourLayout.setHorizontalGroup(
pnlColourLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 123, Short.MAX_VALUE)
);
pnlColourLayout.setVerticalGroup(
pnlColourLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
SliderRed.setMaximum(255);
SliderRed.setValue(0);
SliderRed.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
SliderRedStateChanged(evt);
}
});
btnUndo1.setText("Undo");
btnUndo1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUndo1ActionPerformed(evt);
}
});
btnSave.setText("Save Image");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(pnlOutputt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 38, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(SliderBlue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SliderGreen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SliderRed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(195, 195, 195))
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addComponent(pnlColour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnUndo1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnClear))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(Panel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(53, 53, 53)))))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(54, 54, 54))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(pnlOutputt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnUndo1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(pnlColour, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
.addGap(0, 0, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SliderRed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(SliderBlue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(4, 4, 4)
.addComponent(SliderGreen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(Panel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void pnlOutputtMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlOutputtMousePressed
if (g2 == null){
g2 = (Graphics2D) pnlOutputt.getGraphics();
}
g2.setColor(GetColor());
g2.setStroke(GetStroke());
//get start point
startPoint = evt.getPoint();
//create new freeline object
freeLinePoints = new ArrayList();
}//GEN-LAST:event_pnlOutputtMousePressed
private void pnlOutputtMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlOutputtMouseDragged
if (g2 == null){
g2 = (Graphics2D) pnlOutputt.getGraphics();
}
//get endpoint
endPoint = evt.getPoint();
//draw the line
lineBuffer = new Line2D.Float(startPoint, endPoint);//, (float) xEnd, (float) yEnd);
g2.setColor(GetColor());
g2.setStroke(GetStroke());
g2.draw(lineBuffer);
//add line to buffer
freeLinePoints.add(lineBuffer);
//set new start point to be end point
startPoint = evt.getPoint();
}//GEN-LAST:event_pnlOutputtMouseDragged
private void pnlOutputtMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlOutputtMouseReleased
//create new draw item object passing in list of lines
DrawItem di = new DrawItem(freeLinePoints, GetColor());
drawItems.add(di);
}//GEN-LAST:event_pnlOutputtMouseReleased
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
int dialogButton;
dialogButton = JOptionPane.showConfirmDialog(null, "Are you sure you want to clear");
if(dialogButton == JOptionPane.YES_OPTION) {
if (g2 == null){
g2 = (Graphics2D) pnlOutputt.getGraphics();
}
//clear panel
pnlOutputt.update(g2);
//as long as objects are not referenced then the garbage collector will clear them up from memory
drawItems.clear();
}
if(dialogButton == JOptionPane.NO_OPTION) {
}
}//GEN-LAST:event_btnClearActionPerformed
private void btnUndo1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUndo1ActionPerformed
// TODO add your handling code here:
if (g2 == null){
g2 = (Graphics2D) pnlOutputt.getGraphics();
}
//clear panel
pnlOutputt.update(g2);
//remove last drawn object
if (drawItems.isEmpty() == false)
{
drawItems.remove(drawItems.size()-1);
}
//draw items
//for each complete freehand line
for (int i = 0; i < drawItems.size(); i++)
{
g2.setColor(drawItems.get(i).color);
// for each individual line in the free hand line
for (int n = 0; n < drawItems.get(i).freeLine.size(); n++)
{
//draw it
g2.draw((Line2D)drawItems.get(i).freeLine.get(n));
}
}
}
private void DrawAll()
{
}//GEN-LAST:event_btnUndo1ActionPerformed
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
save();
}//GEN-LAST:event_btnSaveActionPerformed
private void SliderBlueStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SliderBlueStateChanged
// TODO add your handling code here:
blue = SliderBlue.getValue();
updateColours();
}//GEN-LAST:event_SliderBlueStateChanged
private void SliderGreenStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SliderGreenStateChanged
// TODO add your handling code here:
green = SliderGreen.getValue();
updateColours();
}//GEN-LAST:event_SliderGreenStateChanged
private void SliderRedStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SliderRedStateChanged
// TODO add your handling code here:
red = SliderRed.getValue();
updateColours();
}//GEN-LAST:event_SliderRedStateChanged
private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
//Graphics2D g3;
if (g3 == null){
g3 = (Graphics2D) Panel2.getGraphics();
}
Point2D start = new Point2D.Float(0, Panel2.getHeight()/2);
Point2D end = new Point2D.Float(Panel2.getWidth(), Panel2.getHeight()/2);
//, end;
lineBuffer = new Line2D.Float(start, end);
g3.draw(lineBuffer);
}//GEN-LAST:event_formWindowOpened
private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider2StateChanged
if (g3 == null){
g3 = (Graphics2D) Panel2.getGraphics();
}
pnlOutputt.update(g3);
Point2D start = new Point2D.Float(0, Panel2.getHeight()/2);
Point2D end = new Point2D.Float(Panel2.getWidth(), Panel2.getHeight()/2);
//, end;
lineBuffer = new Line2D.Float(start, end);
g3.setStroke(new BasicStroke(jSlider2.getValue()));
g3.setColor(GetColor());
g3.draw(lineBuffer);
//g3.setBackground(new Color(240, 240, 240));
}//GEN-LAST:event_jSlider2StateChanged
private void updateColours()
{
Color bg = new Color(red, green, blue);
pnlColour.setBackground(bg);
}
private Color GetColor()
{
return new Color(red, green, blue);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel Panel2;
private javax.swing.JSlider SliderBlue;
private javax.swing.JSlider SliderGreen;
private javax.swing.JSlider SliderRed;
private javax.swing.JButton btnClear;
private javax.swing.JButton btnSave;
private javax.swing.JButton btnUndo1;
private javax.swing.JSlider jSlider2;
private javax.swing.JPanel pnlColour;
private javax.swing.JPanel pnlOutputt;
// End of variables declaration//GEN-END:variables
}
thanks!
You are viewing our Forum Archives. To view or take place in current topics click here.