Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is an INDIVIDUAL assignment. The goal is to create a simple ui to play lite

ID: 3872833 • Letter: T

Question

This is an INDIVIDUAL assignment. The goal is to create a simple ui to play litebrite. Litebrite e is game of a array of spaces that can be filled with colored lights to create art Lite-Brite allows the artist to create a glowing picture by placing multi-colored translucent e plastic e pegs through opaque e black e paper ee Using a standard lightbulb, the light is blocked by the black paper except where the pegs conduct the light. When lit, the pegs have an appearance similar to that of LEDs Goal of the project is to simulate this in Java. The original lite-brite has a hexagonal pattern but to simplify we will use a regular rectangular pattern. Provide the user with a colorpicker (a quick tutorial here e). Once they choose a color they can click on any grid and your code should color that grid with the chosen color. The user will be able to change colors and continue "drawing. Clicking on a colored grid should turn off the the color (remove the coloring/return the grid to black). Sample Image: njoy your game The starter code in zip version has also been uploaded to Canvas LiteBrite.zip e Extra Credit: 1. Submit a nice drawing with your application. Submit this as a png/jpg/pdf include it in the zip fle·(2 points) 2. Provide the ability to reset/erase the board and start afresh (3 points).

Explanation / Answer

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.net.*; public class LightsOutPanel extends JPanel implements MouseListener { private boolean[][] lights; public static void main(String[] args) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Lights Out!"); frame.setResizable(false); frame.setVisible(true); LightsOutPanel panel = new LightsOutPanel(); if (panel.lights == null) { System.out.println("You did not initialize your light array!" + "It's still null..."); System.exit(-1); } panel.addMouseListener(panel); panel.setPreferredSize(new Dimension(601, 501)); panel.setMinimumSize(new Dimension(601, 501)); Container c = frame.getContentPane(); c.setLayout(new BorderLayout()); c.add(panel, BorderLayout.CENTER); frame.pack(); } public LightsOutPanel() { lights = new boolean[5][6]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { lights[i][j] = true; } } } // unused methods public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void paint(Graphics g) { int boxWidth = 600 / 6; int boxHeight = 500 / 5; int y = 0; for (int row = 0; row < 5; row++) { int x = 0; for (int col = 0; col < 6; col++) { if (lights[row][col]==true) { g.setColor(Color.YELLOW); } else { g.setColor(Color.BLACK); } g.fillRect(x, y, boxWidth, boxHeight); g.setColor(Color.BLUE); g.drawRect(x, y, boxWidth, boxHeight); x += boxWidth; } y += boxHeight; } } // called when the mouse is pressed - determines what row/column the user // has clicked public void mousePressed(MouseEvent e) { int mouseX = e.getX(); int mouseY = e.getY(); int panelWidth = getWidth(); int panelHeight = getHeight(); int boxWidth = panelWidth / lights[0].length; int boxHeight = panelHeight / lights.length; int col = mouseX / boxWidth; int row = mouseY / boxHeight; toggle(row, col); repaint(); } // called to "toggle" the selected row and column, as well as the four // adjacent lights public void toggle(int row, int col) { if (row >= 0 && col >= 0 && row = 0 && row
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote