Fluidity Of The Simple Pong game

Hello everyone,
Im pretty new at this game development but familiar with programming. I had some experience with java also. Watching tutorials in the internet, im getting stucked with this rendering point of the simple 2D games. I use actionListener in the main class, attached it with a timer (0 second delay) and in the actionPerformed(ActionEvent e) method, i first calculate the balls position, collision or the speed (mainly game pyhsics) and after those i call the repaint(Graphics g) method. In this method i draw the background first, then i draw the ball and the paddles.

The problem is, im using images to have a better visualization. But when i create a JFrame which is 1280x760, the game starts not to move smoothly. When i cancel the background drawing, it starts to get far more better. But i still don’t get that there are much more bigger games which are working pretty smooth in this computer like CoD or Assasins Creed, this simple background slows my game. What should i do to fix it? What if i got 100 balls to move in this pong game? Or what if this background has to change in every frame?

Can someone please guide me? Im totally lost what to do. Looked for lwjgl or generally OpenGL but it looks pretty complicated to start. I mean im trying to make simple games like pong, snake or flappy bird. But i really hate rectangles, just wanna use images or sprites instead of them. And now stuck with it…

SOLVED: The problem is this line of code in the repaint method

 g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
		 RenderingHints.VALUE_INTERPOLATION_BILINEAR);

when i disable it, the animation is perfect!

Can you post your code, preferably as an MCVE?

This is the actionPerformed method

@Override
	public void actionPerformed(ActionEvent e) {
		
		if (ball.y <= 0 || ball.y + ball.height + 35 > HEIGHT) { // Horizontal
			// Walls
			// collision
			ballspeedY = -ballspeedY;
		}
		if (ball.x <= 0 || ball.x + ball.width + 5 >= WIDTH) { // Vertical Walls  collision
																
			ballspeedX = -ballspeedX;
		}
		ball.y += ballspeedY;
		ball.x += ballspeedX;
		
		
		renderer.repaint();
	}

And this one is the repaint:

	
   public void repaint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		
		g2d.drawImage(backgroundImage, 0, 0, WIDTH, HEIGHT, null);
		g2d.drawImage(ball.image, ball.x, ball.y, ball.width, ball.height, null);

	}

I had similar issues with my game project, despite rendering to 320x200, performance was atrocious, no matter how much I tried to force Java2d to use the GPU (using volatile images, setting environment settings and so forth).

So I finally bit the bullet and made the move to a proper graphics framework (LWJGL in my case).

But I guess people with more knowledge will chip in soon to give you better pointers (as they did with me). :slight_smile:

Make sure you’re only loading/creating the pictures once. One time I was starting a game and kept running out of memory, and then realized I stupidly was loading all the pictures in the run method which was making new pictures every frame. Do the same with all your objects and make sure they are only being created once. Also you could try increasing the frame rate?

Im sure that im loading the images for once. Somethings wrong with the background. I tried not to draw background but draw 1000 hundred balls on the screen, its working pretty nice. Somethin is just wrong about the background drawing

How big is the background image? A huge image could slow it down.