The question is below and this uses Eclipse. The code provided below displays ni
ID: 3722571 • Letter: T
Question
The question is below and this uses Eclipse.
The code provided below displays nine panels of different colors in a 3 X 3 grid. Whenever the mouse enters a panel, it turns magenta; when the mouse leaves, the color reverts back to its original color. Add the following user story: "When the user clicks on the center panel, the colors of the eight surrounding panels rotate to the next panel in a clockwise direction." Remember to use Test Driven Development.
Also add this user story: "When the user consecutively clicks on two of the outer panels, the colors of these panels are swapped."
Right now the code has some errors and still needs the second user story above. I got most of it to work but I messed something up somewhere.
import java.awt.Color;
public class CenterPanel extends MyJPanel {
/**
*
*/
private static final long serialVersionUID = 6464172774001526817L;
Init init;
public CenterPanel(Color myColor, Init init) {
super(myColor);
this.init = init;
}
@Override
public void clicked() {
init.panels[8].setMyColor(init.panels[5].getBackground());
init.panels[5].setMyColor(init.panels[2].getBackground());
init.panels[2].setMyColor(init.panels[1].getBackground());
init.panels[1].setMyColor(init.panels[0].getBackground());
init.panels[0].setMyColor(init.panels[3].getBackground());
init.panels[3].setMyColor(init.panels[6].getBackground());
init.panels[6].setMyColor(init.panels[7].getBackground());
init.panels[7].setMColor(temp);
}
}
===================================
===================================
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
public class Init {
public JFrame jFrame;
MyJPanel panels[];
Color colors[] = { Color.blue, Color.green, Color.red, Color.white, Color.yellow, Color.black, Color.orange,
Color.cyan, Color.gray };
public Init() {
panels = new MyJPanel[9];
jFrame = new JFrame("NINE SQUARES PROGRAM");
jFrame.setSize(500, 500);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.getContentPane().setLayout(new GridLayout(3, 3));
for (int i = 0; i < panels.length; i++) {
if (i == 4) {
panels[i] = new CenterPanel(colors[i], this);
} else {
panels[i] = new MyJPanel(colors[i]);
}
panels[i] = new MyJPanel(colors[i]);
jFrame.getContentPane().add(panels[i]);
}
jFrame.setVisible(true);
}
public static void main(String[] args) {
new Init();
}
}
===================================
===================================
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
public class MyJPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 3871042477070449604L;
private Color myColor;
public MyJPanel(Color myColor) {
this.myColor = myColor;
setBackground(myColor);
addMouseListener(new MouseWatcher());
}
public void setMyColor(Color myColor) {
this.myColor = myColor;
setOriginalColor();
}
public void setSelectedColor() {
setBackground(Color.magenta);
}
public void setOriginalColor() {
setBackground(myColor);
}
class MouseWatcher extends MouseAdapter {
@Override
public void mouseEntered(MouseEvent me) {
setSelectedColor();
}
@Override
public void mouseExited(MouseEvent me) {
setOriginalColor();
}
@Override
public void mouseClicked(MouseEvent me) {
clicked();
}
}
public void clicked() {
Color oldColors = new Color[init.panels.length];
for (int i = 0; i < oldColors.Length; i++) {
oldColors[i] = init.panels[i].getbackground();
}
init.panels[8].setMyColor(init.panels[5].getBackground());
init.panels[7].setMyColor(init.panels[8].getBackground());
init.panels[6].setMyColor(init.panels[7].getBackground());
init.panels[5].setMyColor(init.panels[2].getBackground());
init.panels[3].setMyColor(init.panels[6].getBackground());
init.panels[2].setMyColor(init.panels[1].getBackground());
init.panels[1].setMyColor(init.panels[0].getBackground());
init.panels[0].setMyColor(init.panels[3].getBackground());
}
}
===================================
===================================
import static org.junit.Assert.assertSame;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class InitTest {
Init init;
@BeforeEach
public void setUp() {
init = new Init();
}
private void clickMultipleTimes(){
}
@Test
public void rotating_colors() {
Color[] oldColors = getStartingColors();
init.panels[4].clicked();
assertSame(oldColors[3], init.panels[0].getBackground());
assertSame(oldColors[0], init.panels[1].getBackground());
assertSame(oldColors[1], init.panels[2].getBackground());
assertSame(oldColors[6], init.panels[3].getBackground());
assertSame(oldColors[2], init.panels[5].getBackground());
assertSame(oldColors[7], init.panels[6].getBackground());
assertSame(oldColors[8], init.panels[7].getBackground());
assertSame(oldColors[5], init.panels[8].getBackground());
}
@Test
public void rotate_twice(){
Color[] oldColors = getStartingColors();
init.panels[4].clicked();
init.panels[4].clicked();
assertSame(oldColors[6], init.panels[0].getBackground());
assertSame(oldColors[3], init.panels[1].getBackground());
assertSame(oldColors[0], init.panels[2].getBackground());
assertSame(oldColors[6], init.panels[3].getBackground());
assertSame(oldColors[2], init.panels[5].getBackground());
assertSame(oldColors[7], init.panels[6].getBackground());
assertSame(oldColors[8], init.panels[7].getBackground());
assertSame(oldColors[5], init.panels[8].getBackground());
}
}
===================================
===================================
import static org.junit.Assert.assertSame;
import java.awt.Color;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class MyJPanelTest {
private static final Color COLOR = Color.BLACK;
private static final Color MAGENTA = Color.magenta;
MyJPanel myJPanel;
@BeforeEach
public void setUp() {
myJPanel = new MyJPanel(COLOR);
}
@Test
void set_selected_color() {
myJPanel.setSelectedColor();
assertSame(MAGENTA, myJPanel.getBackground());
}
@Test
void set_original_color() {
myJPanel.setOriginalColor();
assertSame(COLOR, myJPanel.getBackground());
}
}
Explanation / Answer
CenterPanel.java
import java.awt.Color;
@SuppressWarnings("serial")
public class CenterPanel extends MyJPanel {
Init init;
public CenterPanel(Color myColor, Init init) {
super(myColor);
this.init = init;
}
@Override
public void clicked() {
}
}
Init.java
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
public class Init {
public JFrame jFrame;
MyJPanel panels[];
Color colors[] = { Color.blue, Color.green, Color.red, Color.white,
Color.yellow, Color.black, Color.orange,
Color.cyan, Color.gray };
public Init() {
panels = new MyJPanel[9];
jFrame = new JFrame("NINE SQUARES PROGRAM");
jFrame.setSize(500, 500);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.getContentPane().setLayout(new GridLayout(3, 3));
for (int i = 0; i < panels.length; i++) {
panels[i] = new MyJPanel(colors[i]);
jFrame.getContentPane().add(panels[i]);
}
jFrame.setVisible(true);
}
public static void main(String[] args) {
new Init();
}
}
MyJPanel.java
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MyJPanel extends JPanel {
private Color myColor;
public MyJPanel(Color myColor) {
this.myColor = myColor;
setBackground(myColor);
addMouseListener(new MouseWatcher());
}
public void setMyColor(Color myColor) {
this.myColor = myColor;
setOriginalColor();
}
public void setSelectedColor() {
setBackground(Color.magenta);
}
public void setOriginalColor() {
setBackground(myColor);
}
class MouseWatcher extends MouseAdapter {
@Override
public void mouseEntered(MouseEvent me) {
setSelectedColor();
}
@Override
public void mouseExited(MouseEvent me) {
setOriginalColor();
}
@Override
public void mouseClicked(MouseEvent me) {
clicked();
}
}
public void clicked() {
}
}
InitTest.java
import static org.junit.Assert.assertSame;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class InitTest {
Init init;
@BeforeEach
public void setUp() {
init = new Init();
}
@Test
public void rotating_colors() {
init.panels[4].clicked();
assertSame(init.colors[3],
init.panels[0].getBackground());
assertSame(init.colors[0],
init.panels[1].getBackground());
assertSame(init.colors[1],
init.panels[2].getBackground());
assertSame(init.colors[6],
init.panels[3].getBackground());
assertSame(init.colors[2],
init.panels[5].getBackground());
assertSame(init.colors[7],
init.panels[6].getBackground());
assertSame(init.colors[8],
init.panels[7].getBackground());
assertSame(init.colors[5],
init.panels[8].getBackground());
}
}
MyJPanelTest.java
import static org.junit.Assert.assertSame;
import java.awt.Color;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class MyJPanelTest {
private static final Color COLOR = Color.BLACK;
private static final Color MAGENTA = Color.magenta;
MyJPanel myJPanel;
@BeforeEach
public void setUp() {
myJPanel = new MyJPanel(COLOR);
}
@Test
void set_selected_color() {
myJPanel.setSelectedColor();
assertSame(MAGENTA, myJPanel.getBackground());
}
@Test
void set_original_color() {
myJPanel.setOriginalColor();
assertSame(COLOR, myJPanel.getBackground());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.