lunedì 24 novembre 2008

Esercitazione Lunedi 24 Novembre


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.awt.geom.*;
public class RotazionePoligono extends Applet implements MouseListener, KeyListener{
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);
addKeyListener(this);
}
public void paint (Graphics g){
Graphics2D g2D=(Graphics2D) g;
int ampiezza=getSize().width;
int altezza=getSize().height;
g2D.setColor(Color.YELLOW);
g2D.fillRect(0,0,ampiezza,altezza);
g2D.translate(ampiezza/2,altezza/2);
g2D.scale(20,20);
g2D.rotate(Math.toRadians(rotation));
g2D.setColor(Color.RED);
g2D.draw(poly);
g2D.setColor(Color.BLUE);
g2D.fill(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:

Larss ha detto...

è bello....l michel bisca e mauro