Create a JAVA app that displays an 8X8 grid of panels, all of which are intially
ID: 3773631 • Letter: C
Question
Create a JAVA app that displays an 8X8 grid of panels, all of which are intially colored white. When the user presses the mouse within a panel, its color should be changed randomly.
Start with ShowGridRectangle and add code from MouseEventDemo.
//ShowGridRectangle.java
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.Arrays;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
public class ShowGridRectangle extends Application {
private GridPane pane;
public void start(Stage primaryStage) {
pane = new GridPane();
int k = 0;
int r = 0;
String[][] color={{"BLACK","RED"},{"RED","BLACK"}};
pane.setAlignment(Pos.CENTER);
pane.setHgap(5.5);
pane.setVgap(5.5);
for(int k1 = 0; k1 < 8; k1++) {
if(r > 1)
r = 0;
for(int k2 = 0; k2 < 8; k2++) {
if(k > 1)
k = 0;
Rectangle r1 = new Rectangle(50,50);
r1.setStroke(Color.WHITE);
r1.setFill(Paint.valueOf(color[r][k]));
pane.add(r1,k1,k2);
k++;
}
r++;
}
Scene scene = new Scene(pane);
primaryStage.setTitle("8x8 GRID Rectangle");
primaryStage.setScene(scene);
primaryStage.show();
}
}
//MouseEventDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class MouseEventDemo extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Text text = new Text(20, 20, "Programming is fun");
pane.getChildren().addAll(text);
text.setOnMouseDragged(e ->
{
text.setX(e.getX());
text.setY(e.getY());
});
Scene scene = new Scene(pane, 300, 100);
primaryStage.setTitle("MouseEventDemo");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Explanation / Answer
I have created the java code for the same.
It will randomly pickup any color once the mouse key is pressed.
Java code:
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
public class GUIWindow {
public static void main(String[] args){
JFrame tileGUI = new JFrame();
tileGUI.setTitle("8x8 Tile program");
tileGUI.setSize(300, 200);
tileGUI.setDefaultCloseOperation(JFrame....
Container pane = tileGUI.getContentPane();
pane.setLayout(new GridLayout(0, 8));
for (int row = 1; row <= 8; row++) { // code to create 8x8 matrix
for (int col =1; col <=8; col++) {
ColorPanel panel = new ColorPanel(Color.white); //making initial panel color white
pane.add(panel);
}
}
tileGUI.setVisible(true);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import java.util.Random;
public class ColorPanel extends JPanel{
public ColorPanel(Color backColor){
setBackground(backColor);
addMouseListener(new PanelListener());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
}
private class PanelListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
Color rcolor = new Color(r,g,b);
setBackground(rcolor);
}
}
}