Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 08 Sep 2013
Offline Last Active Oct 14 2013 04:58 AM

Posts I've Made

In Topic: Pong game - Computer AI and criticism

20 September 2013 - 06:38 PM

Oh, I am familiar with packages, I'm just not writing space invaders, only extracting the drawing method presented there (accelerated graphics) 


Thanks, as always !

In Topic: Pong game - Computer AI and criticism

20 September 2013 - 04:57 AM

 Hey, quick question - here are the relevant classes:

Sprite ( a wrapper for image):


import java.awt.Graphics;
import java.awt.Image;

public class Sprite {
	private Image image;
	public Sprite(Image image) {
		this.image = image;
	public int getWidth() {
		return image.getWidth(null);
	public int getHeight() {
		return image.getHeight(null);
	public void draw(Graphics g, int x, int y) {
		g.drawImage(image, x, y, null);

and SpriteStore, for efficient handling of sprites, etc:


import java.util.HashMap;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.awt.GraphicsConfiguration;
import java.awt.Transparency;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteStore {
	// singleton logic
	private static SpriteStore single;
	public static SpriteStore get() {
		return single;
	private HashMap sprites = new HashMap();
	public Sprite getSprite(String ref) {
		if(sprites.get(ref) != null)
			return (Sprite)sprites.get(ref);
		// loader
		BufferedImage sourceImage = null;
		try {
			URL url = this.getClass().getClassLoader().getResource(ref);
			if(url == null)
				fail("Can't find that :( " + ref);
			sourceImage = ImageIO.read(url);			
		} catch(IOException e) {
			fail("Failed to load :( " + ref);
		// accelerated graphics memory allocation
		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		Image image = gc.createCompatibleImage(sourceImage.getWidth(), sourceImage.getHeight(), Transparency.BITMASK);
		Sprite sprite = new Sprite(image);
		sprites.put(ref, sprite);
		return sprite;
	private void fail(String msg) {

Everything is well understood except one thing - where exactly do I put my .jpg and .gif files in order to load them? (if it matters, I'm using Eclipse); which path (String ref) do I use in that case? 
Thanks! biggrin.png

In Topic: Pong game - Computer AI and criticism

15 September 2013 - 02:25 PM

Thanks a lot! I will definitely check that out! 

In Topic: Pong game - Computer AI and criticism

14 September 2013 - 12:10 PM

Thanks for the tips. Interesting point about paint method being slow, will improve.

Thanks again! smile.png



hey - can you elaborate the "set ignorepaintpaint in Game constructor to true"? 

In Topic: Pong game - Computer AI and criticism

13 September 2013 - 11:15 AM

Thanks for both replise!


there already is delay at which the computer reacts, it sleeps for difficulty (a variable) seconds before it 'looks' again and attempts to reposition the paddle. Currently, the computer is blind for 40ms between each reposition. 



the screen is regenerated every 15ms, which gives me the frame rate of 1000/15 =~ 66 FPS. 40ms is, I believe, an equivalent of skipping 2,something frames. Thats not much :/