I'm quite new to Java and I have searched the forums and couldn't find the answer to my problem. I just started this program today.
What I am starting is a billiards game with collision and multiple balls. For a start, I am trying to make a ball that lets you change the angle and the power of the ball (the speed). You hit fire to make the ball move.
I know already that I am doing many things wrong. I'm going to implement a timer eventually instead of using the for loop counting to 10000000, and I will try to implement threads to handle multiple animations at once. I also realize that the way I am animating is probably wrong, and I need some explanation about that too.
But right now, my problem is that I can get the ball to move.. but it sort of zig-zags. I'm almost positive it has something to do with how I'm generating the next coordinate based with sin and cos.. it's rounding it, so I think that's why it's zig zagging. How can I prevent this? What am I doing wrong with making the ball move?
And I am wondering about the program as a whole. Right now I have two classes.. a pool class with is the main class that creates a ball at a certain coordinate, and also draws the pool table, and I also have a ball class, which draws the ball and animates it. Would it be easier to just have one class for this, or is two (or even more) better?
Because for animating, right now, I am not repainting the pool table, it is drawing over the old ball with the same color as the background, and then drawing a new one, which is bad, I think. How should I go about doing this?
Thanks in advance for your help, and sorry for all the code.
Here is the pool class:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class Pool extends JApplet
implements ActionListener
{
/////////////////////////
// Preliminary buttons
/////////////////////////
JButton button1, // decrease angle
button2, // increase angle
button3, // decrease power
button4, // increase power
button5; // fire!
double angle = 90; // angle
int fire = 0;
double power = 1; // power
Graphics g;
Ball b = new Ball();
public void init()
{
button1 = new JButton(" +angle ");
button1.setToolTipText("angle");
button1.addActionListener(this);
button2 = new JButton(" -angle ");
button2.setToolTipText("angle");
button2.addActionListener(this);
button3 = new JButton(" +power ");
button3.setToolTipText("power");
button3.addActionListener(this);
button4 = new JButton(" -power ");
button4.setToolTipText("power");
button4.addActionListener(this);
button5 = new JButton(" fire ");
button5.setToolTipText("fire");
button5.addActionListener(this);
JPanel panel = new JPanel();
panel.add(button2);
panel.add(button1);
panel.add(button4);
panel.add(button3);
panel.add(button5);
Container c = getContentPane();
c.setBackground(new Color(100, 70, 0));
c.add(panel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if (button == button1)
angle += 10;
else if (button == button2)
angle -= 10;
else if (button == button3)
power += .5;
else if (button == button4)
{
if (power > 0)
power -= .5;
}
else if (button == button5)
{
fire++;
}
if (angle == 360)
angle = 0;
if (angle == -10)
angle = 350;
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
drawboard(g);
drawball(g);
}
public void drawball(Graphics g)
{
int ypos = b.returnX();
int xpos = b.returnY();
b.drawBall(100, 200, g);
if (fire>0) //draw tree
{
fire=0;
b.moveBall(xpos, ypos, angle, power, g);
}
}
public void drawboard(Graphics g)
{
final int xStep = getWidth()/8;
int y = 580;
int ypos = b.returnX();
int xpos = b.returnY();
g.setColor(new Color(100, 70, 0));
g.fillRect(0,0,500,600);
g.setColor(new Color(50, 200, 0));
g.fillRect(50, 50, 400, 500);
g.setColor(Color.white);
g.drawString(String.valueOf(angle), 2*xStep, y);
g.drawString(String.valueOf(power), 4*xStep+50, y);
g.drawString(String.valueOf(fire), 6*xStep+40, y);
g.drawString(String.valueOf(ypos), xStep - 20, y);
g.drawString(String.valueOf(xpos), 1*xStep + 10, y);
}
}
And the ball class:
class Ball extends JApplet
{
int xpos = 0;
int ypos = 0;
Graphics g;
public void drawBall(int xposin, int yposin, Graphics g)
{
xpos = xposin ;
ypos = yposin;
g.setColor(Color.white);
g.fillOval(xposin, yposin, 20, 20);
}
public void moveBall(int xposin, int yposin, double angle, double power, Graphics g)
{
double radians;
int xReflect = 1;
int yReflect = 1;
xpos = xposin;
ypos = yposin;
while (power > 0)
{
if (xpos > 425)
xReflect = -1;
if (xpos < 55)
xReflect = 1;
if (ypos < 55)
yReflect = -1;
if (ypos > 525)
yReflect = 1;
radians = Math.toRadians(angle);
//newx is found by adding cosine(radians)*distance
int newx= xpos - (int) ((power*Math.cos(radians)*xReflect));
//newy is found by adding sine(radians)*distance
int newy= ypos - (int)((power*Math.sin(radians)*yReflect));
// int newx = (int)(xposin + ((power * Math.cos(angle)) * xReflect));
// int newy = (int)(yposin + ((power * Math.sin(angle)) * yReflect));
g.setColor(new Color(50, 200, 0));
g.fillOval(xpos, ypos, 20, 20);
g.setColor(Color.white);
g.fillOval(newx, newy, 20, 20);
for(int i =0; i < 10000000; i++)
{
}
xpos = newx;
ypos = newy;
power = power - .05;
repaint();
}
}
public int returnX()
{
return ypos;
}
public int returnY()
{
return xpos;
}
public void paint(Graphics g)
{
super.paint(g);
final int xStep = getWidth()/8;
int y = 580;
g.setColor(new Color(100, 70, 0));
g.fillRect(0,0,500,600);
g.setColor(new Color(50, 200, 0));
g.fillRect(50, 50, 400, 500);
}
}