import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;import javax.imageio.ImageIO;
import javax.swing.*;
public class HisFrame { /**
* @param args
*/
static BufferedImage image1=null;
public static void main(String[] args) {
final Frame f=new Frame("wo de cai dan");
f.setSize(600,400);
f.setLocation(100,100);
final ImagePanel jpi= new ImagePanel();
jpi.filename=null;
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
MenuBar mb=new MenuBar();
Menu m1=new Menu("file");
MenuItem m11=new MenuItem("open");
m11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(f,"open window",FileDialog.LOAD);
fd.setVisible(true);
String file=fd.getDirectory()+fd.getFile();
System.out.println(file);
if(fd.getFile()!=null){
jpi.filename=file;
}
}
});
m1.add(m11);
mb.add(m1);
f.add(jpi,"Center");
f.setMenuBar(mb);
f.setVisible(true);
}
}
class ImagePanel extends JPanel
{
String filename;
public void paint(Graphics g)
{
String name = filename;
if(name!=null){
BufferedImage img = null;
try{img=ImageIO.read(new File(name));}catch(Exception e){}
g.drawImage(img, 10, 10, null);}
this.updateUI();
}
}
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;import javax.imageio.ImageIO;
import javax.swing.*;
public class HisFrame { /**
* @param args
*/
static BufferedImage image1=null;
public static void main(String[] args) {
final Frame f=new Frame("wo de cai dan");
f.setSize(600,400);
f.setLocation(100,100);
final ImagePanel jpi= new ImagePanel();
jpi.filename=null;
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
MenuBar mb=new MenuBar();
Menu m1=new Menu("file");
MenuItem m11=new MenuItem("open");
m11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(f,"open window",FileDialog.LOAD);
fd.setVisible(true);
String file=fd.getDirectory()+fd.getFile();
System.out.println(file);
if(fd.getFile()!=null){
jpi.filename=file;
}
}
});
m1.add(m11);
mb.add(m1);
f.add(jpi,"Center");
f.setMenuBar(mb);
f.setVisible(true);
}
}
class ImagePanel extends JPanel
{
String filename;
public void paint(Graphics g)
{
String name = filename;
if(name!=null){
BufferedImage img = null;
try{img=ImageIO.read(new File(name));}catch(Exception e){}
g.drawImage(img, 10, 10, null);}
this.updateUI();
}
}
是不是updateUI用的不对啊 ?
在问个问题 能不能获得一个JPanel对象 让它保存成一个文件
也就是上面显示的图片能另存
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(ima);
out.close();
这样图片就能另存了
我的意思是存JPanel
我可能在JPanel中在不同位置放几张图片
也可能在图片中画几笔
然后再另存 。