Euclidean distance example. Move the mouse over the applet to see the distance change.
import java.awt.*;
import java.applet.*;
public class euclideandistanceexample001 extends Applet implements Runnable {
// Graphics for double buffering.
Graphics bufferGraphics;
Image offscreen;
int mousex = 0;
int mousey = 0;
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
repaint();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
}
}
}
public boolean mouseMove(Event e, int x, int y){
mousex = x;
mousey = y;
return true;
}
public void update(Graphics g){
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().width );
bufferGraphics.setColor( Color.red );
bufferGraphics.drawString( "Euclidean Distance " , 10 , 10 );
for ( int y = 0 ; y < 240/32 ; y++ ){
for ( int x = 0 ; x < 320/32 ; x++ ){
bufferGraphics.drawRect( x * 32 , y * 32 , 32 , 32 );
int dist = edist( mousex / 32 , mousey / 32 , x , y );
bufferGraphics.drawString( "" + dist , x * 32 , y * 32 + 16 );
}
}
g.drawImage(offscreen,0,0,this);
}
public int edist( int x1 , int y1 , int x2 , int y2 ){
return (int)Math.sqrt( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) );
}
}
Geen opmerkingen:
Een reactie posten
Opmerking: Alleen leden van deze blog kunnen een reactie posten.