Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

need java help please


  • Please log in to reply

#1
lostman

lostman

    New Member

  • Member
  • Pip
  • 1 posts
i've done the layout but i just can't figure out the events for each button.
the lecturer is no help.

this is the assignment page

http://194.81.104.27...csy1020Ass2.pdf


i need to make the buttons in the grid to form a letter M shape by using forward button and Rotate button.

this is my code so far and i haven't got much time left. i'm sorry if the code is messy but it works so far...

thank you

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class Robot extends JFrame implements ActionListener
{
private JButton[][] b = new JButton[20][20];
private JButton east, east2, east3, east4;
private JLabel square1Label, direction1Label;
private JTextField squareField, directionField;
int startPosition = 9;


public Robot()
{
super ("M shape Program");
Container yourContainer;
yourContainer = getContentPane();
yourContainer.setLayout (new BorderLayout());


east = new JButton("Forward");
east2 = new JButton("Roatate");
east3 = new JButton("Clear");
east4 = new JButton("Exit");

east.addActionListener(this);
east2.addActionListener(this);
east3.addActionListener(this);
east4.addActionListener(this);

JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(10,18,5,4));

eastPanel.add(east);
eastPanel.add(east2);
eastPanel.add(east3);
eastPanel.add(east4);
yourContainer.add(eastPanel, BorderLayout.EAST);


JPanel centerPanel = new JPanel ();
centerPanel.setLayout(new GridLayout(10,10));



for(int y = 0;y < 10;y++)
{
for(int x = 0;x < 10;x++)
{
b[x][y] = new JButton("");
centerPanel.add(b[x][y]);
b[x][y].setSize(200,400);
b[x][y].setBackground(Color.green);

b[x][y].addActionListener(this);



}
}
b[0][startPosition].setBackground(Color.black);
yourContainer.add(centerPanel, BorderLayout.CENTER);


JPanel southPanel = new JPanel ();
square1Label = new JLabel("Square: ");
southPanel.add(square1Label);

squareField = new JTextField(2);
southPanel.add(squareField);

direction1Label = new JLabel("Direction: ");
southPanel.add(direction1Label);

directionField = new JTextField(2);
southPanel.add(directionField);


yourContainer.add(southPanel, BorderLayout.SOUTH);

pack();
setSize(500, 400);
setVisible(true); }


public void actionPerformed(ActionEvent event){

String actioncommand = event.getActionCommand();
if (actioncommand.compareTo("Exit") == 0)
System.exit(0);




if (event.getSource() == east)
{
startPosition=startPosition-1;
b[0][startPosition].setBackground(Color.black);

}



}
}

public static void main (String[] args)
{
Robot test = new Robot();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP