Topic: 图片添加问题??

  Print this page

1.图片添加问题?? Copy to clipboard
Posted by: jsxww2002
Posted on: 2005-05-20 16:43

请问如何在应用程序里添加一个可以被鼠标拖动的图片?

2.Re:图片添加问题?? [Re: jsxww2002] Copy to clipboard
Posted by: 8166680
Posted on: 2005-05-23 02:45

我也想知道唉

3.Re:图片添加问题?? [Re: jsxww2002] Copy to clipboard
Posted by: jsxww2002
Posted on: 2005-05-23 09:14

图片添加移动,我写好了,不过不知道怎样保存窗体文件,哪位好心人帮我完善一下好吗?

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);
}
}

4.Re:图片添加问题?? [Re: jsxww2002] Copy to clipboard
Posted by: jsxww2002
Posted on: 2005-05-29 11:27

还是没人会,那我自己顶~~~~~~
那有人知道画图板的打开和保存菜单怎么写吗?我的应该跟这个差不多,如果有人有代码清不要吝啬哦。。。。。。

5.Re:图片添加问题?? [Re: jsxww2002] Copy to clipboard
Posted by: jsxww2002
Posted on: 2005-06-06 15:01

自己再‘顶“一次

6.Re:图片添加问题?? [Re: jsxww2002] Copy to clipboard
Posted by: yinpeng263
Posted on: 2005-06-08 03:39

Examples for menu

http://www.java2s.com/ExampleCode/Swing-JFC/Menu.htm


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923