• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
xDancinpoptartx

Need java Timer code help.

4 posts in this topic

Why wont my timer work?[CODE]
import javax.swing.JPanel;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;


public class Board extends JPanel implements ActionListener {

Image BR;
Charecter Char;
Timer time;
int DELAY = 5;

public Board(){
Char = new Charecter();
ImageIcon ii = new ImageIcon(this.getClass().getResource("/Resources/BackRoads.jpg"));
BR = ii.getImage();
time = new Timer(5, this);
time.start();

}


class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
repaint();
}


}


public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(BR, 0, 0, null);

}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

}
[/CODE]
0

Share this post


Link to post
Share on other sites
1. Wrong forum ...this belongs to "General Programming / For Beginners" at best or a Java forum on the web

2. Look at the javadoc: [url="http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html"]http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html[/url]

3. "time = new Timer(5, this);" should not even compile, cfr javadoc

4. Using a thread would probably be better and simpler

5. Do you need to refresh the drawing at all?! This is necessary only if you have sprites! For static pawns, you can just update you screen once the player moved something.

6, Refreshing every 5 millisecond is a lot for a boardgame!


Nevertheless, using a timer, your board should implement "TimerTask", and use the timer that way;


[CODE]
public Board(){
...
timer = new Timer(true);
timer.schedule(this, 0, 50);
}

public void run() {
repaint();
}
[/CODE]
0

Share this post


Link to post
Share on other sites
Actually, there are two Timer classes in the JDK. You and him are using two different classes. The code the OP isusing is correct for the Timer class he is using. With that said, he already posted this in another thread in this forum, at which I answered his question.
0

Share this post


Link to post
Share on other sites
[CODE]
public class Board extends JPanel implements ActionListener {
Image BR;
Charecter Char;
Timer time;
int DELAY = 5;

public Board(){
Char = new Charecter();
ImageIcon ii = new ImageIcon(this.getClass().getResource("/Resources/BackRoads.jpg"));
BR = ii.getImage();
time = new Timer(5, this);
time.start();

}


public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(BR, 0, 0, null);

}

public void actionPerformed(ActionEvent e) {
repaint();

}
}

[/CODE]



You did not need the unnecessary sub class. but if you did want to use it then you should have put this in the Timer constructor:

time = new Timer(5,new TimerListener());

putting the this keyword means that nothing will be rendered due to the fact that in your actionPerformed method in the Board class was empty.

Hope it helped

Gen.
0

Share this post


Link to post
Share on other sites
[quote name='jonbonazza' timestamp='1326912271' post='4904033']
Actually, there are two Timer classes in the JDK. You and him are using two different classes. The code the OP isusing is correct for the Timer class he is using. With that said, he already posted this in another thread in this forum, at which I answered his question.
[/quote]

No he is not using his Timer class correctly, and he should read the Java Doc.
He imported
[CODE]
import java.util.Timer;
[/CODE]
this the one he is using.
[url="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html"]http://docs.oracle.c...util/Timer.html[/url]
He cannot use it in that way.

You are thinking of javax.swing.Timer which he might want to use but is not using.
[url="http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html"]http://docs.oracle.c...wing/Timer.html[/url]

He will get a compile error with his code because of an Invalid Constructor call.

[quote name='CryoGenesis' timestamp='1326919674' post='4904073']
You did not need the unnecessary sub class. but if you did want to use it then you should have put this in the Timer constructor:

time = new Timer(5,new TimerListener());

putting the this keyword means that nothing will be rendered due to the fact that in your actionPerformed method in the Board class was empty.

Hope it helped

Gen.
[/quote]

You should actually avoid using 'this' in constructors because it is one of the conditions that can cause Java to leak because it's not fully initialized so it is best to get out of that bad habit. A subclass is a good way to avoid it, but a Factory is better but well beyond what he's doing. However, you're right he used the subclass incorrectly.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0