为什么我的图象只能向右下移动?请帮忙看看源代码,怎么改正? ( 积分: 200 )

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&amp;&amp;x<=img.ix+40){
if(y>=img.iy&amp;&amp;y<=img.iy+40){
img.ix=x;
img.iy=y;
img.sx=x;
img.sy=y-10;
repaint();
}
}
}
}
public void mouseMoved(MouseEvent e) {
}
}
 
//主程序清单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(&quot;Loaded image&quot;);
picture=Toolkit.getDefaultToolkit().getImage(&quot;lan.gif&quot;);
//装载图像
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,&quot;172.16.16.43&quot;);
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+&quot;
&quot;+1,20,20);
img[1]=new MyImage(i,s+&quot;
&quot;+2,70,70);
img[2]=new MyImage(i,s+&quot;
&quot;+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&amp;&amp;x<=img.ix+40){
if(y>=img.iy&amp;&amp;y<=img.iy+40){
img.ix=x;
img.iy=y;
img.sx=x;
img.sy=y-10;
repaint();
}
}
}
}
public void mouseMoved(MouseEvent e) {
}
}
 
这个 ,你 自己去 继承他吧。
 
new component;
res the image
constructor Txxxx.Create(AOwer: TComponent);
begin
{load the image;
}
end;
 
interface
uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls;
type
TyouImage = class(TImage)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;

procedure Register;
implementation
procedure Register;
begin
RegisterComponents('wzxphk', [TyouImage]);
end;

end.
 
楼上几个的思路都是对的
public class ImageCom extends Component {
}
然后你想要实现什么方法就在上面的类中写什么方法 比如我实现 show()
就在上面添加
public void show() {
// TODO
}
 
多人接受答案了。
 
顶部