I started learning java, and tried to make a game, i almost finished it but then i realized that it doesn't work, so i copied the project and started dumping code from my game to find what did i do wrong, but i just cant figure it out. Here is the main class:
import javax.swing.JFrame; public class Main extends JFrame { // KONSTRUKTOR public Main() { super("Plavko glupavko"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(800, 600); setResizable(false); setVisible(true); add(new Igra()); } // MAIN METOD public static void main(String[] args) { new Main(); } }
here is igra class:
import java.awt.Color; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JPanel; import javax.swing.Timer; public class Igra extends JPanel implements ActionListener { private Timer tajmer; private Tabla tabla; private Igrac igrac; private int igracV; private int igracDx; // KONSTRUKTOR public Igra() { setSize(800, 600); setBackground(Color.WHITE); addKeyListener(new Slusalac()); setFocusable(true); tabla = new Tabla(1); igrac = tabla.getIgrac(); igracV = 2; igracDx = 0; tajmer = new Timer(50, this); tajmer.start(); } // ISCRTAVANJE public void paint(Graphics g) { super.paint(g); // Igrac g.drawImage(igrac.getSlika(), igrac.getX(), igrac.getY(), null); Toolkit.getDefaultToolkit().sync(); g.dispose(); } // GLAVNA PETLJA public void actionPerformed(ActionEvent e) { pomeriIgracaX(); repaint(); } // Kretanje igraca public void pomeriIgracaX() { int duhx = igrac.getXBezPomeraja() + igracDx; igrac.setX(duhx); } // KLASA ZA TASTATURU private class Slusalac extends KeyAdapter { public void keyPressed(KeyEvent e) { int kod = e.getKeyCode(); // pomeraj desno if (kod == KeyEvent.VK_RIGHT) { igracDx = igracV; } // pomeraj levo if (kod == KeyEvent.VK_LEFT) { igracDx = -igracV; } } public void keyReleased(KeyEvent e) { int kod = e.getKeyCode(); if (kod == KeyEvent.VK_RIGHT) { if (igracDx > 0) igracDx = 0; } if (kod == KeyEvent.VK_LEFT) { if (igracDx < 0) igracDx = 0; } } } }
here is tabla class:
import java.awt.Image; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Tabla { private Image pozadina; // ove liste ce da sadrze objekte private Igrac igrac; public Tabla(int tabla) { Scanner s; String imefajla = "table/tabla"+tabla+".tbl"; File fajl = new File(imefajla); try { s = new Scanner(fajl); int brreda = 0; while (s.hasNextLine()) { String red = s.nextLine(); for (int i = 0; i < red.length(); i++) { if (red.charAt(i) == 'i') { igrac = new Igrac(i, brreda); } } brreda++; } s.close(); } catch (FileNotFoundException e) {} } // GETERI public Igrac getIgrac() { return igrac; } }
and here is igrac class:
import java.awt.Image; import java.awt.Rectangle; import javax.swing.ImageIcon; public class Igrac { private Image slika; private final int SIRINA = 30; private final int VISINA = 30; private int x; private int y; // pomeraj za kameru public static int kpx = 0; public static int kpy = 0; // KONSTRUKTOR public Igrac(int x, int y) { this.x = x * 30; this.y = y * 30; String fajl = "slike/igrac.png"; slika = new ImageIcon(fajl).getImage(); } // GETERI public Image getSlika() { return slika; } public Rectangle getGranice() { return new Rectangle(x, y, SIRINA, VISINA); } public int getXBezPomeraja() { return x; } public int getYBezPomeraja() { return y; } public int getX() { return x + kpx; } public int getY() { return y + kpy; } // SETERI public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } }