Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this JAVA program. Here my previous code for it and the instructions are below it. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public class DrawPanel implements Runnable

I need help with this JAVA program. Here my previous code for it and the instructions are below it.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class DrawPanel implements Runnable { static int width = 250; static int height = 250; JFrame application; public void run() { application = new JFrame(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.add(new IDrawPanel()); application.setSize(width, height); application.setVisible(true); } public static void main(String[] args) { DrawPanel dp = new DrawPanel(); SwingUtilities.invokeLater(dp); } class IDrawPanel extends JPanel { int steps = 16; Color clr = new Color(0, 192, 0); public void paint(Graphics gr) { int hgt, wid, x1, y1, x2, y2, x3, y3, hst, vst; Graphics2D g2 = (Graphics2D)gr; g2.setColor(clr); wid = getWidth(); hgt = getHeight(); hst = wid / steps; vst = hgt / steps; x1 = y1 = 0; x3 = wid - 1; y3 = hgt - 1; for (x2 = hst, y2 = hgt - vst; x2 < wid && y2 > 0; x2 += hst, y2 -= vst) { g2.drawLine(x1, y1, x2, y2); g2.drawLine(x2, y2, x3, y3); } } } } 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Using the DrawPanel example, create a program that will display the string "BCS 345" in a well-proportioned window using a 42-point font of the SanSerif family.

Use the following guidelines:

Rename the IDrawPanel to TextPanel

After the application frame is set visible, get the dimensions of both the application frame and the TextDraw panel using get size

Calculate and save the difference between the two in both the height and width dimensions.

Implement the TextPanel as a Component listener.

In the componentResized() method, set a doFlag to indicate that the font needs to be calculated.

In the paint method is the font needs to be recalculated, do the following

get the current font from the Grapics and derive a 42 point font from it

Save the new font so it can be set in the Graphics whenever it is necessary to repaint

Get the new font's FontMetrics object

Get the display rectangle of the string "BCS 345"

Calculate new dimensions for the TextDraw panel that are 4/3 the width and 3/2 the height

Calculate the beginning point for a text display that centers the text in the dimensions of the panel with the new size.

Set the application frame to the new size plus the difference in the components' height and width calculated earlier. This should resize the window so that it will appear as shown in the attached image

Reset the doFont flag

In the paint() method's common logic, set the font, set the color, and draw the BCS 345 string.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Define Administration and Management

Answered: 1 week ago

Question

Define organisational structure

Answered: 1 week ago

Question

Define line and staff authority

Answered: 1 week ago

Question

Define the process of communication

Answered: 1 week ago

Question

Explain the importance of effective communication

Answered: 1 week ago