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 &&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("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 &&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) {}
}