Write a program named Pattern that uses the DrawingPanel class provided by the t
ID: 3668798 • Letter: W
Question
Write a program named Pattern that uses the DrawingPanel class provided by the textbook to create a drawing panel (canvas) with a width of 540 pixels and an height of 540 pixels.
Prompt the user for the number of rows and columns for the pattern. The numbers should be between 1 and 10. Convert numbers below 1 to 1 and numbers above 10 to 10. Then prompt the user for the Red, Green, and Blue values used to create the custom color for the pattern. These values should be between 0 and 255 -- convert numbers below 0 to 0 and numbers above 255 to 255.
Then draw a pattern with the given number of rows and columns of circles inside squares. Each circle and square should be 50 pixels wide. The (x, y) coordinates used to draw the first circle/square in the first row should be (20, 20).
After drawing all of the circles and squares, output a message to the user to close the Drawing Panel in order to exit the program.
For example, the input below,
would result in the following pattern shown below:
Explanation / Answer
import java.awt.Canvas;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* Author: Sunilkumar Kota
*/
public class Pattern
{
private JFrame mainFrame;
private JPanel controlPanel;
public Pattern()
{
prepareGUI();
}
private void prepareGUI()
{
mainFrame = new JFrame("Java AWT Examples");
mainFrame.setSize(600, 600);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
Pattern pattern = new Pattern();
pattern.showCanvasDemo();
}
private void showCanvasDemo()
{
controlPanel.add(new DrawingPanel());
mainFrame.setVisible(true);
}
class DrawingPanel extends Canvas
{
public DrawingPanel()
{
setBackground(Color.WHITE);
setSize(540, 540);
}
public void paint(Graphics g)
{
int rows = 0;
int columns = 0;
int red = 0, green = 0, blue = 0;
Graphics2D g2;
g2 = (Graphics2D) g;
// g2.drawString ("It is a custom canvas area", 70, 70);
Scanner scanner = new Scanner(System.in);
System.out.print("Number of Rows (1-10) :");
System.out.println("");
if (scanner.hasNextInt())
{
rows = scanner.nextInt();
if (rows < 1)
{
rows = 1;
}
else if (rows > 10)
{
rows = 10;
}
}
System.out.print("Number of Columns (1-10) :");
if (scanner.hasNextInt())
{
columns = scanner.nextInt();
if (columns < 1)
{
columns = 1;
}
else if (columns > 10)
{
columns = 10;
}
}
System.out.println("Red value :");
System.out.println("Green value :");
System.out.println("Blue value :");
if (scanner.hasNext())
{
red = scanner.nextInt();
green = scanner.nextInt();
blue = scanner.nextInt();
}
if (red < 0)
{
red = 0;
}
else if (red > 255)
{
red = 255;
}
if (green < 0)
{
green = 0;
}
else if (green > 255)
{
green = 255;
}
if (blue < 0)
{
blue = 0;
}
else if (blue > 255)
{
blue = 255;
}
Color userColor = new Color(red, green, blue);
g2.setColor(userColor);
int nextX = 20;
int nextY = 20;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
g2.drawRect(nextX, nextY, 50, 50);
g2.drawOval(nextX + 1, nextY + 1, 49, 49);
nextX = nextX + 50;
}
nextX = 20;
nextY = nextY + 50;
}
System.out.println("Please Close the Drawing Panel to exit the Game!!!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.