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 =)