Help Needed For Algorithm Assignment?
Hi there! Well I'm quite new to this, therfore I would greatly appreciated if some1 could help me with my assignment.
Assignment:
Make an applet that makes an object(red oval) move around the applet window by using only four buttons(Up, Right, Down, Left).
I managed to do the button and the oval but both are not connected.
Can some1 help me with the codes coz my assignment is due this week!!
[427 byte] By [
Maudehsera] at [2007-9-28 16:10:43]

This isn't really owt to do with algorithms, but never mind. Please post the code which draws the oval.
Sorry, please also tell us how you would change things to get the oval to move in each direction.
I don't uderstand wat u meant by "how you would change things to get the oval to move in each direction."
I was given a piece of paper with the assignment written on it and also the picture of what the outcome of it....We were supposed to write our own codes.
Here are what I've already done:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Game extends Applet implements ActionListener{
Button up1 = new Button("Up");
Button down1 = new Button("Down");
Button right1 = new Button("Right");
Button left1 = new Button("Left");
public void init() {
Panel p = new Panel(new GridLayout(3, 3));
p.setBackground(Color.white);
p.add(new Label(""));
p.add(up1);
up1.addActionListener(this);
p.add(new Label(""));
p.add(left1);
left1.addActionListener(this);
p.add(new Label(""));
p.add(right1);
right1.addActionListener(this);
p.add(new Label(""));
p.add(down1);
down1.addActionListener(this);
add(p);
}
public void actionPerformed(ActionEvent event) {
repaint();
}
public void paint(Graphics g) {
Font font = new Font("serif", Font.BOLD, 20);
g.setColor(Color.black);
g.setFont(font);
g.drawString("Click on button to move the object.",80,350);
Color c = Color.red;
g.setColor(c);
g.fillOval(200, 150, 155, 95);
}
}
But from what my lecturer said I cannot use fixed variables for the oval. Also from wat you see there are some missing codes coz I do not know what should be written there.
I'm sorry to cause any problems but I really need help...
I don't uderstand wat u meant by "how you would change things to get the oval to move in each direction."
I was given a piece of paper with the assignment written on it and also the picture of what the outcome of it....We were supposed to write our own codes.
Here are what I've already done:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Game extends Applet implements ActionListener{
Button up1 = new Button("Up");
Button down1 = new Button("Down");
Button right1 = new Button("Right");
Button left1 = new Button("Left");
public void init() {
Panel p = new Panel(new GridLayout(3, 3));
p.setBackground(Color.white);
p.add(new Label(""));
p.add(up1);
up1.addActionListener(this);
p.add(new Label(""));
p.add(left1);
left1.addActionListener(this);
p.add(new Label(""));
p.add(right1);
right1.addActionListener(this);
p.add(new Label(""));
p.add(down1);
down1.addActionListener(this);
add(p);
}
public void actionPerformed(ActionEvent event) {
repaint();
}
public void paint(Graphics g) {
Font font = new Font("serif", Font.BOLD, 20);
g.setColor(Color.black);
g.setFont(font);
g.drawString("Click on button to move the object.",80,350);
Color c = Color.red;
g.setColor(c);
g.fillOval(200, 150, 155, 95);
}
}
But from what my lecturer said I cannot use fixed variables for the oval. Also from wat you see there are some missing codes coz I do not know what should be written there.
I'm sorry to cause any problems but I really need help...
So to draw the oval in its initial position you use g.fillOval(200, 150, 155, 95); How would you draw it if I asked you to move it right?
The g.fillOval(200, 150, 155, 95); is not correct. From what my lecturer told me I can't use fixed variables for the oval(x, y,...).He said that I need to figure them out of how to move the oval in tha applet window at the same time maintaining its size. Therefore when u asked me how should I draw when the oval move to the right, I really really don't know...Is there any way that U could help me to solve this problem of mine....
This can help: http://forum.java.sun.com/thread.jsp?forum=406&thread=303906
just a tip.. next time you might want to post simply "help needed to understand algorithm" rather than "help needed for algorithm assingnment". putting in the "assignment" bit will get the backs up on about 80% of the peopel who could probably answer your question. this is just some advice.. feel free to ignore it...
putting in the "assignment" bit will get the backs up on about 80% of the peopel who could probably answer your question.
Actually helping with assignments doesn't bother me, providing they;ve actually made an effort or attempted to do the tast. It is the opposite that irritates me where somebody tries to pretend an obvious assignment is really important 'work'.
Firstly, thanxs 4 the advice...Secondly sori 2 trouble all of u...But guys I'm really lost here, all I want is the codes to make this applet.So I would be glad if sum1 could help out with the codes...
Well it seems that nobody can help me the codes..n I'm running out of time...guess I've myself to blame then....Thanxs alot guys
it's a frame, but i am sure you can change it.
import java.awt.*;
import java.awt.event.*;
public class MoveOval extends Frame implements ActionListener
{
Button bu = new Button("U");
Button bd = new Button("D");
Button bl = new Button("L");
Button br = new Button("R");
Panel ov = new TheOval();
public MoveOval()
{
setLayout(new BorderLayout());
setBounds(10,10,530,430);
init();
setVisible(true);
}
public void init()
{
Panel p1 = new Panel();
p1.setLayout(new GridLayout(0,4,7,7));
p1.add(bu);
p1.add(bd);
p1.add(bl);
p1.add(br);
bu.addActionListener(this);
bd.addActionListener(this);
bl.addActionListener(this);
br.addActionListener(this);
add("South",p1);
Panel p2 = new Panel();
p2.setBackground(Color.yellow);
p2.setLayout(null);
p2.add(ov);
add("Center",p2);
}
public void actionPerformed(ActionEvent ev)
{
if (ev.getSource() == bu) ov.setLocation(ov.getX(),ov.getY()+1);
if (ev.getSource() == bd) ov.setLocation(ov.getX(),ov.getY()-1);
if (ev.getSource() == bl) ov.setLocation(ov.getX()-1,ov.getY());
if (ev.getSource() == br) ov.setLocation(ov.getX()+1,ov.getY());
}
public class TheOval extends Panel
{
public TheOval()
{
setBounds(100,100,55,55);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(1,1,40,50);
}
public void update(Graphics g)
{
paint(g);
}
}
public static void main (String[] args)
{
new MoveOval();
}
}
Noah
Thanxs alot noah.w....I manage to change it into an applet. And I did some editing to it to....Thank u so much!!!