|
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/~gary/csy1020/20052006/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);
}
}
|
|
|
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Robot extends JFrame implements ActionListener
{
private JButton[][] b = new JButton[10][10];
private JTextField squareField, directionField;
Color green;
int[][] ROTATE_0;
int[][] ROTATE_90;
int[][] ROTATE_180;
int[][] ROTATE_270;
int[][] robot;
int[] startPosition;
int[] currentPosition;
int rotationFlag;
final int R_0 = 0;
final int R_90 = 1;
final int R_180 = 2;
final int R_270 = 3;
public Robot()
{
super ("M shape Program");
ROTATE_0 = new int[][]
{
{ 0, 0 }, { -1, -1 }, { -2, -2 }, { -1, 1 }, { -2, 2 },
{ 3, -3 }, { 2, -3 }, { 1, -3 }, { 0, -3 }, { -1, -3 }, { -2, -3 }, { -3, -3 },
{ 3, 3 }, { 2, 3 }, { 1, 3 }, { 0, 3 }, { -1, 3 }, { -2, 3 }, { -3, 3 }
};
ROTATE_90 = new int[][]
{
{ 0, 0 }, { -1, -1 }, { -2, -2 }, { 1, -1 }, { 2, -2 },
{ -3, 3 }, { -3, 2 }, { -3, 1 }, { -3, 0 }, { -3, -1 }, { -3, -2 }, { -3, -3 },
{ 3, 3 }, { 3, 2 }, { 3, 1 }, { 3, 0 }, { 3, -1 }, { 3, -2 }, { 3, -3 }
};
ROTATE_180 = new int[][]
{
{ 0, 0 }, { 1, -1 }, { 2, -2 }, { 1, 1 }, { 2, 2 },
{ 3, -3 }, { 2, -3 }, { 1, -3 }, { 0, -3 }, { -1, -3 }, { -2, -3 }, { -3, -3 },
{ 3, 3 }, { 2, 3 }, { 1, 3 }, { 0, 3 }, { -1, 3 }, { -2, 3 }, { -3, 3 }
};
ROTATE_270 = new int[][]
{
{ 0, 0 }, { -1, 1 }, { -2, 2 }, { 1, 1 }, { 2, 2 },
{ -3, 3 }, { -3, 2 }, { -3, 1 }, { -3, 0 }, { -3, -1 }, { -3, -2 }, { -3, -3 },
{ 3, 3 }, { 3, 2 }, { 3, 1 }, { 3, 0 }, { 3, -1 }, { 3, -2 }, { 3, -3 }
};
// initialize grid control variables
// position of robot center in button grid [y=row, x=col]
robot = ROTATE_0;
startPosition = new int[] { 6, 3 };
currentPosition = new int[2];
setCurrentPosition(startPosition);
rotationFlag = R_0;
green = new Color(0,120,0);
Container yourContainer;
yourContainer = getContentPane();
// yourContainer.setLayout (new BorderLayout()); default layout manager
JButton forward = new JButton("Forward");
JButton rotate = new JButton("Rotate");
JButton clear = new JButton("Clear");
JButton exit = new JButton("Exit");
forward.setActionCommand("forward");
rotate.setActionCommand("rotate");
clear.setActionCommand("clear");
exit.setActionCommand("exit");
forward.addActionListener(this);
rotate.addActionListener(this);
clear.addActionListener(this);
exit.addActionListener(this);
JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8,15,0,15);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
eastPanel.add(forward, gbc);
eastPanel.add(rotate, gbc);
eastPanel.add(clear, gbc);
eastPanel.add(exit, gbc);
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTH;
eastPanel.add(new JLabel(), gbc);
yourContainer.add(eastPanel, BorderLayout.EAST);
JPanel centerPanel = new JPanel ();
centerPanel.setLayout(new GridLayout(10,10));
Color borderColor = UIManager.getColor("Panel.background");
for(int row = 0; row < b.length; row++)
{
for(int col = 0; col < b[0].length; col++)
{
b[row][col] = new JButton("");
centerPanel.add(b[row][col]);
// b[row][col].setSize(200,400); has no affect here
b[row][col].setBackground(green);
b[row][col].setBorder(BorderFactory.createLineBorder(borderColor));
// b[row][col].addActionListener(this);
}
}
// b[0][startPosition].setBackground(Color.black);
yourContainer.add(centerPanel, BorderLayout.CENTER);
JPanel southPanel = new JPanel ();
JLabel square1Label = new JLabel("Square: ");
southPanel.add(square1Label);
squareField = new JTextField(2);
southPanel.add(squareField);
JLabel direction1Label = new JLabel("Direction: ");
southPanel.add(direction1Label);
directionField = new JTextField(3);
southPanel.add(directionField);
yourContainer.add(southPanel, BorderLayout.SOUTH);
// initializeGrid();
resetGrid();
// pack();
setResizable(false);
setSize(500, 450);
setLocation(200,200);
setVisible(true);
}
public void actionPerformed(ActionEvent event){
JButton button = (JButton)event.getSource();
String actionCommand = button.getActionCommand();
System.out.println("actionCommand = " + actionCommand);
if (actionCommand.equalsIgnoreCase("Forward"))
{
currentPosition[1]++;
move(currentPosition);;
}
if (actionCommand.equalsIgnoreCase("Rotate"))
{
rotationFlag++;
if(rotationFlag > R_270)
rotationFlag = R_0;
rotate();
}
if (actionCommand.equalsIgnoreCase("Clear"))
resetGrid();
if (actionCommand.equalsIgnoreCase("Exit"))
System.exit(0);
}
/** clears the grid */
private void clearGrid()
{
// clear grid
for(int y = 0;y < 10;y++)
for(int x = 0;x < 10;x++)
b[x][y].setBackground(green);
}
private void resetGrid()
{
robot = ROTATE_0;
setCurrentPosition(startPosition);
clearGrid();
move(startPosition);
}
private void move(int[] offsetToCenter)
{
clearGrid();
for(int j = 0, row, col; j < robot.length; j++)
{
row = robot[j][0] + offsetToCenter[0];
col = robot[j][1] + offsetToCenter[1];
//System.out.println("row = " + row + "\tcol = " + col);
if(isInBounds(row, col))
b[row][col].setBackground(Color.black);
}
String loc = String.valueOf(currentPosition[0]) + ", " + currentPosition[1];
squareField.setText(loc);
directionField.setText("east");
}
private void rotate()
{
switch(rotationFlag)
{
case R_0:
robot = ROTATE_0;
break;
case R_90:
robot = ROTATE_90;
break;
case R_180:
robot = ROTATE_180;
break;
case R_270:
robot = ROTATE_270;
}
move(currentPosition);
}
private boolean isInBounds(int x, int y)
{
if(x < 0 || x > b[0].length-1 || y < 0 || y > b.length-1)
return false;
return true;
}
private void setCurrentPosition(int[] position)
{
currentPosition[0] = position[0];
currentPosition[1] = position[1];
}
public static void main (String[] args)
{
Robot test = new Robot();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|
|
|
|
|
hi ,
i'm so sorry but its not how its meant to be done my friend..
its meant to appear as a m shape when clicking forward button and rotate button.
the forward button makes buttons on the grid turn black one after another like a robot movement going north, then the rotate button makes buttons on the grid turn black going south east direction then changes to north east direction. then the last part is south direction with the forward button. the direction textfield has to stat the direction you need to go when click forward and rotate like this N,NE,E. the same for square textfield but this stats the number square your on when your clicking forward button and rotate.
it has to form as you see it on my assigment click after click.
i've changed my code to 1 dimentional array because i was told i'm doing it wrong and slightly harder by doing 2 dimentional array. where i have set the start postion is where the the movement needs to start which needs to go north first.
thanks you for the help before hand but i'm really stuck and i have nowhere else to turn to.
i hope this code is ok:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Robot extends JFrame implements ActionListener
{
private JButton[] b = new JButton[100];
private JButton forward, rotate, clear, exit;
private JLabel square1Label, direction1Label;
private JTextField squareField, directionField;
int startPosition = 90;
int squareForward = 80;
int direction;
public Robot()
{
super ("M shape Program");
Container yourContainer;
yourContainer = getContentPane();
yourContainer.setLayout (new BorderLayout());
forward = new JButton("Forward");
rotate = new JButton("Roatate");
clear = new JButton("Clear");
exit = new JButton("Exit");
forward.addActionListener(this);
rotate.addActionListener(this);
clear.addActionListener(this);
exit.addActionListener(this);
JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(10,18,5,4));
eastPanel.add(forward);
eastPanel.add(rotate);
eastPanel.add(clear);
eastPanel.add(exit);
yourContainer.add(eastPanel, BorderLayout.EAST);
JPanel centerPanel = new JPanel ();
centerPanel.setLayout(new GridLayout(10,10));
for(int x = 0;x < 100;x++)
{
b[x] = new JButton("");
centerPanel.add(b[x]);
b[x].setSize(200,400);
b[x].setBackground(Color.green);
b[x].addActionListener(this);
}
b[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 direction = event.getActionCommand();
if (direction.compareTo("Exit") == 0)
System.exit(0);
if (event.getSource() == forward)
{
squareForward=squareForward-1;
b[squareForward].setBackground(Color.black);
directionField.setText("N");
}
}
public static void main (String[] args)
{
Robot test = new Robot();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|
|
|
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Robot extends JFrame implements ActionListener
{
private JButton[][] b = new JButton[10][10];
private JTextField squareField, directionField;
Color green;
int[] startPosition;
int[] direction;
int[] currentPosition;
int rotationFlag;
final int N = 0;
final int NE = 1;
final int E = 2;
final int SE = 3;
final int S = 4;
final int SW = 5;
final int W = 6;
final int NW = 7;
public Robot()
{
super ("M shape Program");
// initialize grid control variables
// position in button grid = [row, col]
startPosition = new int[] { 10, 0 };
direction = new int[2];
setDirection(-1, 0);
currentPosition = new int[2];
setCurrentPosition(startPosition);
rotationFlag = N;
green = new Color(0,120,0);
Container yourContainer;
yourContainer = getContentPane();
JButton forward = new JButton("Forward");
JButton rotate = new JButton("Rotate");
JButton clear = new JButton("Clear");
JButton exit = new JButton("Exit");
forward.setActionCommand("forward");
rotate.setActionCommand("rotate");
clear.setActionCommand("clear");
exit.setActionCommand("exit");
forward.addActionListener(this);
rotate.addActionListener(this);
clear.addActionListener(this);
exit.addActionListener(this);
JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8,15,0,15);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
eastPanel.add(forward, gbc);
eastPanel.add(rotate, gbc);
eastPanel.add(clear, gbc);
eastPanel.add(exit, gbc);
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTH;
eastPanel.add(new JLabel(), gbc);
yourContainer.add(eastPanel, BorderLayout.EAST);
JPanel centerPanel = new JPanel ();
centerPanel.setLayout(new GridLayout(10,10));
Color borderColor = UIManager.getColor("Panel.background");
for(int row = 0; row < b.length; row++)
{
for(int col = 0; col < b[0].length; col++)
{
b[row][col] = new JButton("");
centerPanel.add(b[row][col]);
b[row][col].setBackground(green);
b[row][col].setBorder(BorderFactory.createLineBorder(borderColor));
}
}
yourContainer.add(centerPanel, BorderLayout.CENTER);
JPanel southPanel = new JPanel ();
JLabel square1Label = new JLabel("Square: ");
southPanel.add(square1Label);
squareField = new JTextField(2);
southPanel.add(squareField);
JLabel direction1Label = new JLabel("Direction: ");
southPanel.add(direction1Label);
directionField = new JTextField("N", 2);
directionField.setHorizontalAlignment(JTextField.CENTER);
directionField.setFont(directionField.getFont().deriveFont(Font.BOLD));
southPanel.add(directionField);
yourContainer.add(southPanel, BorderLayout.SOUTH);
setResizable(false);
setSize(500, 450);
setLocation(200,200);
setVisible(true);
resetGrid();
}
public void actionPerformed(ActionEvent event){
JButton button = (JButton)event.getSource();
String actionCommand = button.getActionCommand();
if (actionCommand.equalsIgnoreCase("Forward"))
forward();
if (actionCommand.equalsIgnoreCase("Rotate"))
rotate();
if (actionCommand.equalsIgnoreCase("Clear"))
resetGrid();
if (actionCommand.equalsIgnoreCase("Exit"))
System.exit(0);
}
private void clearGrid()
{
for(int y = 0;y < 10;y++)
for(int x = 0;x < 10;x++)
b[x][y].setBackground(green);
setCurrentPosition(startPosition);
}
private void resetGrid()
{
while(rotationFlag != N)
rotate();
setCurrentPosition(startPosition);
clearGrid();
int row = currentPosition[0];
int col = currentPosition[1];
forward();
}
private void forward()
{
currentPosition[0] += direction[0];
currentPosition[1] += direction[1];
int row = currentPosition[0];
int col = currentPosition[1];
//System.out.println("row = " + row + "\tcol = " + col);
if(isInBounds(row, col))
b[row][col].setBackground(Color.black);
String loc = String.valueOf(row) + ", " + col;
squareField.setText(loc);
}
private void rotate()
{
rotationFlag++;
if(rotationFlag > NW)
rotationFlag = N;
String s = "";
switch(rotationFlag)
{
case N:
setDirection(-1, 0);
s = "N";
break;
case NE:
setDirection(-1, 1);
s = "NE";
break;
case E:
setDirection(0, 1);
s = "E";
break;
case SE:
setDirection(1, 1);
s = "SE";
break;
case S:
setDirection(1, 0);
s = "S";
break;
case SW:
setDirection(1, -1);
s = "SW";
break;
case W:
setDirection(0, -1);
s = "W";
break;
case NW:
setDirection(-1, -1);
s = "NW";
break;
default:
System.err.println("unexpected rotationFlag: " + rotationFlag);
}
directionField.setText(s);
}
private boolean isInBounds(int x, int y)
{
if(x < 0 || x > b[0].length-1 || y < 0 || y > b.length-1)
return false;
return true;
}
private void setCurrentPosition(int[] position)
{
currentPosition[0] = position[0];
currentPosition[1] = position[1];
}
private void setDirection(int x, int y)
{
direction[0] = x;
direction[1] = y;
}
public static void main (String[] args)
{
Robot test = new Robot();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.runRobot();
}
public void runRobot()
{
int i;
for (i=0; i < 6; i++)
{
forward();
}
for (i=0; i<3; i++)
{
rotate();
}
for (i=0; i<3; i++)
{
forward();
}
for (i=0; i<6; i++)
{
rotate();
}
for (i=0; i<3; i++)
{
forward();
}
for (i=0; i<3; i++)
{
rotate();
}
for (i=0; i<6; i++)
{
forward();
}
}
}
|
|
|
|
|
thank you
you make it look easy.
there's statments that you done i just need a understanding what the code suppose to do at that point.
if you know any websites regarding this that would be cool instead of you explaining.
you stated:
private boolean isInBounds(int x, int y)
{
if(x < 0 || x > b[0].length-1 || y < 0 || y > b.length-1)
return false;
return true;
} ?
setDirection(-1, 0); ?
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8,15,0,15);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER; ??
private void forward()
{
currentPosition[0] += direction[0];
currentPosition[1] += direction[1];
int row = currentPosition[0];
int col = currentPosition[1];
//System.out.println("row = " + row + "\tcol = " + col);
if(isInBounds(row, col))
b[row][col].setBackground(Color.black);
String loc = String.valueOf(row) + ", " + col;
squareField.setText(loc);
} ???
thank you very much...
|
|
|
|
|
|
|
|