0 Members and 2 Guests are viewing this topic.
import java.awt.Graphics;import java.awt.event.*;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.Timer;public class Game extends JPanel implements KeyListener { /* Game Panel class containing all functions */ /* Global static variables */ JFrame mainFrame = new JFrame("Falling Blocks"); int y = 520; int[] locations = {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800}; /* Global dynamic variables */ int a; //Horizontal acceleration int x; boolean needs_repaint; boolean on_title_screen, on_game, on_game_over; public void init_variables() { /* Initates the dynamic variables in order to show Title Screen */ a = 0; x = 0; on_title_screen = true; on_game = false; on_game_over = false; needs_repaint = true; } public void paintComponent(Graphics g) { /* Draws the elements on the screen */ super.paintComponent(g); if (on_title_screen) { g.drawString("Press SPACE to start game", 10, 10); g.drawString("Made by David Gomes", 10, 30); } else if (on_game) { //Draw the player g.fillRect(x, y, 50, 50); } else { //Game Over g.drawString("Game Over", 10, 10); } //Repainting has finished needs_repaint = false; } public Game() { /* Defines the frame and the game panel */ this.setFocusable(true); this.addKeyListener(this); /* Fixed Layout frame */ mainFrame.setSize(852, 600); mainFrame.setResizable(false); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add game panel to main frame mainFrame.getContentPane().add(this); } public void run() { /* Displays the frame, start the game */ mainFrame.setVisible(true); this.requestFocus(); init_variables(); } @Override public void keyPressed(KeyEvent key) { /* Handles key presses */ if (on_title_screen) { /* Start the game when user presses SPACE */ switch (key.getKeyCode()) { case KeyEvent.VK_SPACE: on_title_screen = false; on_game = true; needs_repaint = true; break; } } else if (on_game) { switch (key.getKeyCode()) { case KeyEvent.VK_LEFT: // Move x coordinate left if (x > 0) { x -= 50; needs_repaint = true; } break; case KeyEvent.VK_RIGHT: // Move x coordinate right if (x < 800) { x += 50; needs_repaint = true; } break; } } else { // Game Over init_variables(); //Go to title screen needs_repaint = true; } // Repaint only if needed if (needs_repaint) this.repaint(); } /* Game Class is not abstract, and these functions are not needed */ @Override public void keyReleased(KeyEvent arg0) { } @Override public void keyTyped(KeyEvent arg0) { }}
new Thread(new Runnable() { long prevTime = System.currentTimeMillis(); public void run() { if(System.currentTimeMillis() >= prevTime+1000*N){ prevTime = System.currentTimeMillis(); //call the function (or embed the code here) } } }).start();
import java.awt.Color;import java.awt.Graphics;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.Timer;import java.awt.event.*;public class Game extends JPanel { JFrame mainFrame = new JFrame("Game"); String[] full_map = {"0101010101", "1010101010", "0101010101", "1010101010", "0101010101", "1010101010", "0101010101", "1010101010", "0101010101", "1010101010"}; new Thread(new Runnable() { long prevTime = System.currentTimeMillis(); public void run() { if (System.currentTimeMillis() >= prevTime+1000*2) { prevTime = System.currentTimeMillis(); System.out.println("hello"); } } }).start(); public void paintComponent(Graphics g) { /* Draws the elements on the screen */ super.paintComponent(g); int y, x; for (y = 0; y < this.full_map.length; y++) { for (x = 0; x < this.full_map[y].length(); x++) { if (this.full_map[y].charAt(x) == '1') { g.setColor(Color.BLACK); g.fillRect(50*x, 50*y, 50, 50); } } } } public Game() { /* Defines the frame and the game panel */ this.setFocusable(true); mainFrame.setSize(this.full_map[0].length()*50, this.full_map.length*50); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.getContentPane().add(this); } public void start() { /* Displays the frame, start the game */ mainFrame.setVisible(true); this.requestFocus(); }}
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on token ";", { expected after this token Syntax error on token "}", delete this token at Game.<init>(Game.java:22)
public Game(){ //other stuff timer = new Timer([MILLISECONDS], new ActionListener(){ public void actionPerformed(ActionEvent e){ //run whatever functions you want. }}); timer.start();}
You're creating a new object (a thread) inside your class and calling one of its methods. I'm pretty sure you can't do that, you can only have code there to initialize variables. You can instead do "Thread myThread = new Thread(..);" and then in your constructor do "myThread.start();" Or just move it all to the constructor since you don't need the object after that.