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);
}
}