help with saving a JPanel to jpg  
Author Message
info





PostPosted: 2005-12-13 10:42:00 Top

java-programmer, help with saving a JPanel to jpg I have a JPanel that I paint with a graph. The upper 40% is a graph
with a grid background, and the rest is a legend for the graph. When I
try to save the JPanel as a jpg, I get an image with the same
dimensions but there is no graph or legend and the grid, which uses the
dimensions to set the upper grid limit, covers the entire panel.
Everything on screen is fine. The current code is below. I've tried
the available varieties of paint and tried using the Robot class
(though this could be explored more). Any tips on where I'm messing up
or what could be changed to get this working would be appreciated.

java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(
panel.getPreferredSize().width,
panel.getPreferredSize().height,
java.awt.image.BufferedImage.TYPE_INT_RGB);
java.awt.Graphics2D g = image.createGraphics();
g.setRenderingHint(java.awt.RenderingHints.KEY_FRACTIONALMETRICS,
java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g.setRenderingHint(java.awt.RenderingHints.KEY_TEXT_ANTIALIASING,
java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
panel.paintAll(g);
g.dispose();
javax.imageio.ImageIO.write(image, "jpeg",
new java.io.File("./" + jtf.getText() + ".jpg"));
image.flush();

 
hiwa





PostPosted: 2005-12-13 11:11:00 Top

java-programmer >> help with saving a JPanel to jpg email***@***.com 銇儭銉冦偦銉笺偢:

> I have a JPanel that I paint with a graph. The upper 40% is a graph
> with a grid background, and the rest is a legend for the graph. When I
> try to save the JPanel as a jpg, I get an image with the same
> dimensions but there is no graph or legend and the grid, which uses the
> dimensions to set the upper grid limit, covers the entire panel.
> Everything on screen is fine. The current code is below. I've tried
> the available varieties of paint and tried using the Robot class
> (though this could be explored more). Any tips on where I'm messing up
> or what could be changed to get this working would be appreciated.
>
> java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(
> panel.getPreferredSize().width,
> panel.getPreferredSize().height,
> java.awt.image.BufferedImage.TYPE_INT_RGB);
> java.awt.Graphics2D g = image.createGraphics();
> g.setRenderingHint(java.awt.RenderingHints.KEY_FRACTIONALMETRICS,
> java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_ON);
> g.setRenderingHint(java.awt.RenderingHints.KEY_TEXT_ANTIALIASING,
> java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
> panel.paintAll(g);
> g.dispose();
> javax.imageio.ImageIO.write(image, "jpeg",
> new java.io.File("./" + jtf.getText() + ".jpg"));
> image.flush();

See: http://www.physci.org/codes/sscce.jsp

My 2 cent:
A stab in the dark, among sooooo many ...
If your panel is not visible, painting into a BufferedImage or Image
dosn't work.

 
info





PostPosted: 2005-12-13 11:43:00 Top

java-programmer >> help with saving a JPanel to jpg The panel has setVisible(true). On screen everything is fine. When
getting the image, I get part of what's onscreen and some odd behavior.
The background that covers part of the panel, on screen, covers the
entire saved image.

 
 
hiwa





PostPosted: 2005-12-13 12:01:00 Top

java-programmer >> help with saving a JPanel to jpg Anyway, you must observe http://www.physci.org/codes/sscce.jsp and
post a small demo code that is generally compilable, runnable,
testable and could reproduce your problem.

I believe your bug is lurking in other part of your code than that is
posted. (another stab in the dark, ... among sooooo many of them :
your paintComponent() method is not properly written.)

Try this:
---------------------------------------------------------------------------
/* this works fine */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

public class SaveJPanel{
JFrame frame;
Container con;
TestPanel tp;
JTextField jtf;

public SaveJPanel(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = frame.getContentPane();

tp = new TestPanel();
jtf = new JTextField();

con.add(tp, BorderLayout.CENTER);
con.add(jtf, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);

jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
savePanel(tp);
}
});
}

public void savePanel(JComponent compo){
BufferedImage image = new
BufferedImage(compo.getPreferredSize().width,
compo.getPreferredSize().height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();

g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

compo.paintAll(g);
g.dispose();
try{
ImageIO.write(image, "jpeg", new File("./" + jtf.getText()
+ ".jpg"));
}
catch (IOException ie){
ie.printStackTrace();
}
image.flush();
}

class TestPanel extends JPanel{

public TestPanel(){
setBackground(Color.cyan);
}

public Dimension getPreferredSize(){
return new Dimension(600, 600);
}

public void paintComponent(Graphics g){

super.paintComponent(g);

g.setColor(Color.red);
g.drawLine(100, 100, 500, 500);
g.setFont(new Font("Dialog", Font.BOLD, 38));
g.setColor(new Color(101, 39, 112));
g.drawString("Akatch Paratch", 150, 150);
}
}

public static void main(String[] args){
new SaveJPanel();
}
}
---------------------------------------------------------------------------------------

 
 
info





PostPosted: 2005-12-13 12:03:00 Top

java-programmer >> help with saving a JPanel to jpg The panel has setVisible(true). On screen everything is fine. When
getting the image, I get part of what's onscreen and some odd behavior.
The background that covers part of the panel, on screen, covers the
entire saved image.