Hello, I'm writing a small game for a class. The problem is when i run it it starts consuming the computers resources, i bet i've done something very wrong but i just can't find it (haven't even added bouncing back from the paddle to the code, or game over for that matter)

4 classes


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Mang extends JPanel implements MouseMotionListener, ActionListener {
	Reket alus=new Reket();
	Pall Pallike=new Pall();
	Timer t=new Timer(100, this);
	int abiX=180;
	int abiY=getHeight()-50;
	public Mang(){
		alus.muudaAsukoht(abiX, abiY);
		setLayout(new BorderLayout());
	public void paintComponent(Graphics g){
	public void actionPerformed(ActionEvent e){
	public int aknaLaius(){
		int xlaius = getWidth()-30;
		return xlaius;
	public void mouseMoved(MouseEvent e){
		alus.muudaAsukoht(abiX, abiY);
	public void mouseDragged(MouseEvent e){}
	public static void main(String[] arg){
		JFrame f=new JFrame("Pongimäng");
		f.add(new Mang());


import java.awt.*;
public class Reket extends Kujund{
int lai=40;
int paks=5;
public void joonista(Graphics g){
	g.fillRect(kysiX()-(lai/2), kysiY(), lai, paks);


import java.awt.*;
abstract class Kujund {
	double x=180;
	double y=512;
	public abstract void joonista(Graphics g);
	public void muudaAsukoht(int ux, int uy){
	public int kysiX(){return (int)x;}
	public int kysiY(){return (int)y;}


import java.awt.*;

public class Pall{
	int r=5;
	int ringx=100;
	int ringy=100;
	int xsamm=2;
	int ysamm=3;
	public void joonista(Graphics g){
		g.fillOval(ringx-r, ringy-r, 2*r, 2*r);	
	public void liigutaPalli(){
		Mang pongiObject = new Mang();
		int akenlai = pongiObject.aknaLaius();
		if(xsamm+ringx+r<=akenlai & xsamm+ringx+r >= 0){

Mang is the main program, Reket is the paddle (and it uses Kujund) and Pall is the Ball.
Before i added the ball the paddle moved smoothly, afterwards every second that passes, the ball and the paddle start moving more slowly. I appreciate any help i can get =)

Recommended Answers

How many Mang objects does the code create?
How many should it create?

Add a println in the Mang constructor to print a message.

Jump to Post

All 8 Replies

How many Mang objects does the code create?
How many should it create?

Add a println in the Mang constructor to print a message.

Put a println, first time it printed 1 line, the second time timer passed it was 2, then 4, 8, 16 etc....now...how do i fix it?

How many Mang objects should the program create?

I don't know that much about Java, but i would imagine 1?

Then look at your code and see where there are new instances of the Mang class being created and remove all of them that are extra and not needed.

Can I get a hint? As i said, i don't know that much about Java...

Who wrote the code that is posted? Ask that person how to create a new instance of a class.
Hint: use the new statement. It is used over 6 times in the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.