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]
# 1
I don't know what's going on in your code: x versus n, while loop etc... but where do you invoke repaint?
DrLaszloJamfa at 2007-7-9 0:10:32 > top of Java-index,Java Essentials,Java Programming...
# 2
Am I missing something terribly obvious, or are you not actually changing n anywhere?
DavidKNa at 2007-7-9 0:10:32 > top of Java-index,Java Essentials,Java Programming...
# 3

You're changing the state of the object, but I see no evidence that you're redisplaying anything.

Why have a loop here? It's not going to animate anything, if that's what you're trying to do.From the point of view of an animation, you're changing the state just once, but really inefficientlly.

Are you aware that if both x and acceleration are positive or zero, then this loop will not terminate?

paulcwa at 2007-7-9 0:10:32 > top of Java-index,Java Essentials,Java Programming...
# 4
Uh, never actually. I just figure I could redraw the rectangle and all I need to use is javax.swing and java.awt.I suppose I don't need n. I just forgot to delete that line.Message was edited by: t1m3r
t1m3ra at 2007-7-9 0:10:32 > top of Java-index,Java Essentials,Java Programming...
# 5
Would it help to supply all of the code for all three classes I have?
t1m3ra at 2007-7-9 0:10:32 > top of Java-index,Java Essentials,Java Programming...
# 6
> The method will take a change in x as a parameter You might want to do this as well.
floundera at 2007-7-9 0:10:32 > top of Java-index,Java Essentials,Java Programming...
# 7
So, you want me to post the code as well as an explanation?
t1m3ra at 2007-7-9 0:10:32 > top of Java-index,Java Essentials,Java Programming...
# 8
Let me set the scene: you would like some help or advice from forum members. The forum members have no idea what you are doing: what your goal is, what the specification to the problem is, etc.. What would be a reasonable thing to do now?
DrLaszloJamfa at 2007-7-9 0:10:32 > top of Java-index,Java Essentials,Java Programming...
# 9

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

t1m3ra at 2007-7-9 0:10:32 > top of Java-index,Java Essentials,Java Programming...
# 10

Perhaps the first step is not to worry about move(), velocity, accel, etc... but just get a block drawn on your custom component, because right now I don't see any connection between Block and Sim, or am I missing something.

Start small. Leave out user input and movement until after you get something simpler working.

DrLaszloJamfa at 2007-7-9 0:10:33 > top of Java-index,Java Essentials,Java Programming...
# 11

Oh, yeah, and I compile it as a project (on my Linux machine "javac <dir>" and on the Windows machine I just use JCreator to compile it as a project). If you're wondering how it's implemented, in Model.java, the Model adds the Sim. I tried to do it in the same class Model, originally, but it didn't work out.

t1m3ra at 2007-7-9 0:10:33 > top of Java-index,Java Essentials,Java Programming...
# 12

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.

t1m3ra at 2007-7-9 0:10:33 > top of Java-index,Java Essentials,Java Programming...
# 13
When you get the "cannot find Symbol" error for an API class (like Font, for example), it means you do not have the proper import statements at the top of your file. For example:import java.util.*;
DrLaszloJamfa at 2007-7-9 0:10:33 > top of Java-index,Java Essentials,Java Programming...
# 14

I received fewer errors after importing javax.swing, java.awt.*, and java.swing, and java.lang.*...

E:\dir\Animation.java:5: cannot find symbol

symbol: class Applet

public class Animation extends Applet implements Runnable

^

E:\dir\Animation.java:12: cannot find symbol

symbol : constructor Thread(Animation)

location: class java.lang.Thread

th = new Thread(this);

^

E:\dir\Animation.java:29: cannot find symbol

symbol : method repaint()

location: class Animation

repaint();

^

3 errors

Process completed.

I'm sorry I forgot to mention: I found the code at http://forum.java.sun.com/thread.jspa?threadID=768791

I also think it's a little odd that there's no main method. I thought it was mandatory to have a main method is some class.

t1m3ra at 2007-7-9 0:10:33 > top of Java-index,Java Essentials,Java Programming...
# 15
The main is buried in the applet plug in. So it's there, but you don't write it. It's the same story with web applications and servlets.
DrLaszloJamfa at 2007-7-21 17:11:15 > top of Java-index,Java Essentials,Java Programming...