Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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