• 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
Patriarch K

PaintComponent runs slow and flickers in Java

9 posts in this topic

I have a game which is painting a lot of gifs and images everywhere and it runs a little bit slow meanwhile. Sometimes it can run very good for a minute or something, but then it starts to lag and stuff. Most of the images flicker from time to time. Not extremely much, but they still flicker a little bit.
I will shoe the relevant parts of my code and maybe you can find what I need to do. I have googled and seen things with dubble buffering and stuff, but I don't really get it.

The code here is just a lot of stuff, I have removed a lot of "game logic" and image files so the names are not the same, but you will at least get my method with this code:


[CODE]
public class Game3 extends JPanel implements ActionListener{

ImageIcon flowerIcon = new ImageIcon("img\\Dragon.gif");
Image flower = flowerIcon.getImage();
ImageIcon flowerhitIcon = new ImageIcon("img\\DragonHit.gif");
Image flowerhit = flowerhitIcon.getImage();
ImageIcon flowerfireIcon = new ImageIcon("img\\DragonFire.gif");
Image flowerfire = flowerfireIcon.getImage();

URL music = null;
AudioClip musicSound;
URL pop = null;
AudioClip popSound;
Font font;
Random r;
boolean skott = false;
Nalle nalle = new Nalle();
ArrayList<Bubble> bubblor = new ArrayList<Bubble>();
Timer tim;

Game3(){
try {
wee = new URL("file:img\\wee.wav");
}
catch (MalformedURLException exp) {
}
weeSound = Applet.newAudioClip(wee);
try {
namnam = new URL("file:img\\namnamn.wav");
}
catch (MalformedURLException exp) {
}
namnamSound = Applet.newAudioClip(namnam);
bubbles = Integer.parseInt(textBubbles);
tim = new Timer(20, this);
setFocusable(true);
addKeyListener(k);
musicSound.loop();
tim.start();
}
}

public void checkCollisions(){
blablabla
}

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(Map.redLevel == true){
g2d.drawImage(background, himmelX, himmelY, null);
g2d.drawImage(background2, himmelX2, himmelY2, null);
g2d.drawImage(lava, grassX+80, grassY+355, null);
g2d.drawImage(lava, grassX+680, grassY+130, null);
repaint();
}
}

@Override
public void actionPerformed(ActionEvent arg0) {
blablabla....
}
[/CODE]
0

Share this post


Link to post
Share on other sites
Are you regulating the FPS (Frame per seconds) of your game loop ? You do not need to draw constantly the images because it will just provoke a huge lag. You can simply draw your images let's say 25 times per seconds. I don't think you should override the PaintComponent method to draw all your images as you can't control when this method is called.

I think you should try going with Slick2D API, it provides everything you need for your game and it's pretty simple to use. It's not a good idea to use Swing as you do for a game as Swing is slow and is not made for games, it's made for interface such as menus.

Slick2D will provide you a game engine which will regulate the FPS and a Graphic object where you can draw your images.
0

Share this post


Link to post
Share on other sites
Normally, you will repaint them but only a fixed number of times in a second. This way you can control the speed of the game. But first, I think you really shouldn't use Swing to make a game, you will get other performances issues like the one you have.
0

Share this post


Link to post
Share on other sites
You can easily switch to Slick 2D it shouldn't be hard to do so. This is an example of a simple game application using Slick.

[CODE]
package slick.path2glory.SimpleGame;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class SimpleGame extends BasicGame{

public SimpleGame()
{
super("Slick2DPath2Glory - SimpleGame");
}

@Override
public void init(GameContainer gc)
throws SlickException {
//this is where you will load your images
}

@Override
public void update(GameContainer gc, int delta)
throws SlickException
{
//this is where you will control the game AI
}

public void render(GameContainer gc, Graphics g)
throws SlickException
{
//This is where you can draw your images in the provided Graphics
}

public static void main(String[] args)
throws SlickException
{
AppGameContainer app =
new AppGameContainer(new SimpleGame());

app.setDisplayMode(800, 600, false);
app.start();
}
}
[/CODE]

As you can see it's pretty much the same concept that you are using for drawing, but the API provides a game container which is a game loop in a independant window.
0

Share this post


Link to post
Share on other sites
Well, in my case then I rely on the timer which is 20 frames per millisecond. If I make a separate update mechanism, wouldn't like 100 or something be a little bit too slow?
My game is very soon finished and it works fine as it is at the moment, but not perfect - that's why I made this thread. I will try the SLIK on my next game!
0

Share this post


Link to post
Share on other sites
For the timer you can try differents values to test the stability of your games but as I said 25 frames per [b]second[/b] should be enough.

And is the repaint() method call is really necessary in your paintComponent method ?
[CODE]
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(Map.redLevel == true){
g2d.drawImage(background, himmelX, himmelY, null);
g2d.drawImage(background2, himmelX2, himmelY2, null);
g2d.drawImage(lava, grassX+80, grassY+355, null);
g2d.drawImage(lava, grassX+680, grassY+130, null);
repaint();
}
}
[/CODE] Edited by PsychotikRabbit
0

Share this post


Link to post
Share on other sites
I just had a look at an old game I made and I did the following for double buffering. I can't remember why but I just played it and it runs fine.

[code]
panel.setDoubleBuffered(true);
RepaintManager.currentManager(frame).setDoubleBufferingEnabled(true);
[/code]

Where panel is my extended JPanel with the overriden paintComponent() method (I saw that I'm not calling repaint() at the end of my paintComponent() method, which supports PsychotikRabbit's suggestion).
Where frame is a JFrame.

Hope this works for you too.

Matt Edited by de_mattT
0

Share this post


Link to post
Share on other sites
It's been a while since I last did graphics in native Java, but from the looks of it, you're not using active rendering. This article helped me a lot, when I got tired of the slow repaint() calls: http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418
0

Share this post


Link to post
Share on other sites
I have 66 frames per second without any problems in Swing. Edited by P0jahn
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