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的成员变量
NewCanvas newCanvas;
public void init()
{
picture=Toolkit.getDefaultToolkit().getImage("lan.gif"
//装载图像
newCanvas= new NewCanvas(picture,this,"172.16.16.43"
newCanvas.ix=20;
newCanvas.iy=30;
newCanvas.sx=20;
newCanvas.sy=20;
newCanvas.setVisible(true);
}
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.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.*;
public class NewCanvas extends Canvas{
ShowImage parent;
Image img=null;
int ix=0;
int iy=2;
int sx=0;
int sy=0;
boolean mouse=false;
String ipAddr;
Dimension dim;
public NewCanvas(Image i, ShowImage p, String ip)
{
super();
img = i;
parent = p;
this.ipAddr = ip;
}
void SetImage(Image i)
{
img = i;
}
void SetImagexy(int x, int y)
{
ix = x;
iy = y;
}
void SetStringxy(int x, int y)
{
sx = x;
sy = y;
}
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);
}
if ( mouse )
{
dim = size();
g.setColor(Color.blue);
g.drawRect(0,0,dim.width-1,dim.height-1);
}
}
public boolean mouseEnter(Event evt, int x, int y)
{
mouse = true;
repaint();
return true;
}
public boolean mouseExit(Event evt, int x, int y)
{
mouse = false;
repaint();
return true;
}
public boolean mouseDown(Event evt, int x, int y)
{
Frame helpDialog=new Frame() ;
helpDialog.setTitle("this is a dial"
helpDialog.show();
return true;
}
}
//源程序清单ShowImage.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ShowImage extends Applet{
Image picture;
//定义类型为Image的成员变量
NewCanvas newCanvas;
public void init()
{
picture=Toolkit.getDefaultToolkit().getImage("lan.gif"
//装载图像
newCanvas= new NewCanvas(picture,this,"172.16.16.43"
newCanvas.ix=20;
newCanvas.iy=30;
newCanvas.sx=20;
newCanvas.sy=20;
newCanvas.setVisible(true);
}
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.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.*;
public class NewCanvas extends Canvas{
ShowImage parent;
Image img=null;
int ix=0;
int iy=2;
int sx=0;
int sy=0;
boolean mouse=false;
String ipAddr;
Dimension dim;
public NewCanvas(Image i, ShowImage p, String ip)
{
super();
img = i;
parent = p;
this.ipAddr = ip;
}
void SetImage(Image i)
{
img = i;
}
void SetImagexy(int x, int y)
{
ix = x;
iy = y;
}
void SetStringxy(int x, int y)
{
sx = x;
sy = y;
}
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);
}
if ( mouse )
{
dim = size();
g.setColor(Color.blue);
g.drawRect(0,0,dim.width-1,dim.height-1);
}
}
public boolean mouseEnter(Event evt, int x, int y)
{
mouse = true;
repaint();
return true;
}
public boolean mouseExit(Event evt, int x, int y)
{
mouse = false;
repaint();
return true;
}
public boolean mouseDown(Event evt, int x, int y)
{
Frame helpDialog=new Frame() ;
helpDialog.setTitle("this is a dial"
helpDialog.show();
return true;
}
}