Question
I have created a Java program that creates a 2d Array checkerboard and have assigned a JLabel to the middle of it. It contains a
I have created a Java program that creates a 2d Array checkerboard and have assigned a JLabel to the middle of it. It contains a borderlayout of up down left and right. I must implement functions so when I press up down left or right the string in the middle will move accordingly. Please help me add the functions to do so. Here is my source code
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
public class CheckerBoard extends JFrame {
private final int ROWS = 8;
private final int COLS = 8;
private final int GAP = 2;
private final int NUM = ROWS * COLS;
private JFrame fram = new JFrame();
private JPanel pane = new JPanel(new GridLayout(ROWS,COLS,GAP,GAP));
private JPanel[][] panel = new JPanel[ROWS][COLS];
private JLabel cat = new JLabel("=^.^=");
private JButton up = new JButton("UP");
private JButton down = new JButton("down");
private JButton left = new JButton("LEFT");
private JButton right = new JButton("RIGHT");
private Color color1 = Color.WHITE;
private Color color2 = Color.BLUE;
private Color tempColor;
public CheckerBoard() {
super ("Run Kitty Run");
add(pane);
add(up,BorderLayout.NORTH);
add(down,BorderLayout.SOUTH);
add(left,BorderLayout.WEST);
add(right,BorderLayout.EAST);
panel[4][4].add(cat);
for (int x = 0; x < ROWS; ++x)
{
for(int y = 1; y < ROWS; ++y)
{
panel[x][y] = new JPanel();
pane.add(panel[x][y]);
if(x % ROWS == 0 && y % ROWS ==1)
{
tempColor = color1;
color1 = color2;
color2 = tempColor;
}
if(x % 2 == 0 && y % 2 == 1 || x % 2 == 1 && y % 2 == 0)
{
panel[x][y].setBackground(color1);
}
else {
panel[x][y].setBackground(color2);
}
}
}
};
public static void main(String[] args) {
// TODO Auto-generated method stub
CheckerBoard frame = new CheckerBoard();
frame.setSize(300,300);
frame.setVisible(true);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started