当前位置: 技术问答>java相关
在java的Applet中的关于图象在界面中任意拖拉、图象之间的走向的连线的程序(高分、在线等待)
来源: 互联网 发布时间:2017-04-23
本文导语: 如题,如果一经试用可以的,马上结帖。 | 个箭头方向好难调,应该是你要的效果了,美化你自己完成吧,否则做好以后你也没成就感了,:) import javax.swing.*; import java.awt.*; import java.awt.im...
如题,如果一经试用可以的,马上结帖。
|
个箭头方向好难调,应该是你要的效果了,美化你自己完成吧,否则做好以后你也没成就感了,:)
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
public class applet5 extends Applet {
Image img1=null;
Image img2=null;
int myWidth=0,myHeight=0;
boolean b1=false,b2=false;
int width1=0,height1=0,w1=0,h1=0,wid1=0,hei1=0,w11=0,h11=0;
int width2=0,height2=0,w2=0,h2=0,wid2=0,hei2=0,w22=0,h22=0;
int startX=0,startY=0,endX=0,endY=0,startObj=0,endObj=0;
canvas can1=null;
canvas can2=null;
MediaTracker mt=new MediaTracker(this);
public void init() {
img1=this.getImage(this.getCodeBase(),"1.gif");
img2=this.getImage(this.getCodeBase(),"2.gif");
this.setLayout(null);
mt.addImage(img1,0);
mt.addImage(img2,0);
try {
mt.waitForAll();
} catch(Exception ex) {System.err.println(ex.toString());}
can1=new canvas(img1);
can2=new canvas(img2);
width1=img1.getWidth(this);
height1=img1.getHeight(this);
width2=img2.getWidth(this);
height2=img2.getHeight(this);
wid1=hei1=100;
wid2=hei2=200;
this.add(can1);
this.add(can2);
can1.setBounds(100,100,width1,height1);
can2.setBounds(200,200,width2,height2);
}
public void paint(Graphics g) {
if(b1&&b2) {
switch(startObj) {
case 1:
startX=wid1+width1/2;
startY=hei1+height1/2;
endX=wid2+width2/2;
endY=hei2+height2/2;
myWidth=width1;
myHeight=height1;
break;
case 2:
endX=wid1+width1/2;
endY=hei1+height1/2;
startX=wid2+width2/2;
startY=hei2+height2/2;
myWidth=width2;
myHeight=height2;
break;
}
g.drawLine(startX,startY,endX,endY);
drawArrowhead(g);
}
}
public void drawArrowhead(Graphics g) {
double xx=0,yy=0,xx1=0,yy1=0,xx2=0,yy2=0;
xx=startX+(endX-startX)*myWidth/Math.pow((double)(endX-startX)*(endX-startX)+(double)(endY-startY)*(endY-startY),1/2d);
yy=startY+(endY-startY)*myHeight/(Math.pow((double)(endX-startX)*(endX-startX)+(double)(endY-startY)*(endY-startY),1/2d));
xx1=xx-20*Math.cos(Math.atan(((double)yy-startY)/((double)xx-startX))-radians(30));
yy1=yy-20*Math.sin(Math.atan(((double)yy-startY)/((double)xx-startX))-radians(30));
xx2=xx-20*Math.sin(radians(60)-Math.atan(((double)(yy-startY))/((double)(xx-startX))));
yy2=yy-20*Math.cos(radians(60)-Math.atan(((double)(yy-startY))/((double)(xx-startX))));
if((startObj==1&&wid1+w1-w11=wid2+w2-w22)) {
xx1=xx+20*Math.cos(Math.atan(((double)yy-startY)/((double)xx-startX))-radians(30));
yy1=yy+20*Math.sin(Math.atan(((double)yy-startY)/((double)xx-startX))-radians(30));
xx2=xx+20*Math.sin(radians(60)-Math.atan(((double)(yy-startY))/((double)(xx-startX))));
yy2=yy+20*Math.cos(radians(60)-Math.atan(((double)(yy-startY))/((double)(xx-startX))));
}
g.fillPolygon(new int[]{(int)xx2,(int)xx1,(int)xx},new int[]{(int)yy2,(int)yy1,(int)yy},3);
}
public double radians(int degrees) {
return ((double)degrees)*Math.PI/180.0;
}
class canvas extends Canvas {
Image im1=null;
public canvas(Image im) {
super();
im1=im;
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if(e.getSource()==can1) {
w1=e.getX();
h1=e.getY();
wid1=((applet5)((canvas)e.getSource()).getParent()).can1.getX();
hei1=((applet5)((canvas)e.getSource()).getParent()).can1.getY();
((applet5)((canvas)e.getSource()).getParent()).can1.setBounds(wid1+w1-w11,hei1+h1-h11,width1,height1);
((applet5)((canvas)e.getSource()).getParent()).validate();
((canvas)e.getSource()).repaint();
((applet5)((canvas)e.getSource()).getParent()).repaint();
} else if(e.getSource()==can2) {
w2=e.getX();
h2=e.getY();
wid2=((applet5)((canvas)e.getSource()).getParent()).can2.getX();
hei2=((applet5)((canvas)e.getSource()).getParent()).can2.getY();
((applet5)((canvas)e.getSource()).getParent()).can2.setBounds(wid2+w2-w22,hei2+h2-h22,width2,height2);
((applet5)((canvas)e.getSource()).getParent()).validate();
((applet5)((canvas)e.getSource()).getParent()).repaint();
}
}
});
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(e.getSource()==can1) {
if(!((applet5)((canvas)e.getSource()).getParent()).b1&&((applet5)((canvas)e.getSource()).getParent()).b2) {
((applet5)((canvas)e.getSource()).getParent()).startObj=1;
((applet5)((canvas)e.getSource()).getParent()).endObj=2;
}
((applet5)((canvas)e.getSource()).getParent()).b1=true;
w11=e.getX();
h11=e.getY();
} else if(e.getSource()==can2) {
if(((applet5)((canvas)e.getSource()).getParent()).b1&&!((applet5)((canvas)e.getSource()).getParent()).b2) {
((applet5)((canvas)e.getSource()).getParent()).startObj=2;
((applet5)((canvas)e.getSource()).getParent()).endObj=1;
}
((applet5)((canvas)e.getSource()).getParent()).b2=true;
w22=e.getX();
h22=e.getY();
}
}
});
}
public void paint(Graphics g) {
g.drawImage(im1,0,0,this);
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
public class applet5 extends Applet {
Image img1=null;
Image img2=null;
int myWidth=0,myHeight=0;
boolean b1=false,b2=false;
int width1=0,height1=0,w1=0,h1=0,wid1=0,hei1=0,w11=0,h11=0;
int width2=0,height2=0,w2=0,h2=0,wid2=0,hei2=0,w22=0,h22=0;
int startX=0,startY=0,endX=0,endY=0,startObj=0,endObj=0;
canvas can1=null;
canvas can2=null;
MediaTracker mt=new MediaTracker(this);
public void init() {
img1=this.getImage(this.getCodeBase(),"1.gif");
img2=this.getImage(this.getCodeBase(),"2.gif");
this.setLayout(null);
mt.addImage(img1,0);
mt.addImage(img2,0);
try {
mt.waitForAll();
} catch(Exception ex) {System.err.println(ex.toString());}
can1=new canvas(img1);
can2=new canvas(img2);
width1=img1.getWidth(this);
height1=img1.getHeight(this);
width2=img2.getWidth(this);
height2=img2.getHeight(this);
wid1=hei1=100;
wid2=hei2=200;
this.add(can1);
this.add(can2);
can1.setBounds(100,100,width1,height1);
can2.setBounds(200,200,width2,height2);
}
public void paint(Graphics g) {
if(b1&&b2) {
switch(startObj) {
case 1:
startX=wid1+width1/2;
startY=hei1+height1/2;
endX=wid2+width2/2;
endY=hei2+height2/2;
myWidth=width1;
myHeight=height1;
break;
case 2:
endX=wid1+width1/2;
endY=hei1+height1/2;
startX=wid2+width2/2;
startY=hei2+height2/2;
myWidth=width2;
myHeight=height2;
break;
}
g.drawLine(startX,startY,endX,endY);
drawArrowhead(g);
}
}
public void drawArrowhead(Graphics g) {
double xx=0,yy=0,xx1=0,yy1=0,xx2=0,yy2=0;
xx=startX+(endX-startX)*myWidth/Math.pow((double)(endX-startX)*(endX-startX)+(double)(endY-startY)*(endY-startY),1/2d);
yy=startY+(endY-startY)*myHeight/(Math.pow((double)(endX-startX)*(endX-startX)+(double)(endY-startY)*(endY-startY),1/2d));
xx1=xx-20*Math.cos(Math.atan(((double)yy-startY)/((double)xx-startX))-radians(30));
yy1=yy-20*Math.sin(Math.atan(((double)yy-startY)/((double)xx-startX))-radians(30));
xx2=xx-20*Math.sin(radians(60)-Math.atan(((double)(yy-startY))/((double)(xx-startX))));
yy2=yy-20*Math.cos(radians(60)-Math.atan(((double)(yy-startY))/((double)(xx-startX))));
if((startObj==1&&wid1+w1-w11=wid2+w2-w22)) {
xx1=xx+20*Math.cos(Math.atan(((double)yy-startY)/((double)xx-startX))-radians(30));
yy1=yy+20*Math.sin(Math.atan(((double)yy-startY)/((double)xx-startX))-radians(30));
xx2=xx+20*Math.sin(radians(60)-Math.atan(((double)(yy-startY))/((double)(xx-startX))));
yy2=yy+20*Math.cos(radians(60)-Math.atan(((double)(yy-startY))/((double)(xx-startX))));
}
g.fillPolygon(new int[]{(int)xx2,(int)xx1,(int)xx},new int[]{(int)yy2,(int)yy1,(int)yy},3);
}
public double radians(int degrees) {
return ((double)degrees)*Math.PI/180.0;
}
class canvas extends Canvas {
Image im1=null;
public canvas(Image im) {
super();
im1=im;
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if(e.getSource()==can1) {
w1=e.getX();
h1=e.getY();
wid1=((applet5)((canvas)e.getSource()).getParent()).can1.getX();
hei1=((applet5)((canvas)e.getSource()).getParent()).can1.getY();
((applet5)((canvas)e.getSource()).getParent()).can1.setBounds(wid1+w1-w11,hei1+h1-h11,width1,height1);
((applet5)((canvas)e.getSource()).getParent()).validate();
((canvas)e.getSource()).repaint();
((applet5)((canvas)e.getSource()).getParent()).repaint();
} else if(e.getSource()==can2) {
w2=e.getX();
h2=e.getY();
wid2=((applet5)((canvas)e.getSource()).getParent()).can2.getX();
hei2=((applet5)((canvas)e.getSource()).getParent()).can2.getY();
((applet5)((canvas)e.getSource()).getParent()).can2.setBounds(wid2+w2-w22,hei2+h2-h22,width2,height2);
((applet5)((canvas)e.getSource()).getParent()).validate();
((applet5)((canvas)e.getSource()).getParent()).repaint();
}
}
});
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(e.getSource()==can1) {
if(!((applet5)((canvas)e.getSource()).getParent()).b1&&((applet5)((canvas)e.getSource()).getParent()).b2) {
((applet5)((canvas)e.getSource()).getParent()).startObj=1;
((applet5)((canvas)e.getSource()).getParent()).endObj=2;
}
((applet5)((canvas)e.getSource()).getParent()).b1=true;
w11=e.getX();
h11=e.getY();
} else if(e.getSource()==can2) {
if(((applet5)((canvas)e.getSource()).getParent()).b1&&!((applet5)((canvas)e.getSource()).getParent()).b2) {
((applet5)((canvas)e.getSource()).getParent()).startObj=2;
((applet5)((canvas)e.getSource()).getParent()).endObj=1;
}
((applet5)((canvas)e.getSource()).getParent()).b2=true;
w22=e.getX();
h22=e.getY();
}
}
});
}
public void paint(Graphics g) {
g.drawImage(im1,0,0,this);
}
}
}