package jsynoptic.ui;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import simtools.diagram.DiagramComponent;
public class ImageViewer extends JDialog implements ActionListener, ImageObserver{
/**
* tools bar buttons
*/
protected JButton bSave;
/**
* image panel
*/
protected Color color;
protected String imageName;
protected ImageIcon imageIcon;
protected BufferedImage image;
/**
* Used to save bufferedImage into a file
*/
protected PrintToImage saveImage;
public ImageViewer(Frame parent, String title) {
super(parent, title, false);
Container content = getContentPane();
content.setLayout(new BorderLayout());
// Create image panel
imageIcon = new ImageIcon();
JScrollPane scrollPane = new JScrollPane(new JLabel(imageIcon));
content.add(scrollPane, BorderLayout.CENTER);
// Create tools bar
JToolBar toolsBar = new JToolBar();
toolsBar.add(bSave = JSynopticPanels.resources.getBox("save", this));
content.add(toolsBar, BorderLayout.NORTH);
saveImage = new PrintToImage();
}
public void setImage(DiagramComponent d , Color color){
if (image!=null)
image.flush();
this.imageName = d.getName();
this.color = color;
// Create image buffer
Rectangle r=d.getBounds();
r.width=(int)((double)r.width/d.getZoom());
r.height=(int)((double)r.height/d.getZoom());
image =new BufferedImage(r.width,r.height,BufferedImage.TYPE_3BYTE_BGR);
if (image!=null){
Graphics2D g2= image.createGraphics();
if (color!=null)
g2.setColor(color);
else
g2.setColor(Color.WHITE);
g2.fillRect(0,0,r.width,r.height);
d.printDiagram(g2);
}
imageIcon.setImage(image);
}
public void actionPerformed(ActionEvent e){
if ((e.getSource() == bSave)) {
try{
saveImage.print(imageName, image, color);
}catch (IOException io){
}
}
}
}