13 Replies - 407 Views - Last Post: 05 June 2012 - 06:34 AM Rate Topic: -----

#1 MMBurma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-August 11

Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 04:13 PM

Hey everyone, first I'd like to say that I'm sorry to post another homework/final project question, but this is a moment of desperation. I've been building a Reversi game, and everything was going PERFECTLY. But, after trying to implement the computer's actions, the draw method breaks. Not that it doesn't draw, but the original method in the framework simply refuses to fire off. If I comment out the computer's actions, it draws fine. The student who made it is pretty competent, yet even he has no idea why something like this would happen, especially without any sort of error messages. Any ideas what this could be from? I'll post some relevant code, if you need anything else don't hesitate to ask; Just conserving space in this post. Sorry if to be another beginner-esque student, but I'm trapped here. Thanks.

This is the method that originates from the framework:
public void paint(Graphics g) {
		super.paint(g); // CALL SUPER CLASS
		Graphics2D g2d = (Graphics2D) g; // CAST GRAPHICS2D
		System.err.println("Paint method in Run activated!");
		gl.Draw(g2d); // CALL DRAW METHOD FROM GAMELOOP
	}



This is the GameLoop class, which basically serves as a centerpiece for the game's files and calls the update and draw methods.

package Game;

import java.awt.Graphics2D;
//(many more imports, but I cleaned them up for this post)

public class GameLoop implements Loop {
	
	Graphics2D graph2D;
	Game game = new Game(this);
	Board gameBoard = new Board();
	static int drawPause = 2000;
	
	/*
	 * Here is where you will put everything that you want to be initialized and
	 * it is called when the game is started after the create method. To change
	 * and get information about the form, type: Handler.
	 * and after the dot look through the different methods.
	 */
	@Override
	public void Create() {
		Handler.setDim(500, 600);
		Handler.setTitle("Greg's Reversi");
		Handler.setResizable(false);
	}
	
	/*
	 * Here is where you call all of the assets you want to load. This is called
	 * before the create method is called. To load an image use Handler.loadImage(path)
	 * method that I created to load images quick and easy.
	 */
	@Override
	public void Load() {
		
	}
	
	/*
	 * Here is where you will put all of your checks or updates that you want to
	 * have happen every second. This is called every split second. For example
	 * you can use this for key presses using KeyAdapt and also for checking the
	 * mouse using MouseAdapt. To check a KeyPress do KeyAdapt.wasKeyPressed(KeyEvent. 
	 * and then after the dot you would choose the key you want.
	 */
	@Override
	public void Update() {
		game.update();
		System.out.println("Gameloop updated.");
		gameBoard.update();
	}

	/*
	 * Here is where you will put what you want to draw to the screen and is
	 * called right after the update method every split second. To draw an image
	 * to the screen type: g2d.drawImage(img, x, y, observer)
	 * Usually the observer parameter is set to null and will still print the image
	 * to the screen.
	 */
	@Override
	public void Draw(Graphics2D g2d) {
		gameBoard.draw(g2d);
		System.out.println("Drawing in GameLoop...");
	}
	
	
}



And here's the Board class, responsible for actually drawing each Tile object.
package Game;

import java.awt.Graphics2D;
//(Same deal with these imports, trimmed for post)

public class Board extends JPanel {
	
	private static int boardDimension = 10, tileSize = 50;		//Sets dimensions of the board
	//Board Dimensions are squared, and should always be a multiple of two
	Graphics2D graph2d = new Graphics2D();
	private List<Tile> tileList = new ArrayList<Tile>();
	
	//Default constructor, creates new board
	public Board() {
		super(new GridLayout(boardDimension + 1, boardDimension));	//One extra row for utility buttons
		createBoard();
	}
	
	private void createBoard() {
		
		JOptionPane.showMessageDialog(null, "You are Black; Black always moves first.");
		
		//This for-loop runs once per row needed, ie. would create 10 rows of 10 tiles each
		for (int j = 0; j < boardDimension; j++) {
			//This for-loop will program one row by adding all Tile objects to tileList
			for (int i = 0; i < boardDimension; i++) {
				tileList.add(new Tile(i, j));
			}
		}
		
		//After board has been created, the four center tiles must be changed as following the rules for Othello and other reversi games
		//Two black squares in center
		tileList.get(45).setState(1);
		tileList.get(54).setState(1);
		//Two white squares in center
		tileList.get(44).setState(2);
		tileList.get(55).setState(2);
	}
	
	public List<Tile> getTileList() {
		return tileList;
	}
	
	public void update() {
		repaint(); //This does not do anything, but it was worth a shot.
	}
	
	public void draw(Graphics2D g2d) {
		
		System.out.println("Drawing in board...");
		
		graph2d = g2d;
		
		//This for-loop will program one row by adding all Tile objects to tileList
		for (int i = 0; i < (boardDimension * boardDimension); i++) {
			tileList.get(i).draw(g2d);
		}
		
		
	}
	
	
	
	
	
	
	
}



Thanks again for even taking a peek. If there's any form of expletives I sincerely apologize, I was frustrated and trying to corner the problem so there may be a println I missed or something.

I also forgot to show the Game class, which is where many decisions happen in the game itself.

package Game;

import javax.swing.JOptionPane;

import Engine.MouseAdapt;

public class Game {
	
	private int difficulty;
	private int turn; //1 = player, 2 = computer
	private int playerStatus = 1, compStatus = 2;
	private static int sleepTime = 500, turnPause = 100;
	private boolean midTurn;
	
	GameLoop gameloop;
	Computer computer;
	Player player;
	JOptionPane tempPane = new JOptionPane();
	
	public Game(GameLoop gl) {
		gameloop = gl;
		setDifficulty();
		
		player = new Player(gameloop, playerStatus);
		computer = new Computer(gameloop, difficulty, compStatus);
		
		turn = 1;
		midTurn = false;
	}
	
	public void setDifficulty() {
		//Array holding all possible difficulty levels & question of which to use via pop-up
		Object[] options = {0, 1, 2, 3};
		difficulty = 0; //Sets difficulty to 0 if player cancels dialog box
		tempPane.showInputDialog(null, "Difficulty Setting", "Please enter the difficulty level (0-3; 0 is easiest): ", JOptionPane.PLAIN_MESSAGE, null, options, difficulty);
	}
	
	public int getDifficulty() {
		return difficulty;
	}
	
	public int getTurn() {
		return turn;
	}
	
	public boolean isMidTurn() {
		return midTurn;
	}
	
	public void update() {
		System.err.println("Update in game...");
		
		//Player's turn
		if (turn == 1)
			player.update();
		if (turn == 1 && midTurn == false) {
			player.takeTurn();
			midTurn = true;
		}
		
		//Computer's turn
		else if (turn == 2 && midTurn == false) {
			computer.takeTurn();
			midTurn = true;
		}
	}
	
	public void setTurn(int newTurnVal, boolean newMidTurn) {
		turn = newTurnVal;
		midTurn = newMidTurn;
		System.out.println("Changed turns.");
		System.out.println("Turn is now #" + turn);
		System.out.println("MidTurn is set to: " + midTurn);
		try {
			Thread.currentThread().sleep(turnPause);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		update();
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Issues with calling draw method (10 times/second)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 05:02 PM

who calls Game setTurn() ?
Was This Post Helpful? 0
  • +
  • -

#3 MMBurma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-August 11

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 05:14 PM

View Postpbl, on 04 June 2012 - 05:02 PM, said:

who calls Game setTurn() ?


setTurn() is called by the Player class; I'll post that as well.

package Game;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.Timer;

import Engine.Handler;
import Engine.KeyAdapt;
import Engine.MouseAdapt;

public class Player {
	
	private int mouseX = -1, mouseY = -1, myStatus;
	private static int sleepTime = 1000, tileSize = 50;
	private boolean duringTurn, clickedNewPoint, isTurnFinished;
	
	GameLoop gameloop;
	
	public Player(GameLoop gl, int playerStatus) {
		isTurnFinished = false;
		duringTurn = false;
		clickedNewPoint = false;
		gameloop = gl;
		myStatus = playerStatus;
	}
	
	public void takeTurn() {
		System.out.println("Your turn... HAVE NO MERCY!");
		
		mouseX = -1;
		mouseY = -1;
		
		isTurnFinished = false;
		duringTurn = true;
		
		//Keep waiting until player clicks a point
		while (clickedNewPoint == false) {
			try {
				update();
				Thread.sleep(sleepTime);
			}
			catch (InterruptedException e) {
				System.err.println("Thread Interruption Error");
			}
		}
		
		if (clickedNewPoint == true && duringTurn == true) {
			
			System.err.println("Attention! This should only print once!");
			
			clickedNewPoint = false;
			duringTurn = false;
			
			System.out.println("MouseX = " + mouseX + ", MouseY = " + mouseY);
			
			//Converting mouseX and mouseY to corresponding tile locations on the grid
			int x = (int) mouseX / tileSize;
			int y = (int) mouseY / tileSize;
			
			//Reset mouse values
			mouseX = -1;
			mouseY = -1;
			
			//Create move, check validity
			Move thisMove = new Move(gameloop, x, y, myStatus);
			
			if (thisMove.isValid() == true) {
				thisMove.makeMove();
				System.out.println("Player finished move.");
				gameloop.game.setTurn(2, false);
			}
			else {
				clickedNewPoint = false;
				takeTurn();
			}
		}
			
		
	}
	
	public int getStatus() {
		return myStatus;
	}

	public void update() {
		if (MouseAdapt.isMouseClicked() == true && MouseAdapt.isMouseReleased() == true) {
			
			System.out.println("Mouse location is (" + MouseAdapt.getMouseX() + ", " + MouseAdapt.getMouseY() + ")");
			
			if (duringTurn == true) {
				mouseX = MouseAdapt.getMouseX();
				mouseY = MouseAdapt.getMouseY();
				System.out.println("MousePoint is now: (" + mouseX + ", " + mouseY + ")");
				clickedNewPoint = true;
			}
		}
	}
}



That was the Player class, here is the computer class. Both call setTurn() after finishing their respective turns. Computer is not finished, but was theoretically finished for testing purposes.

package Game;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Computer {
	
	private int difficulty, myStatus = 2;
	private static int boardSize = 10;
	private List<Tile> tileList;
	
	GameLoop gameloop;
	Random randomGen = new Random();
	
	public Computer(GameLoop gl, int dif, int compStatus) {
		gameloop = gl;
		difficulty = dif;
		myStatus = compStatus;
	}
	
	public int getStatus() {
		return myStatus;
	}

	public void takeTurn() {
		System.out.println("Prepare to be bested by your computer overlord!");
		
		tileList = gameloop.gameBoard.getTileList();
		
		if (difficulty == 0)
			makeRandomMove();
		else if (difficulty > 0)
			makeBestMove();
		else
			System.err.println("Error while deciding computer's preferred move method.");
	}
	
	public void makeRandomMove() {
		System.out.println("Computer is picking a random move. What a n00b.");
		
		//Randomly generate an option, used to pick an algorithm which 
		//int option = randomGen.nextInt(4);
		/**
		//Set option to 0 for testing
		int option = 0;
		switch (option) {
		//Sweep possibilities from upper-left, left to right
		case 0:
			for (int j = 0; j < boardSize; j++) {
				for (int i = 0; i < boardSize; i++) {
					Move thisMove = new Move(gameloop, i, j, myStatus);
					if (thisMove.isValid() == true)
						finalizeMove(thisMove);
				}
			}
			break;
		
		case 1: break;
		case 2: break;
		case 3: break;
			
		}
		
		
		**/
		
		Move thisMove = new Move(gameloop, 5, 4, myStatus);
		System.out.println("Computer's test move is valid? " + thisMove.isValid());
		finalizeMove(thisMove);
	}
	
	public void makeBestMove() {
		
	}
	
	public void finalizeMove(Move myMove) {
		myMove.makeMove();
		System.out.println("Computer finished move.");
		gameloop.game.setTurn(1, false);
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 05:19 PM

You have at least 2 different Thread.sleep()
Are you sure you are pausing the good thread ? You can freeze your GUI quite fast if you do not pause the good thread.

Use a javax.swing.Timer for that type of thing
Was This Post Helpful? 1
  • +
  • -

#5 MMBurma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-August 11

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 05:25 PM

View Postpbl, on 04 June 2012 - 05:19 PM, said:

You have at least 2 different Thread.sleep()
Are you sure you are pausing the good thread ? You can freeze your GUI quite fast if you do not pause the good thread.

Use a javax.swing.Timer for that type of thing


Thank you, I'll experiment with that. I was using sleep() to keep it simpler than creating an all new TimerTask class, but it makes sense that sleep could be dangerous; I'll try a timer.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 05:39 PM

View PostMMBurma, on 04 June 2012 - 08:25 PM, said:

Thank you, I'll experiment with that. I was using sleep() to keep it simpler than creating an all new TimerTask class, but it makes sense that sleep could be dangerous; I'll try a timer.

Not a all new TimerTask class. There is one in the API just for that
http://docs.oracle.c...wing/Timer.html
Was This Post Helpful? 1
  • +
  • -

#7 MMBurma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-August 11

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 05:56 PM

View Postpbl, on 04 June 2012 - 05:39 PM, said:

View PostMMBurma, on 04 June 2012 - 08:25 PM, said:

Thank you, I'll experiment with that. I was using sleep() to keep it simpler than creating an all new TimerTask class, but it makes sense that sleep could be dangerous; I'll try a timer.

Not a all new TimerTask class. There is one in the API just for that
http://docs.oracle.c...wing/Timer.html


Wow, thank you! However, I've rooted out all sleep calls, and didn't need to add any timers. I also managed to convert it from player vs computer to player vs player, to get that simpler version working. But, as SOON as I implemented the code to call player 2's turn, the exact same problem arose; the board was updated data-wise, but the image was exactly the same. A System.err.println("blahblahblah") showed that this method was only called once. I'm at my wit's end, this problem never happened to me before this project.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 06:34 PM

View PostMMBurma, on 04 June 2012 - 08:56 PM, said:

A System.err.println("blahblahblah") showed that this method was only called once. I'm at my wit's end, this problem never happened to me before this project.

You'll have to show us which method and how you set up your Timer and its actionPerformed()
Was This Post Helpful? 0
  • +
  • -

#9 MMBurma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-August 11

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 06:50 PM

View Postpbl, on 04 June 2012 - 06:34 PM, said:

View PostMMBurma, on 04 June 2012 - 08:56 PM, said:

A System.err.println("blahblahblah") showed that this method was only called once. I'm at my wit's end, this problem never happened to me before this project.

You'll have to show us which method and how you set up your Timer and its actionPerformed()


Well that's the thing, I managed to simply cut out the sleep()'s. But here's the two methods that SHOULD be called every .1 seconds, yet only get called at project launch.

#1 is the update() method in Game

	public void update() {
		
		System.out.println("This is only getting called once, even after both if statements have been completed");
		
		//Player1's turn
		if (turn == 1) {
			player1.update();
			if (midTurn == false) {
				System.out.println("Player 1's turn.");
				player1.takeTurn();
				midTurn = true;
				checkForWin();
				takeBreak();
			}
		}
		
		//Player2's turn
		else if (turn == 2) {
			player2.update();
			if (midTurn == false) {
				System.out.println("Player 2's turn.");
				player2.takeTurn();
				midTurn = true;
				checkForWin();
				takeBreak();
			}
		}
		
		
	}



#2 is the Draw method in Board

public void draw(Graphics2D g2d) {//This for-loop will program one row by adding all Tile objects to tileList
		System.err.println("Should be called repeatedly; only fires off once");
		
		for (int i = 0; i < (boardDimension * boardDimension); i++) {
			tileList.get(i).draw(g2d);
		}
		
		
	}


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 06:59 PM

You showed your two methods but not how:
- to setup the Timer to fire and actionEvent
- how the actionPerformed() method calls these two method
- how turn == 2 is reset to 1
Was This Post Helpful? 1
  • +
  • -

#11 MMBurma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-August 11

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 07:13 PM

View Postpbl, on 04 June 2012 - 06:59 PM, said:

You showed your two methods but not how:
- to setup the Timer to fire and actionEvent
- how the actionPerformed() method calls these two method
- how turn == 2 is reset to 1


Sorry, here's the original trigger built into the framework itself; I didn't design it, and my friend who did said to leave it be. He's shown me projects with it working, so not sure why it won't for mine; it appears the turn mechanism in update has something to do with it.

Here's the Run (trigger) class, which DOES use sleep() as I didn't build it:

package Engine;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import Game.GameLoop;

public class Run extends JPanel implements Runnable {

	GameLoop gl;

	public Run(GameLoop gl) {
		setFocusable(true); // LET IT BE FOCUSABLE FOR KEYPRESSES

		if (Handler.getKeyListener() != null) // ADD KEY LISTENER IF SET
			addKeyListener(Handler.getKeyListener());

		if (Handler.getMouseListener() != null) // ADD MOUSE LISTENER IF SET
			addMouseListener(Handler.getMouseListener());

		this.gl = gl; // SET GL TO THE GAMELOOP FROM MAIN
		gl.Load(); // LOAD ASSESTS
	}

	@Override
	public void run() {
		while (true) {
			gl.Update(); // UPDATE EVERY SECOND
			repaint(); // REPAINT EVERY SECOND
			try {
				Thread.currentThread().sleep(10);	//	SLEEP
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void paint(Graphics g) {
		super.paint(g); // CALL SUPER CLASS
		Graphics2D g2d = (Graphics2D) g; // CAST GRAPHICS2D
		
		gl.Draw(g2d); // CALL DRAW METHOD FROM GAMELOOP
	}
}



The above update() method calls the update method in GameLoop as seen previously, which then calls the update() methods in Game and Board via their objects. The repaint method is then responsible for calling the draw method in GameLoop, which then calls the draw method in Board.

Turn is reset via this Player class, which has been ported to pvp rather than pvc since last post.
*Note the segment calling gameloop.game.setTurn uses a variable specific to each player to determine the turn to change to, as it is the opposite player's turn/value.

package Game;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.Timer;

import Engine.Handler;
import Engine.KeyAdapt;
import Engine.MouseAdapt;

public class Player {
	
	private int mouseX = -1, mouseY = -1, myStatus, enemyStatus;
	private static int sleepTime = 1000, tileSize = 50;
	private boolean duringTurn, clickedNewPoint, isTurnFinished;
	
	GameLoop gameloop;
	
	public Player(GameLoop gl, int playerStatus) {
		isTurnFinished = false;
		duringTurn = false;
		clickedNewPoint = false;
		gameloop = gl;
		myStatus = playerStatus;
		
		if (myStatus == 1)
			enemyStatus = 2;
		else if (myStatus == 2)
			enemyStatus = 1;
		else
			System.err.println("Error: Misnumbered player status(es).");
		
		mouseX = -1;
		mouseY = -1;
	}
	
	public void takeTurn() {
		System.out.println("Turn has begun.");
		System.out.println("myStatus = " + myStatus + " and enemyStatus = " + enemyStatus);
		
		mouseX = -1;
		mouseY = -1;
		
		isTurnFinished = false;
		duringTurn = true;
		
		//Keep waiting until player clicks a point
		while (clickedNewPoint == false) {
			update();
		}
		
		if (clickedNewPoint == true && duringTurn == true) {
			
			clickedNewPoint = false;
			duringTurn = false;
			
			System.out.println("MouseX = " + mouseX + ", MouseY = " + mouseY);
			
			//Converting mouseX and mouseY to corresponding tile locations on the grid
			int x = (int) mouseX / tileSize;
			int y = (int) mouseY / tileSize;
			
			//Reset mouse values
			mouseX = -1;
			mouseY = -1;
			
			//Create move, check validity
			Move thisMove = new Move(gameloop, x, y, myStatus);
			
			if (thisMove.isValid() == true) {
				thisMove.makeMove();
				System.out.println("Finished move.");
				gameloop.game.setTurn(enemyStatus, false);
			}
			else {
				clickedNewPoint = false;
				takeTurn();
			}
		}
			
		
	}
	
	public int getStatus() {
		return myStatus;
	}

	public void update() {
		if (MouseAdapt.isMouseClicked() == true && MouseAdapt.isMouseReleased() == true) {
			
			System.out.println("Mouse location is (" + MouseAdapt.getMouseX() + ", " + MouseAdapt.getMouseY() + ")");
			
			if (duringTurn == true) {
				mouseX = MouseAdapt.getMouseX();
				mouseY = MouseAdapt.getMouseY();
				System.out.println("MousePoint is now: (" + mouseX + ", " + mouseY + ")");
				clickedNewPoint = true;
			}
		}
	}
}


That Run class may be the root of the problem; it doesn't seem to go any deeper, as that is where the continuously spaced method calls begin.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 07:19 PM

You haven't implement the javax.swing.Timer
Was This Post Helpful? 0
  • +
  • -

#13 MMBurma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-August 11

Re: Issues with calling draw method (10 times/second)

Posted 04 June 2012 - 07:55 PM

View Postpbl, on 04 June 2012 - 07:19 PM, said:

You haven't implement the javax.swing.Timer


I understand what you meant before now, and have fully replaced the previous sleep in the framework Run class with a timer. However, the exact same problem occurs: only one time are the methods called.

I modified the Handler class in the framework to perform as ActionListener. It essentially calls the run() method in Run again, but the timer event isn't appearing to work correctly. Here are the newly modified timer/event classes:

Handler (I only added a method to this at the bottom, the rest is generic handler material):

package Engine;

//import MouseAdapt;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;

import javax.swing.ImageIcon;


public class Handler implements ActionListener {

	Run run;
	
	private static int WindowX = 800;
	private static int WindowY = 600;

	private static String title = "Game";

	private static KeyAdapter keyAdapt = new KeyAdapt();
	private static MouseAdapter mouseAdapt = new MouseAdapt();
	
	private static boolean resizable = false;

	public static void setDim(int x, int y) {
		WindowX = x;
		WindowY = y;
	}

	public static void setTitle(String newTitle) {
		title = newTitle;
	}

	public static KeyAdapter getKeyListener() {
		return keyAdapt;
	}

	public static MouseAdapter getMouseListener() {
		return mouseAdapt;
	}

	public static String getTitle() {
		return title;
	}

	public static int getWindowX() {
		return WindowX;
	}

	public static int getWindowY() {
		return WindowY;
	}
	
	public static Image loadImage(String path) {
		ImageIcon i = new ImageIcon(path);
		return i.getImage();
	}
	
	public static void setResizable(boolean set) {
		resizable = set;
	}
	
	public static boolean getResizable() {
		return resizable;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		run.run();
		System.out.println("Timer event performed");
	}
	
	public void inheritRun(Run runner) {
		run = runner;
		System.out.println("Inherited Run");
	}
	
	
}




Run:

package Engine;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;

import javax.swing.JPanel;
import javax.swing.Timer;

import Game.GameLoop;

public class Run extends JPanel implements Runnable {

	GameLoop gl;
	private static int delay = 10;
	Handler taskPerformer = new Handler();
	Timer timer = new Timer(delay, taskPerformer);
	
	public Run(GameLoop gl) {
		setFocusable(true); // LET IT BE FOCUSABLE FOR KEYPRESSES

		if (Handler.getKeyListener() != null) // ADD KEY LISTENER IF SET
			addKeyListener(Handler.getKeyListener());

		if (Handler.getMouseListener() != null) // ADD MOUSE LISTENER IF SET
			addMouseListener(Handler.getMouseListener());

		this.gl = gl; // SET GL TO THE GAMELOOP FROM MAIN
		gl.Load(); // LOAD ASSESTS
		taskPerformer.inheritRun(this);
	}

	@Override
	public void run() {
		System.out.println("Nyan");
		gl.Update(); // UPDATE EVERY SECOND
		repaint(); // REPAINT EVERY SECOND
		
		new Timer(delay, taskPerformer).start();
	}

	public void paint(Graphics g) {
		super.paint(g); // CALL SUPER CLASS
		Graphics2D g2d = (Graphics2D) g; // CAST GRAPHICS2D
		
		gl.Draw(g2d); // CALL DRAW METHOD FROM GAMELOOP
	}
}



Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Issues with calling draw method (10 times/second)

Posted 05 June 2012 - 06:34 AM

Gheez your code is complicated for nothing
You defined Timer variables that you never use
In your run() method to create and start() a new Timer at each call... how many Timer running do you think you will be ending having ?
As a CRT screen is updated every 16.6 millisecond any Timer that calls repaint() more often than evert 17 or 18 milliseconds is just a resource waste
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1