G
gxhu
Unregistered / Unconfirmed
GUEST, unregistred user!
//主程序清单ShowImage.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowImage extends JFrame{
static Image picture=null;
//定义类型为Image的成员变量
public ShowImage(){
try{
jbInit();
}catch(Exception e){
e.printStackTrace();
}
}
public void jbInit() throws Exception{
setSize(400,300);
setTitle("Loaded image"
picture=Toolkit.getDefaultToolkit().getImage("lan.gif"
//装载图像
MediaTracker mt=new MediaTracker(this);
mt.addImage(picture,0);
try{
mt.waitForID(0);
}catch(InterruptedException e){ }
}
public static void main(String args[]){
ShowImage frame= new ShowImage();
ImagePanel imagePanel= new ImagePanel(picture,"172.16.16.43"
frame.getContentPane().add(imagePanel);
//imagePanel.addMouseListener(imagePanel);
imagePanel.addMouseMotionListener(imagePanel);
frame.show();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
//程序清单ImagePanel.java文件
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseMotionListener;
class MyImage{
Image picture=null;
//图象
String ipAdd=null;
//图象描述信息
int ix=20, iy=20;
//图象显示的坐标x,y
int sx=20, sy=iy-10;
//图象描述信息显示的坐标x,y
public MyImage(Image i,String s,int ImageX,int ImageY){
picture=i;
ipAdd=s;
ix=ImageX;
sx=ix;
iy=ImageY;
sy=iy-10;
}
}
public class ImagePanel extends JPanel implements MouseMotionListener{
private MyImage [] img=new MyImage[3];
public ImagePanel(Image i,String s){
img[0]=new MyImage(i,s+"
"+1,20,20);
img[1]=new MyImage(i,s+"
"+2,70,70);
img[2]=new MyImage(i,s+"
"+3,130,120);
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g2);
// 画所有图象
for (int i = 0;
i < img.length;
i++){
g2.drawImage(img.picture,img.ix,img.iy,40,40,this);
g2.drawString(img.ipAdd,img.sx,img.sy);
}
// 图象间连线
g2.drawLine(img[0].ix,img[0].iy,img[1].ix,img[1].iy);
g2.drawLine(img[2].ix,img[2].iy,img[1].ix,img[1].iy);
}
public void mouseDragged(MouseEvent e) {
int x,y;
x=e.getX();
y=e.getY();
//判断鼠标释放点坐标是否在图象区域
for(int i=0;i<img.length;i++){
if(x>=img.ix&&x<=img.ix+40){
if(y>=img.iy&&y<=img.iy+40){
img.ix=x;
img.iy=y;
img.sx=x;
img.sy=y-10;
repaint();
}
}
}
}
public void mouseMoved(MouseEvent e) {
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowImage extends JFrame{
static Image picture=null;
//定义类型为Image的成员变量
public ShowImage(){
try{
jbInit();
}catch(Exception e){
e.printStackTrace();
}
}
public void jbInit() throws Exception{
setSize(400,300);
setTitle("Loaded image"
picture=Toolkit.getDefaultToolkit().getImage("lan.gif"
//装载图像
MediaTracker mt=new MediaTracker(this);
mt.addImage(picture,0);
try{
mt.waitForID(0);
}catch(InterruptedException e){ }
}
public static void main(String args[]){
ShowImage frame= new ShowImage();
ImagePanel imagePanel= new ImagePanel(picture,"172.16.16.43"
frame.getContentPane().add(imagePanel);
//imagePanel.addMouseListener(imagePanel);
imagePanel.addMouseMotionListener(imagePanel);
frame.show();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
//程序清单ImagePanel.java文件
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseMotionListener;
class MyImage{
Image picture=null;
//图象
String ipAdd=null;
//图象描述信息
int ix=20, iy=20;
//图象显示的坐标x,y
int sx=20, sy=iy-10;
//图象描述信息显示的坐标x,y
public MyImage(Image i,String s,int ImageX,int ImageY){
picture=i;
ipAdd=s;
ix=ImageX;
sx=ix;
iy=ImageY;
sy=iy-10;
}
}
public class ImagePanel extends JPanel implements MouseMotionListener{
private MyImage [] img=new MyImage[3];
public ImagePanel(Image i,String s){
img[0]=new MyImage(i,s+"
"+1,20,20);
img[1]=new MyImage(i,s+"
"+2,70,70);
img[2]=new MyImage(i,s+"
"+3,130,120);
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g2);
// 画所有图象
for (int i = 0;
i < img.length;
i++){
g2.drawImage(img.picture,img.ix,img.iy,40,40,this);
g2.drawString(img.ipAdd,img.sx,img.sy);
}
// 图象间连线
g2.drawLine(img[0].ix,img[0].iy,img[1].ix,img[1].iy);
g2.drawLine(img[2].ix,img[2].iy,img[1].ix,img[1].iy);
}
public void mouseDragged(MouseEvent e) {
int x,y;
x=e.getX();
y=e.getY();
//判断鼠标释放点坐标是否在图象区域
for(int i=0;i<img.length;i++){
if(x>=img.ix&&x<=img.ix+40){
if(y>=img.iy&&y<=img.iy+40){
img.ix=x;
img.iy=y;
img.sx=x;
img.sy=y-10;
repaint();
}
}
}
}
public void mouseMoved(MouseEvent e) {
}
}