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

Mang.java

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());
		addMouseMotionListener(this);
		t.start();
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Pallike.joonista(g);
		alus.joonista(g);
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==t){
			Pallike.liigutaPalli();			
		}
		repaint();
	}
	
	public int aknaLaius(){
		int xlaius = getWidth()-30;
		return xlaius;
	}
	
	public void mouseMoved(MouseEvent e){
		abiX=e.getX();
		abiY=getHeight()-50;
		alus.muudaAsukoht(abiX, abiY);
		repaint();
	}
	public void mouseDragged(MouseEvent e){}
	
	
	public static void main(String[] arg){
		JFrame f=new JFrame("Pongimäng");
		f.add(new Mang());
		f.setSize(400,600);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

Reket.java:

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);
}
}

Kujund.java:

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){
	x=ux;
	y=uy;
	}
	public int kysiX(){return (int)x;}
	public int kysiY(){return (int)y;}
}

Pall.java

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){
			ringx+=xsamm;
		}else{
			xsamm=-xsamm;
		}
		if(ysamm+ringy+r>=0){
			ringy+=ysamm;
		}
	}
}

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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.