| Код | import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.awt.geom.*; import com.sun.image.codec.jpeg.*; import java.io.*;
/* A simple demonstration how to store an image in JPEG format. Note that the package com.sun.image.codec.jpeg.* is needed. */ public class ImageSavingExample extends Frame {
private BufferedImage theImage;
public ImageSavingExample() { addWindowListener(new MyFinishWindow());
//initialise the (buffered) image theImage = new BufferedImage(200,300,BufferedImage.TYPE_INT_RGB);
//get its Graphics2D object Graphics2D g2dImage = theImage.createGraphics();
//paint something on the buffered image clearWindow(g2dImage); g2dImage.setPaint(Color.blue); g2dImage.draw(new Rectangle2D.Double(10,10,150,250)); g2dImage.setPaint(Color.green); g2dImage.drawString("Test",30,230);
try { //create a file output stream FileOutputStream fos = new FileOutputStream("test.jpg"); //create an JPEGImageEncoder using the output stream JPEGImageEncoder jie = JPEGCodec.createJPEGEncoder(fos); //let the JPEGImageEncoder write the image to the file/output stream jie.encode(theImage); } catch (Exception e) { System.out.println(e); }
}
//This not necessary. It is just to show that the stored image is the //same one is the stored one. public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(theImage,50,50,null);
}
public static void clearWindow(Graphics2D g) { g.setPaint(Color.white); g.fill(new Rectangle(0,0,600,600)); g.setPaint(Color.black); }
}
|
| Код | import java.awt.*; import java.awt.event.*;
class MyFinishWindow extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } }
|
Держи надеюс помогежет |