Question
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to print the toString() function to the console window each time the application runs. This toString() function is supposed to show the tile's shape, letter, and color component (or components). Could someone please review and debug my code to help me figure out why my toString() function is not working?
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
import java.util.ArrayList;
// This class inherits what is in JPanel
class Tile extends JPanel {
private int red, green, blue, letGen, shapes;
private String [] alphabetlist = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
"R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
private String letter;
// Getter for shapes
public int getShapes() {
return shapes;
}
// Setter for shapes
public void setShapes() {
this.shapes = shapes;
}
// Getter for letter
public int getLetter() {
return letter;
}
// Setter for letter
public void setLetter() {
this.letter = letter;
}
@Override
public String toString() {
return String.format("Tile shape: %s Letter in tile: %s Color component(s) of tile: ", shapes, letter);
}
// Constructor for the class
Tile() {
super(); // Calls super class constructor
SetRandomValues(); // Constructor to set random values
}
// This is to prevent someone from messing with your constructor
final public void SetRandomValues() {
red = GetNumberBetween(0,255); // Calling the method GetNumberBetween
green = GetNumberBetween(0,255); // Calls the method GetNumberBetween
blue = GetNumberBetween(0,255); // Calls the method GetNumberBetween
letGen = GetNumberBetween(0,25); // Generates a letter from the alphabet between A - Z
letter = alphabetlist[letGen];
shapes = GetNumberBetween(0,1);
}
// Gets a letter between two letters
private static int GetNumberBetween(int min, int max) {
Random someRandomLetter = new Random();
return min + someRandomLetter.nextInt(max-min+1); // Returns a random letter
}
public void paintComponent(Graphics g) {
super.paintComponent(g); // Calls the super class's paintComponent
int panelWidth = getWidth(); // Gets the width
int panelHeight = getHeight(); // Gets the height
g.setColor(new Color(red,green,blue)); // Sets the color to the random color we created in the code above
// 0 draws a circle
if (shapes == 0) {
Graphics2D g2d = (Graphics2D)g;
Ellipse2D.Double circle = new Ellipse2D.Double(10, 10, panelWidth-20, panelHeight-20);
g2d.fill(circle); // Fills the panel with circles in the middle
}
else {
g.fillRect(10, 10, panelWidth-20, panelHeight-20); // Fills the panel with rectangles in the middle
}
// This sets the contrasting color so we can see the text in the boxes
g.setColor(new Color(GetContrastingColor(red),GetContrastingColor(green),GetContrastingColor(blue)));
final int fontSize=10; // Sets the font size
g.setFont(new Font("Arial", Font.PLAIN, fontSize)); // Sets the font type, normal font
int stringX = (panelWidth/2)-30; // Sets the position we want to draw the font at for the x position to center the font
int stringY = (panelHeight/2)+30; // Sets the position we want to draw the font at for the y position to center the font
g.drawString(letter,stringX,stringY);
}
// Makes sure that we have a contrasting color
private static int GetContrastingColor(int colorIn) {
return ((colorIn+128)%256);
}
}
class TileFrame extends JFrame implements ActionListener {
private ArrayList
// Default constructor
public TileFrame() {
setBounds(200,200,1200,800); // Sets the bounds for the JFrame/window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Places X button on the screen so that the Frame/window closes
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
contentPane.add(buttonPanel, BorderLayout.SOUTH); // Adds button panel to the south of the BorderLayout
JButton randomize = new JButton("Randomize!"); // Creates new "Randomize" button
buttonPanel.add(randomize); // Adds randomize to buttonPanel
randomize.addActionListener(this); // Adds ActionListener on "randomize" button - the "this" is our JFrame
JPanel lettersGridPanel = new JPanel(); // Creates a panel called lettersGridPanel - holds all letters
contentPane.add(lettersGridPanel, BorderLayout.CENTER); // Adds lettersGridPanel to the center of the BorderLayout
lettersGridPanel.setLayout(new GridLayout(10,10)); // Sets this to a 10x10 GridLayout
tileList = new ArrayList
// For loop which goes through this loop 100 times (creates 100 tiles, adds each of the 100 tiles to the grid)
for(int i=1; i<101; i++) {
Tile tile = new Tile(); // Creates a new Tile object
tileList.add(tile); // Adds tilelist to tile
lettersGridPanel.add(tile); // Adds lettersGridPanel to tile
}
}
public void actionPerformed(ActionEvent e) {
// When the button is pushed in tileList, we tell each item within tileList them to randomize themselves
for(Tile tile : tileList) {
tile.SetRandomValues();
}
repaint();
}
}
public class Mosaic {
public static void main(String[] args) {
System.out.println("Start paint***");
// Creates new frame
TileFrame myTileFrame = new TileFrame();
myTileFrame.setVisible(true); // Makes the frame visible
}
}
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