Question
Problem - Java Programming (Source code located below, just fill in the missing code in the designated areas) :) Please provide a screenshot once complete
Problem - Java Programming (Source code located below, just fill in the missing code in the designated areas) :)
Please provide a screenshot once complete of it compiled which should look sorta similar to the Sample one provided.
Develop a Java application named Problem1.java that uses a simple GUI laid-out inside the application main window (lets call it GUI). The main window should be sized to be 75% of your platforms screen and must contain the following five widgets. Hint See the Sample Program GUI for a sample version of the GUI.
-a 1-row, full-window-width, red-text Label "Enter "important" quote below"
-a multi-row, full-window-width text area (lets call it textArea)
-2 command buttons (labeled "Show" and "Exit")
-a panel to draw on (lets call it drawingPanel) that occupies most of the window
As your application runs, it must respond to GUI events as follows
-A left-click on the "Show" command button must process the string text entered by the program user into textArea. Note If you like, you may provide a favorite quotation as a default value for text that is different from the one Dr. Hanna used.
-A left-click on the "Exit" command button must cause the application to terminate normally.
To process text, you must
-Tokenize text into a collection of n strings (lets call it words[i], i [ 0,n-1 ]). You may assume (1) that text is always composed of 3 or more words arranged as 1 or more sentences; and (2) that the words in text aredelimited (that is, separated from each other) by any combination of one or more slashes '\\', double-quotes '\"', tabs '\t', spaces ' ', end?of?lines ' ', periods '.', commas ',', colons ':', semicolons ';', dashes '-', question marks '?', and/or exclamation points '!'. Note You may use the String class method split(), but you can also use the StringTokenizer class instead of the String split() method.
-Sort the n strings in words[] into descending lexicographic order.
-Assume panelD is the smaller of the two dimensions of drawingPanel. Draw a red, un-filled outer circle with radiusouter = 0.40*panelD pixels and center (xc,yc) = (panelD/2,panelD/2).
-Draw another concentric red, un-filled inner circle with a much smaller radiusinner = 0.05*panelD pixels (concentric means the inner circle has the same center as the outer circle, namely,(xc,yc)).
-Let a = 2p radians. For i [ 0,n-1 ], let angle[i] = (p/2-a*i), draw the radial[i] blue line-segment at angle[i] so that it extends from the inner circle to a point that is 0.05*panelD pixels beyond the outer circle. Every radial[i] line-segment must be tipped with a radius = 5-pixel, red, filled circle. Notes (1) radial[0] points straight up, angle[0] = p/2 radians; and (2) every radial[i] line-segment, if extended, would intersect with the shared center of the two red, un-filled circles.
-Draw a green, un-filled, n-sided, regular polygon circumscribed by the outer circle such that the polygon line-segments connect the points where the radial[i] blue line-segments intersect the outer circle.
-Draw the words[i] 1 character at a time along radial[i], i = 0,1,..., n-1, using (1) font family = "Courier New" and font size = 24; (2) a font color cyclically selected from the set of Color constants { black,red,green,blue,cyan,yellow,magenta }; and (3) a font style cyclically selected from the set of constants { PLAIN, ITALIC, PLAIN+BOLD, ITALIC+BOLD }. For example, all the characters in words[0] must be displayed as PLAIN black text, words[1] as ITALIC red, words[2] as PLAIN+BOLD green,...,words[6] as PLAIN+BOLD magenta, et cetera. Draw all characters in all words[i]so that the lower-left hand corner of each character rests close to its radial line-segment and with the first character of each words[i] placed close to the inner circle.
//---------------------------------------------------------
// Chapter #27, Problem #1
// Problem1.java
//---------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Problem1
{
public static void main(String[] args)
{
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
}
//---------------------------------------------------------
// Chapter #27, Problem #1
// GUI.java
//---------------------------------------------------------
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//---------------------------------------------------------
class XY
//---------------------------------------------------------
{
public int x;
public int y;
}
//---------------------------------------------------------
class WORD
//---------------------------------------------------------
{
public String word;
public double angle; // angle of radial line
public XY xy1; // (x,y) coordinate of outer circle end point
public XY xy2; // (x,y) coordinate of inner circle end point
public XY xy3; // (x,y) coordinate of center of red, filled circle "tip"
public WORD(String word)
{
this.word = new String(word);
this.xy1 = new XY();
this.xy2 = new XY();
this.xy3 = new XY();
}
}
//---------------------------------------------------------
public class GUI extends JFrame
//---------------------------------------------------------
{
private final double GUIPCT = 0.75; // main window size expressed as % of screen size
private final Color COLORS[] =
{
Color.black ,Color.red, Color.green, Color.blue,
Color.cyan, Color.yellow,Color.magenta
};
private final int FONTSTYLES[] =
{
Font.PLAIN,
Font.ITALIC,
Font.PLAIN+Font.BOLD,
Font.ITALIC+Font.BOLD
};
private final int FONTSIZE = 24;
private GridBagLayout GUIlayout;
private GridBagConstraints GUIconstraints;
private boolean canDraw = false; // Why is this needed?!
private WORD words[];
private int n;
//-------------------------------------------------------
public GUI()
//-------------------------------------------------------
{
super("Chapter #27, Problem #1");
// determine screen and GUI dimensions and center-of-screen coordinates
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenW = Rounded(screenSize.getWidth());
int screenH = Rounded(screenSize.getHeight());
int GUIW = Rounded(screenW*GUIPCT);
int GUIH = Rounded(screenH*GUIPCT);
int screenXC = screenW/2;
int screenYC = screenH/2;
// center GUI on screen
setBounds(screenXC-GUIW/2,screenYC-GUIH/2,GUIW,GUIH);
// define and place GUI components using grid-bag layout
// From my dictionary, "grab-bag is (1) a container from which a person draws
// a gift without knowing what it is; (2) any miscellaneous collection"
GUIlayout = new GridBagLayout();
setLayout( GUIlayout );
GUIconstraints = new GridBagConstraints();
GUIconstraints.ipadx = 5;
GUIconstraints.ipady = 5;
JLabel textLabel = new JLabel("Enter \"important quote\" below");
textLabel.setForeground(Color.red);
AddGUIComponent(textLabel,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,
0,0,2,1,1,0);
Student provides missing code for defining and placing textArea, drawingPanel and "Show" button
// "Show" button handler
showButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
Student provides missing code for getting text from textArea, tokenizing the text to extract the words[]
then sorting the words[] in descending lexicographic order. *Hints* (1) use the gettext() method;
and (2) use the String class method split() or the StringTokenizer class instead of the String
split() method.
canDraw = true;
drawingPanel.repaint();
}
}
);
Student provides missing code for defining and placing "Exit" button
// "Exit" button handler
exitButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
);
}
//-------------------------------------------------------
private void AddGUIComponent(Component component,
int anchor,int fill,
int gridx,int gridy,int gridwidth,int gridheight,
int weightx,int weighty)
//-------------------------------------------------------
{
GUIconstraints.anchor = anchor;
GUIconstraints.fill = fill;
GUIconstraints.gridx = gridx;
GUIconstraints.gridy = gridy;
GUIconstraints.gridwidth = gridwidth;
GUIconstraints.gridheight = gridheight;
GUIconstraints.weightx = weightx;
GUIconstraints.weighty = weighty;
GUIlayout.setConstraints(component,GUIconstraints);
add(component);
}
//-------------------------------------------------------
int Rounded(double x)
//-------------------------------------------------------
{
if ( x
return( (int) (x-0.5) );
else
return( (int) (x+0.5) );
}
//-------------------------------------------------------
class MyPanel extends JPanel
//-------------------------------------------------------
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int panelW = getWidth();
int panelH = getHeight();
int panelD = (panelW
int XC = panelW/2;
int YC = panelH/2;
int ro = Rounded(panelD*0.40); // radius of outer circle
int ri = Rounded(panelD*0.05); // radius of inner circle
int rt = 5; // radius of red, filled circle "tips"
if ( canDraw )
{
// draw red inner and outer circles
Student provides missing code *Hints* (1) setColor(); and (2) drawOval()
double A = 2*Math.PI;
for (int i = 0; i
{
// compute (x,y) coordinates for words[i]
words[i].angle = (Math.PI/2) - A*i;
words[i].xy1.x = XC+Rounded(ro*Math.cos(words[i].angle));
words[i].xy1.y = YC-Rounded(ro*Math.sin(words[i].angle));
Student provides missing code to compute xy2 (the (x,y) coordinate of inner circle point) and xy3 (the
(x,y) coordinate of center of red, filled circle "tip")
// draw blue radial lines with red, filled, 5-pixel radius circle "tips"
Student provides missing code *Hints* (1) setColor(); (2) drawLine(); and (3) fillOval()
}
// draw green polygon lines
Student provides missing code *Hints* (1) setColor(); and (2) drawLine()
// draw words[i] along blue radial lines, character-by-character
for (int i = 0; i
{
Student provides missing code *Hints* (1) setColor(); (2) setFont(); and (3) drawChars()
}
}
}
}
}
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