图片添加移动,我写好了,不过不知道怎样保存窗体文件,哪位好心人帮我完善一下好吗?
import java.awt.*;
import java.awt.event.*;
public class yidong1 extends Frame implements ActionListener,MouseMotionListener,MouseListener
{
static yidong1 frm=new yidong1();
PrintJob p=null;
Graphics g=null;
int x=10,y=50,posX=10,posY=50,dx,dy;
static Image img;
static MenuBar mb=new MenuBar();
static Menu menu1=new Menu("文件(F)");
static Menu menu2=new Menu("添加图片(M)");
static MenuItem mi1=new MenuItem("新建");
static MenuItem mi2=new MenuItem("打开");
static MenuItem mi3=new MenuItem("保存");
static MenuItem mi4=new MenuItem("打印");
static MenuItem mi5=new MenuItem("关闭");
static Menu menu4=new Menu("图片");
static MenuItem mi6=new MenuItem("1");
static boolean clicked6=false,clicked7=false;
public static void main(String args[])
{
mb.add(menu1);
mb.add(menu2);
menu1.add(mi1);
menu1.add(mi2);
menu1.add(mi3);
menu1.add(mi4);
menu1.add(mi5);
menu2.add(menu4);
menu4.add(mi6);
mi1.addActionListener(frm);
mi2.addActionListener(frm);
mi3.addActionListener(frm);
mi4.addActionListener(frm);
mi5.addActionListener(frm);
mi6.addActionListener(frm);
Toolkit tool=frm.getToolkit();
img=tool.getImage("1.jpg");
frm.setTitle("测试");
frm.setSize(800,600);
frm.addMouseListener(frm);
frm.addMouseMotionListener(frm);
frm.setMenuBar(mb);
frm.setVisible(true);
frm.addWindowListener(new CloseWin());
}
public void actionPerformed(ActionEvent e)
{
MenuItem mi=(MenuItem) e.getSource();
if(mi==mi1)
{ g=null;}
else if(mi==mi2)
{}
else if(mi==mi3)
{}
else if(mi==mi4)
{ p=getToolkit().getPrintJob(this,"ok",null);
g=p.getGraphics();
this.printAll(g);
g.dispose();
p.end(); }
else if(mi==mi5)
{System.exit(0);}
else if(mi==mi6)
{
clicked6=true;
Graphics g=getGraphics();
print(g);
}
}
public void mousePressed(MouseEvent e)
{
dx=e.getX()-posX; // 取得鼠标按下之点与基准点X方向的距离
dy=e.getY()-posY; // 取得鼠标按下之点与基准点Y方向的距离
}
public void mouseDragged(MouseEvent e)
{
if(dx>0 && dx<img.getWidth(this) && dy>0 && dy<img.getHeight(this)) // 如果指针落在正方形区域内
{
x=e.getX()-dx; // 取得拖拽时的基准电X坐标
y=e.getY()-dy; // 取得拖拽时的基准电Y坐标
Graphics g=getGraphics();
update(g);
}
}
public void paint(Graphics g)
{ if(clicked6)
{
g.drawImage(img,x,y,img.getWidth(this),img.getHeight(this),this);
posX=x; // 更新基准点的X坐标
posY=y; // 更新基准点的Y坐标
}
}
public void mouseMoved(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
if(e.getButton() == MouseEvent.BUTTON3)
{
if(dx>0 && dx<img.getWidth(this) && dy>0 && dy<img.getHeight(this))
{clicked6=false;
Graphics g=getGraphics();
update(g);}
}
}
}
class CloseWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Frame frm=(Frame)(e.getSource());
frm.dispose();
System.exit(0);
}
}