if anyone can help me add this onto my work that would be very appreciated. Modi
ID: 3833079 • Letter: I
Question
if anyone can help me add this onto my work that would be very appreciated.
Modify GUI and Graphics Case Study Exercise 8.1 to include a JLabel as a status bar that displays counts representing the number of each shape displayed. Class DrawPanel should declare a method that returns a String containing the status text. In main, first create the DrawPanel, then create the JLabel with the status text as an argument to the JLabel’s constructor. Attach the JLabel to the SOUTH region of the JFrame, as shown in Fig. 9.14.
import java.awt.Color;
import java.awt.Graphics;
import java.security.SecureRandom;
import javax.swing.*;
public class DrawPanel extends JPanel {
private static final long serialVersionUID = 1L;
private SecureRandom randomNumbers = new SecureRandom ();
private MyLine lines [];
private MyRectangle rectangles [];
private MyOval ovals[];
private JLabel label;
// the constructor
public DrawPanel () {
final int NUMBER_COLORS = 256; // (0-255 COLOR VALUES)
final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
int x1, y1, x2, y2;
Color color;
// set background color
setBackground (Color.MAGENTA);
setBackground (Color.WHITE);
// allocate the array
lines = new MyLine [5 + randomNumbers.nextInt (5)];
rectangles = new MyRectangle[5 + randomNumbers.nextInt (5)];
ovals = new MyOval[5 + randomNumbers.nextInt (5)];
// create the MyLine objects
for (int count = 0; count < lines.length; count++) {
// generate random coordinates (0 to 299)
x1 = randomNumbers.nextInt (WINDOW_WIDTH);
y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
x2 = randomNumbers.nextInt (WINDOW_WIDTH);
y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
//display some output intended as a 'diagnostic' output
// System.out.printf ("x1=%d, y1 = %d ", x1, y1);
// generate a random color
color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
boolean flag = randomNumbers.nextBoolean ();
// Construct a MyLine object and add it reference to the array
lines [count] = new MyLine (x1, y1, x2, y2, color, flag);
} // end for loop to create MyLine objects
for (int count = 0; count < rectangles.length; count ++) {
x1 = randomNumbers.nextInt (WINDOW_WIDTH);
y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
x2 = randomNumbers.nextInt (WINDOW_WIDTH);
y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
boolean flag = randomNumbers.nextBoolean ();
rectangles [count] = new MyRectangle (x1, y1, x2, y2, color, flag);
} // end for loop rectangles
for (int count = 0; count < ovals.length; count ++) {
x1 = randomNumbers.nextInt (WINDOW_WIDTH);
y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
x2 = randomNumbers.nextInt (WINDOW_WIDTH);
y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
color = new Color (randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
boolean flag = randomNumbers.nextBoolean ();
ovals [count] = new MyOval (x1, y1, x2, y2, color, flag);
} // end for loop ovals
} // end drawPanel constructor
// when time to paint the panel, draw the lines, rectangles, and ovals
public void paintComponent (Graphics g) {
// paint the parent first
super.paintComponent (g);
// draw the lines
for (MyLine line : lines)
line.draw (g);
for (MyRectangle rect : rectangles)
rect.draw(g);
for (MyOval oval :ovals)
oval.draw(g);
} // end method paintComponent
} // end class DrawPanel
TEST DRAW CLASS (MAIN)
// DrawPanel.java
//Jerico Napalan
//CSIS 293
package project2;
import java.awt.BorderLayout;
import javax.swing.*;
public class TestDraw {
public static void main(String[] args) {
// declare local variable/objects, using constants for the window dimensions
final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
DrawPanel panel = new DrawPanel (); // call the constructor creating MyLine objects
JFrame app = new JFrame (); // the window and its components
JLabel label = new JLabel (panel.toString());
// specify what is done when the user 'presses' the terminate icon in the title bar
app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// add the panel to the 'container' for the JFrame
app.add(panel);
// set the size of the drawing area
app.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// finally, set the flag that tells the Java Virtual Machine to call the 'paintComponent'
app.setVisible (true); // show the window
app.add(label, BorderLayout.SOUTH);
} // end method main
} // end class TestDraw
Explanation / Answer
import java.awt.Color;
import java.awt.Graphics;
import java.security.SecureRandom;
import javax.swing.*;
public class DrawPanel extends JPanel {
private static final long serialVersionUID = 1L;
private SecureRandom randomNumbers = new SecureRandom ();
private MyLine lines [];
private MyRectangle rectangles [];
private MyOval ovals[];
private JLabel label;
// the constructor
public DrawPanel () {
final int NUMBER_COLORS = 256; // (0-255 COLOR VALUES)
final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
int x1, y1, x2, y2;
Color color;
// set background color
setBackground (Color.MAGENTA);
setBackground (Color.WHITE);
// allocate the array
lines = new MyLine [5 + randomNumbers.nextInt (5)];
rectangles = new MyRectangle[5 + randomNumbers.nextInt (5)];
ovals = new MyOval[5 + randomNumbers.nextInt (5)];
// create the MyLine objects
for (int count = 0; count < lines.length; count++) {
// generate random coordinates (0 to 299)
x1 = randomNumbers.nextInt (WINDOW_WIDTH);
y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
x2 = randomNumbers.nextInt (WINDOW_WIDTH);
y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
//display some output intended as a 'diagnostic' output
// System.out.printf ("x1=%d, y1 = %d ", x1, y1);
// generate a random color
color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
boolean flag = randomNumbers.nextBoolean ();
// Construct a MyLine object and add it reference to the array
lines [count] = new MyLine (x1, y1, x2, y2, color, flag);
} // end for loop to create MyLine objects
for (int count = 0; count < rectangles.length; count ++) {
x1 = randomNumbers.nextInt (WINDOW_WIDTH);
y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
x2 = randomNumbers.nextInt (WINDOW_WIDTH);
y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
boolean flag = randomNumbers.nextBoolean ();
rectangles [count] = new MyRectangle (x1, y1, x2, y2, color, flag);
} // end for loop rectangles
for (int count = 0; count < ovals.length; count ++) {
x1 = randomNumbers.nextInt (WINDOW_WIDTH);
y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
x2 = randomNumbers.nextInt (WINDOW_WIDTH);
y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
color = new Color (randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
boolean flag = randomNumbers.nextBoolean ();
ovals [count] = new MyOval (x1, y1, x2, y2, color, flag);
} // end for loop ovals
} // end drawPanel constructor
public String getStatus(){
String str=" Line: "+lines.length+" Rectangle: "+rectangles.length+" Oval: "+ovals.length;
return str;
}
// when time to paint the panel, draw the lines, rectangles, and ovals
public void paintComponent (Graphics g) {
// paint the parent first
super.paintComponent (g);
// draw the lines
for (MyLine line : lines)
line.draw (g);
for (MyRectangle rect : rectangles)
rect.draw(g);
for (MyOval oval :ovals)
oval.draw(g);
} // end method paintComponent
} // end class DrawPanel
package project2;
import java.awt.BorderLayout;
import javax.swing.*;
public class TestDraw {
public static void main(String[] args) {
// declare local variable/objects, using constants for the window dimensions
final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
DrawPanel panel = new DrawPanel (); // call the constructor creating MyLine objects
JFrame app = new JFrame (); // the window and its components
JLabel label = new JLabel (panel.getStatus());
// specify what is done when the user 'presses' the terminate icon in the title bar
app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// add the panel to the 'container' for the JFrame
app.add(panel);
// set the size of the drawing area
app.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// finally, set the flag that tells the Java Virtual Machine to call the 'paintComponent'
app.setVisible (true); // show the window
app.add(label, BorderLayout.SOUTH);
} // end method main
} // end class TestDraw
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.