codice:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
public class Main extends JPanel implements KeyListener,ActionListener{
static JFrame f;
static Main p;
static Image imgSfondo,imgPacman,imgFantasminoRosso;
static int tastoPremuto,xPacman=17,yPacman=19,x,y,xOldPacman,yOldPacman,xFantasmino=240,yFantasmino=186,xOldFantasmino,yOldFantasmino,distanzaPacmanFantasmino;
static Shape Pacman,Pallini,Fantasmino;
static ArrayList <Shape> muri,Palle,Mostri ;
static Movimento mov;
static boolean muroCostruito=false,pallaAggiunta=false;
static Graphics2D g1;
public static void main(String[] args) {
Mostri = new ArrayList<Shape>();
muri = new ArrayList<Shape>();
Palle = new ArrayList<Shape>();
JFrame f = new JFrame();
p = new Main();
f.setSize(476,428);
Dimension screenD = Toolkit.getDefaultToolkit().getScreenSize();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation( (screenD.width-486) /2 ,(screenD.height- 438)/2 );
p.setBackground(Color.BLUE);
imgFantasminoRosso =Toolkit.getDefaultToolkit().createImage("D:\\Università\\Java\\eclipse\\Pacman\\FantasminoRosso.png");
imgPacman =Toolkit.getDefaultToolkit().createImage("D:\\Università\\Java\\eclipse\\Pacman\\PacmanDestra.png");
p.addKeyListener(p);
p.setFocusable(true);
f.setResizable(false);
f.add(p);
f.setVisible(true);
mov = new Movimento(p);
Timer t = new Timer(1,p);
mov.start();
t.start();
}
public void paint (Graphics g){
g1 = (Graphics2D)g ;
super.paint(g1);
g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g1.drawImage(imgPacman, xPacman,yPacman, 30,30, this);
g1.drawImage(imgFantasminoRosso, xFantasmino,yFantasmino, 30,30, this);
Pacman = new Rectangle2D.Double(xPacman,yPacman,30,30);
Fantasmino = new Rectangle2D.Double(xFantasmino,yFantasmino,30,30);
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()== KeyEvent.VK_LEFT){
imgPacman =Toolkit.getDefaultToolkit().createImage("D:\\Università\\Java\\eclipse\\Pacman\\PacmanSinistra.png");
tastoPremuto= 1;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
imgPacman =Toolkit.getDefaultToolkit().createImage("D:\\Università\\Java\\eclipse\\Pacman\\PacmanDestra.png");
tastoPremuto = 2;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
imgPacman =Toolkit.getDefaultToolkit().createImage("D:\\Università\\Java\\eclipse\\Pacman\\PacmanGiù.png");
tastoPremuto = 3;
}
if(e.getKeyCode()==KeyEvent.VK_UP){
imgPacman =Toolkit.getDefaultToolkit().createImage("D:\\Università\\Java\\eclipse\\Pacman\\PacmanSu.png");
tastoPremuto = 4;
}
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
DIciamo che le parti più importanti sono queste, il gioco in questione è pacman , la pallina a è pacman e la pallina b che deve raggiungere pacman è il fantasmino...Grazie