move() method
I have a "Block" class in which I need to have a "move()" method. The method will take achange in x as a parameter from the constructor and redraw the screen. Block is the superclass here (which extends Rectangle2D.Float). It compiles just fine, but the block doesn't move. Here's the code to the method:
publicvoid move()
{
double n = 10.0;
while (x >= 0)
{
x = (int)((x) + (x*acceleration));
super.setRect(n, 10, 10, 20);
}
}
I also triedsuper.reshape(n, 10, 10, 20), but that didn't work either. I invoke it in a class called "Model" and say
b.move();
Anybody have any ideas or know what's wrong?
Thanks! :)
[997 byte] By [
t1m3ra] at [2007-11-26 17:42:21]

Okay, I appologize :( Here's the basic outline of what each class does:
Here's the code for the class Block.java (the superclass):
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
/*
Block.java contains methods and constructors for the block object.
CONSTRUCTOR
Block(mass, acceleration, static, kinetic);
METHODS
doesMove(force);
doesGoForever(kineitic);
setForce(mass, acceleration);
getForce();
setDistance(mass, acceleration, kinetic);
getDistance();
*/
public class Block extends Rectangle2D.Float
{
//VARIABLES
//Constructor-dependent variables:
private double acceleration, time, kinetic;
protected double sttic;
//Other variables:
private double velocity, mass, a, distance, friction, force;
//CONSTRUCTOR
public Block (double m, double a, double sF, double kF, double t)
{
mass = m;
acceleration = a;
sttic = sF;
kinetic = kF;
time = t;
}
//METHODS
/*Calculate and return the force on the block*/
public double getForce()
{
force = mass * acceleration;
return force;
}
//Using the force exerted on the block, compare it to the force of friction. If it's less, then the block does not move.
public boolean doesMove()
{
if (this.getForce() >= (sttic * mass * 9.8 )) return true;
else return false;
}
//If the kinetic friction is zero, then the block goes forever!!!
public boolean doesGoForever()
{
if (kinetic == 0)
return true;
else
return false;
}
//Calculates and returns the distance the block travels when released
public double getDistance ()
{
distance = ((0.5 * (acceleration - (kinetic * 9.8)) * Math.pow(time, 2)));
return distance;
}
//Calculates and returns the initial velocity upon releasing the block
public double getVelocity ()
{
velocity = (acceleration / time);
return velocity;
}
public double getForceFriction()
{
friction = (9.8 * kinetic);
return friction;
}
public void setDistance (double dist)
{
dist = distance;
}
public void setVelocity (double vel)
{
velocity = vel;
}
public void setForceFriction (double ff)
{
ff = friction;
}
public void move()
{
double n = 10.0;
while (x >= 0)
{
x = (int)((x) + (x*acceleration));
super.setRect(n, 10, 10, 20);
}
}
}
Here's the code for Model.java:
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import TerminalIO.*;
import java.applet.*; //*Whew!*
/*
Interface Model: draws the simulation
1.Draw the rectangle.
2.Draw the line.
3.Redraw the rectangle.
*/
public class Model extends JFrame
{
public static void main (String[] args)
{
KeyboardReader reader = new KeyboardReader();
System.out.print("Mass: ");
double mass = reader.readDouble();
System.out.print("Accelleration due to pushing the block: ");
double acceleration = reader.readDouble();
System.out.print("Coefficient of Static Friction: ");
double stic = reader.readDouble();
System.out.print("Coefficient of Kinetic Friction: " );
double kinetic = reader.readDouble();
System.out.print("Time the block is accellerated: ");
double time = reader.readDouble();
Block b = new Block(mass, acceleration, stic, kinetic, time);
String str;
//Boolean expression to check if there is friction
if ((b.doesMove() == false) ||
(b.getDistance() <= 0))
{
str = "The block did not move.\n";
}
else
str = "The block moved with a velocity of " + b.getVelocity() + " m/s \n" +
"for a distance of " + b.getDistance() + " m.";
System.out.println("\nRESULTS:\n" +
"You applied a force of " + b.getForce() +
" Newtons.\n" + str);
Model m = new Model();
b.move();
}
public Model()
{
this.setSize(500, 100);
this.setTitle("THE SIMULATION");
this.setResizable(false);
this.add(new Sim(), BorderLayout.CENTER);
this.setVisible(true);
}
}
Finally, here's the code for Sim.java:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Sim extends JComponent
{
//Parameters for rect
public float yr = 10;//y-value
public float h = 10;//height
public float w = 20;//width
//Parameters for ln
public float xln1 = 0; //x1-value
public float xln2 = 500;//x2-value
public float yln1 = 20;//y1-value
public float yln2 = 20;//y2-value
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Shape rect = new Rectangle2D.Float(10, yr, w, h);
Shape ln = new Line2D.Float(xln1, yln1, xln2, yln2);
g2.setPaint(Color.BLACK);
g2.draw(rect);
g2.setPaint(Color.RED);
g2.draw(ln);
}
public int toInt(float f)
{
String f1 = String.valueOf(f);
return Integer.valueOf(f1);
}
}
The reason why I wasn't sure if I should post all this was I didn't want to do something bad like maybe bog down the server, create a really long thread etc.
In the code, you may not be familiar with KeyboardReader, but it's a little like the Scanner class. I hope this was helpful, and I can explain any reasoning questions you have. Thanks :)
I found another thread on here of someone who has the same problem (generally speaking). I found the code, but I'm not really sure how it fits into the main method. Do I even need one? I tried compiling the code, but I got the following errors:
--Configuration: block - JDK version 1.5.0_05 <Default> - <Default>--
H:\dir\Animation.java:1: cannot find symbol
symbol: class Applet
public class Animation extends Applet implements Runnable
^
H:\dir\Animation.java:3: cannot find symbol
symbol : class Font
location: class Animation
Font f;
^
H:\dir\Animation.java:12: cannot find symbol
symbol : class Graphics
location: class Animation
public void paint(Graphics g)
^
H:\dir]\Animation.java:8: cannot find symbol
symbol : constructor Thread(Animation)
location: class java.lang.Thread
th = new Thread(this);
^
H:\dir\Animation.java:15: cannot find symbol
symbol : variable Color
location: class Animation
g.setColor(Color.red);
^
H:\dir\Animation.java:25: cannot find symbol
symbol : method repaint()
location: class Animation
repaint();
^
6 errors
Process completed.