ho trovato il codice adesso lo posto, spero qualcuno riesca ad aiutarmi
codice:package Immagini; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class MovimentoFrame extends JFrame{ public static final int MILLISECONDI = 100; public static final int SPRITE_DIM = 32; public static final int COLONNE = 20; public static final int RIGHE = 20; public static final int APPLET_WIDTH = RIGHE*SPRITE_DIM; public static final int APPLET_HEIGHT = COLONNE*SPRITE_DIM; public static final int JUMP = SPRITE_DIM; private JLabel[] arraylabel; private ImageIcon imgPers; private JLabel labelPers; private JPanel jp; private boolean isControlled=true; private int x; private int y; private int inc_x; private int inc_y; private Timer timer; private Image image; private ImageIcon immagine; private ImageIcon imgTexture; public MovimentoFrame(int x0, int y0){ x=x0; y=y0; inc_x=inc_y=0; try { image = ImageIO.read(new File("/home/vincenzo/Scrivania/grass09.jpg")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } getContentPane().setLayout(null); //tolgo il LayoutManager predefinito getContentPane().setPreferredSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT)); imgPers = new ImageIcon("/home/vincenzo/Scrivania/persT.PNG"); labelPers = new JLabel(imgPers); labelPers.setBounds(0, 0, 32, 32); // Imposto posizione e dimensione del mio JTextPane labelPers.setOpaque(false); jp = new BackgroundPanel(image); jp.setLayout(null); jp.setBounds(0, 0, APPLET_WIDTH, APPLET_HEIGHT); // Imposto posizione e dimensione del mio JPanel //getContentPane().add(labelPers); jp.add(labelPers);// createImageIcon(); createPanelImage(); getContentPane().add(jp); addKeyListener(new DirectionKeyListener()); timer = new Timer(MILLISECONDI, new ActionListener() { public void actionPerformed(ActionEvent evt) { int x1 = x + inc_x; int y1 = y + inc_y; if ((x1>=0) && (x1<APPLET_WIDTH)) x=x1; if ((y1>=0) && (y1<APPLET_HEIGHT)) y=y1; aggiorna(); } }); } private class DirectionKeyListener extends KeyAdapter { //Spostiamo l'immagine a seconda dell'evento della tastiera public void keyPressed(KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: inc_y=-JUMP; isControlled=false; break; case KeyEvent.VK_DOWN: inc_y=JUMP; isControlled=false; break; case KeyEvent.VK_LEFT: inc_x=-JUMP; isControlled=false; break; case KeyEvent.VK_RIGHT: inc_x=JUMP; isControlled=false; break; default: break; } } public void keyReleased(KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: inc_y=0; break; case KeyEvent.VK_LEFT: case KeyEvent.VK_RIGHT: inc_x=0; break; } } } public void aggiorna() { labelPers.setBounds(x, y,32,32); if(!isControlled) controll(); repaint(); } public void controll(){ for(int i=0;i<RIGHE;i++) if(labelPers.getLocationOnScreen().equals(arraylabel[i].getLocationOnScreen())){ System.out.println(arraylabel[i].getX()+" "+arraylabel[i].getY()); break; } isControlled = true; } class BackgroundPanel extends JPanel { private Image img; public BackgroundPanel (Image img) { super (new BorderLayout ()); this.img = img; } public void paintComponent (Graphics g) { super.paintComponent (g); g.drawImage (img, 0, 0, this); } } private void createImageIcon() { int i; int j; arraylabel = new JLabel[RIGHE]; imgTexture = new ImageIcon("/home/vincenzo/Scrivania/erbaTR.PNG"); for(i=0;i<RIGHE;i++){ arraylabel[i]= new JLabel(imgTexture); arraylabel[i].setSize(32,32); //arraylabel[i].setBounds(i*JUMP, JUMP*4, 32, 32); } } private void createPanelImage(){ //for(int i=0;i<RIGHE;i++) // jp.add(arraylabel[i]); for(int i=0;i<RIGHE;i++){ arraylabel[i].setBounds(i*JUMP, JUMP*5, 32, 32); jp.add(arraylabel[i]); } } public static void main(String[] args) { MovimentoFrame c= new MovimentoFrame(0,0); c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.pack(); c.setResizable(false); c.setVisible(true); c.timer.start(); c.setLocationRelativeTo(null); System.out.println(c.getSize()); } }


Rispondi quotando