martedì 2 dicembre 2008

Programma rotazione poligono
 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.Polygon;
public class RotazionePoligonno extends Applet implements MouseListener {
private int[] xpoints ={
0,-10,-7,7,10};
private int[] ypoints ={-
10,-2,10,10,-2};
private Polygon poly;

int rotation=
0;
public void init(){
poly=new Polygon(xpoints,ypoints,xpoints.length);
addMouseListener(this);
}

public void paint (Graphics g){
Graphics2D g2D= (Graphics2D) g;
int ampiezza=getSize().width;
int altezza=getSize().height;


g2D.translate(ampiezza/
2,altezza/2);
g2D.scale(
20,20);
g2D.rotate(Math.toRadians(rotation));
g2D.setColor(Color.RED);
g2D.draw(poly);

}
public void mouseEntered (MouseEvent m) {}
public void mouseExited (MouseEvent m) {}
public void mouseReleased (MouseEvent m) {}
public void mouseClicked (MouseEvent m) {}
public void mousePressed(MouseEvent m) {
switch (m.getButton() ){
case MouseEvent.BUTTON1:
rotation--;
if (rotation<
0) rotation=359;
repaint ();
break;
case MouseEvent.BUTTON3:
rotation++;
if (rotation<
360) rotation=0;
repaint ();
break;
}
}

public void keyReleased(KeyEvent k){}
public void keyTyped(KeyEvent k){}
public void keyPressed(KeyEvent k){

switch (k.getKeyCode()) {
case KeyEvent.VK_LEFT:
rotation--;
if (rotation<
0) rotation=359;
repaint();
break;
case KeyEvent.VK_RIGHT:
rotation++;
if (rotation>
360) rotation=0;
repaint();
break;

}
}
}


1 commento: