图象移动问题,请帮忙看看源代码! ( 积分: 200 )

  • 主题发起人 主题发起人 gxhu
  • 开始时间 开始时间
G

gxhu

Unregistered / Unconfirmed
GUEST, unregistred user!
//我想拖动图象在另一个位置重画,需要切换窗口才能见到效果。有没有办法不用切换窗口?
//源程序清单ShowImage.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ShowImage extends Applet{
Image picture;
//定义类型为Image的成员变量
static NewCanvas newCanvas;
public void init()
{
picture=Toolkit.getDefaultToolkit().getImage("lan.gif");
//装载图像
newCanvas= new NewCanvas(picture,this,"172.16.16.43");
newCanvas.ix=20;//图象x坐标
newCanvas.iy=30;//图象y坐标
newCanvas.sx=20;//字符串x坐标
newCanvas.sy=20;//字符串y坐标
}
public void paint(Graphics g){
newCanvas.paint(g);
}
public static void main(String args[]){
Frame frame=new Frame("This is a test");
frame.setSize(800,600);

Applet ap=new ShowImage();
ap.init();
frame.add(ap);
ap.addMouseListener(newCanvas);
ap.addMouseMotionListener(newCanvas);
ap.start();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
//源程序清单NewCanvas.java
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseMotionListener;
import java.util.EventListener;
import java.awt.event.MouseListener;

public class NewCanvas extends Canvas implements MouseMotionListener, MouseListener {
Object parent;
Image img=null;//图象
int ix=0;//图象x坐标
int iy=2;//图象y坐标
int sx=0;//字符串x坐标
int sy=0;//字符串y坐标

boolean mouse=false;
String ipAddr;
Dimension dim;

public NewCanvas(Image i, ShowImage p, String ip)
{
super();
img = i;
parent = p;
this.ipAddr = ip;
}

public void paint(Graphics g)
{
if ( img != null ) g.drawImage(img,ix,iy,40,40,Color.white,this);
if ( ipAddr != null )
{
g.drawString(ipAddr,sx,sy);
}
}
public void mouseDragged(MouseEvent e) {
if(e.getX()>ix &&
e.getX()<ix+40)
if(e.getY()>iy &amp;&amp;e.getY()<iy+40){
mouse=true;
}
}

public void mousePressed(MouseEvent e) {
if(mouse){
ix=e.getX();
iy=e.getY();
sx=ix;
sy=iy-10;
repaint();
}
}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}
}
 
//我想拖动图象在另一个位置重画,需要切换窗口才能见到效果。有没有办法不用切换窗口?
//源程序清单ShowImage.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ShowImage extends Applet{
Image picture;
//定义类型为Image的成员变量
static NewCanvas newCanvas;
public void init()
{
picture=Toolkit.getDefaultToolkit().getImage(&quot;lan.gif&quot;);
//装载图像
newCanvas= new NewCanvas(picture,this,&quot;172.16.16.43&quot;);
newCanvas.ix=20;//图象x坐标
newCanvas.iy=30;//图象y坐标
newCanvas.sx=20;//字符串x坐标
newCanvas.sy=20;//字符串y坐标
}
public void paint(Graphics g){
newCanvas.paint(g);
}
public static void main(String args[]){
Frame frame=new Frame(&quot;This is a test&quot;);
frame.setSize(800,600);

Applet ap=new ShowImage();
ap.init();
frame.add(ap);
ap.addMouseListener(newCanvas);
ap.addMouseMotionListener(newCanvas);
ap.start();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
//源程序清单NewCanvas.java
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseMotionListener;
import java.util.EventListener;
import java.awt.event.MouseListener;

public class NewCanvas extends Canvas implements MouseMotionListener, MouseListener {
Object parent;
Image img=null;//图象
int ix=0;//图象x坐标
int iy=2;//图象y坐标
int sx=0;//字符串x坐标
int sy=0;//字符串y坐标

boolean mouse=false;
String ipAddr;
Dimension dim;

public NewCanvas(Image i, ShowImage p, String ip)
{
super();
img = i;
parent = p;
this.ipAddr = ip;
}

public void paint(Graphics g)
{
if ( img != null ) g.drawImage(img,ix,iy,40,40,Color.white,this);
if ( ipAddr != null )
{
g.drawString(ipAddr,sx,sy);
}
}
public void mouseDragged(MouseEvent e) {
if(e.getX()>ix &amp;&amp;
e.getX()<ix+40)
if(e.getY()>iy &amp;&amp;e.getY()<iy+40){
mouse=true;
}
}

public void mousePressed(MouseEvent e) {
if(mouse){
ix=e.getX();
iy=e.getY();
sx=ix;
sy=iy-10;
repaint();
}
}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}
}
 
在窗口间的移动好像要用到剪贴板 ! 好久没有用过这些了 !你看看 !
 
好象您误解我的意思了。
我的想法是拖动图象在某个位置,就在该位置立即显示该图象。可是我上面的程序有问题,它非得要我切换一下窗口(相当与隐藏)才能看到这个效果。请帮我看看错误在哪?
 
程序没看,是不是要自己在拖动完成之后触发一下窗体的ONPAINT?
 
解决问题了。一本好书帮我最终解决了问题。当然也感谢各位朋友的引导和帮助!!!
现在把这本书告诉大家:《最新java 2核心技术》
 
多人接受答案了。
 
后退
顶部