import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* ImageTest.java
*
*/
/**
* Test frame
*
* @author sergey
*/
public class ImageTest extends JFrame{
/** Creates a new instance of ImageTest */
public ImageTest() {
Container contentPane = getContentPane();
setSize(300, 300);
contentPane.add(new ImagePane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new ImageTest().setVisible(true);
}
}
class ImagePane extends JPanel {
private int iw, ih;
private BufferedImage bi;
private Graphics2D big;
private File f;
protected String path = "c:" + File.separator + "test.jpeg";
public boolean isOpaque() {
return true; // draw background!!!
}
protected void paintComponent(java.awt.Graphics g) {
Graphics2D g2 = (Graphics2D)g;
drawYourPicHere(); // Dibujamos el cuadro
g2.drawImage(bi, null, 0, 0); // Dibujamos el cuadro sobre la pantalla
saveImage(path); // Conservamos el cuadro
}
/**
* Draw image in Background
*/
protected void drawYourPicHere() {
iw = getWidth();
ih = getHeight();
bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
big = bi.createGraphics();
big.setColor(Color.BLACK);
big.fillRect(0, 0, iw, ih);
Font f = new Font("Serif", Font.PLAIN, 72);
big.setFont(f);
big.setColor(Color.WHITE);
big.drawString("test", 75, 100);
}
public void saveImage(String fileName) {
try {
f = new File(fileName);
ImageIO.write(bi, "JPEG", f);
}catch (Exception e) {
System.out.println("Exception in -> ImagePanel.saveImage " );
e.printStackTrace();
}
}
}