JAVA Objective : To make you comfortable with using the SimpleGUI graphical inte
ID: 3775357 • Letter: J
Question
JAVA
Objective
:
To
make you comfortable with using the SimpleGUI graphical interface and simple Java commands.
About SimpleGUI
:
SimpleGUI is a bunch of
Java code arranged in a collection called a jar file. It's provides a set of Java commands that your program can use to displ
ay graphical
user interfaces, including images, shapes, and text.
Download the latest version of the SimpleGUI jar file
here. The
SimpleGUI website
also has other information and downloads related to SimpleGUI, for those who
are interest
ed. You'll only need the jar file (and the manual, linked to below) for this assignment.
Information on how to use and install SimpleGUI is available in the manual
here, and the javadocs page for SimpleGUI
provides information on the individual
commands that SimpleGUI provides.
For those who don't want to read through all of the
documentation, here's a brief explanation of how to enable SimpleGUI in your program: In NetBeans, find the
project you are working on, or create a new one. In the "Project Explorer" window pane on the left, you'll see two folders that come directly under
neath your project,
one called "Source Packages" and one called "Libraries". Right
-click on the "Libraries" folder, and select "Add JAR/Folder". In the window that pops up, find the
"SimpleGUI.jar" file you downloaded, select it, and click the "Open" butto
n.
You can verify that SimpleGUI is enabled in your Java project by creating a Java class, creating a main method in the Java cl
ass, and putting the following single
instruction in the main method:
demos.Demo03_BasicDrawing.main(null);
When you run the
class you created, it should pop up a menu with a pretty drawing of colorful lines. (It's the same drawing you see on the Sim
pleGUI home page.)
Your program: drawing shapes
Your program will consist of a single Java file, which you should call
ShapeDrawer.java
.
The program should ask the user to enter several numbers and some text. It should then display some shapes and text using Sim
pleGUI.
However, your program should make use of the information that the user enters to affect the output of t
he program. For instance, the program could ask the user to
enter a width and a height, and then display shapes with that size. Or it could ask for an X and Y position, and then place s
hapes at that position.
Here are the requirements for your program:
•
It must create a SimpleG
UI object for input and output.
•
It must display at least 2 different kinds of shapes (eg, ellipsis, rectangle, line).
•
It must prompt the user for at least 2 numbers, then read in those numbers. Those numbers must affect what the program di
splays in some significant way.
•
It must perform at least three arithmetic calculations (+,
-,*,/,%) that involve the input numbers. The results of these calculations must affect what the program
displays in some significant way.
•
It must display text t
hat appears over top of a shape.
•
It must prompt the user to enter at least 1 string, then read in that string. The string must affect what the program display
s in some significant way. (2 points)
•
It must u
se at least 3 different colors.
For each of these requirements, include a comment in your code that indicates what part of the program you believe addresses the requirement.
Helpful hints
The
javadocs page for the SimpleGUI class
includes helpful information on commands like
drawEllipsis()
and
drawText()
that you will find useful for this
assignment. (Scroll down to the section called "Method Summary" for information on the commands that are available.)
You can see a working example demo of
SimpleGUI code
below.
Copy
-and-
paste this into NetBeans if you'd like to run it.
To create a SimpleGUI window, you should execute this command:
SimpleGUI sg = new SimpleGUI (false);
Of course, you don't have to call the reference variable
"sg", like I did in this example. You can use any variable name you like. But for the rest of these hints, I will
assume the variable is called "sg".
For the command above to compile, you need to include an "import" statement at the top of your program (before "
public class ShapeDrawer
"), to tell Java
where to find the SimpleGUI code. Here's the command for that:
import simplegui.SimpleGUI;
To place shapes and text on the SimpleGUI window, you should execute commands by using
sg.();
For instance:
sg.drawDot(100, 200, 20);
This places a dot on the window with a position 100 pixels from the left, 200 pixels from the
top, and with a radius of 20 pixels. Notice that you have to put the "sg"
in front of the command name for it to work. For all of the commands in the SimpleGUI class, you will need to put "sg." in fr
ont of the command name (or whatever
the name of your Si
mpleGUI variable is).
To prompt the user for input, use the command
sg.print("your prompt here");
To read information from the keyboard, use one of these commands:
sg.keyReadDouble()
sg.keyReadString()
sg.keyReadChar()
import simplegui.SimpleGUI;
import java.awt.Color;
public class SimpleGUIExample
{
public static void main(String [] args)
{
SimpleGUI sg = new SimpleGUI(false);
sg.drawFilledBox(20, 20, 100, 200, Color.PINK, 1.0, "box1");
sg.drawFilledBox(40, 40, 120, 240, Color.YELLOW, 0.5, "box2");
sg.drawText("hello world", 35, 35);
sg.print("please enter a String");
String s = sg.keyReadString();
sg.drawText(s, 60, 60);
}
}
JAVA
Objective
This assignment will give you practice with static methods, parameters, and returns.
•
The goal of this project is to take existing Java code, and compress
it as much as possible by re-
organizing it into methods.
Your program:
Your program will consist of a single Java file, which you should call RefactoredShapeDrawer.java. (The word "refactored" is
a software engineering term for re-
organized code.) The program should work IDENTICALLY to
the attached
Java code for the
OriginalShapeDrawer
class, meaning that if the user enters the
same information into the
RefactoredShapeDrawer
and the
OriginalShapeDrawer
, the output should be identical.
The key requirement for the
RefactoredShapeDrawer
will be that it reorganizes the code into methods, to make the program shorter and easier to understand.
You should copy all of the code from
OriginalShapeDrawer
into
RefactoredShapeDrawer
(but make sure you change the line for the declaration of the class
name). You should then edit the code in
R
efactoredShapeDrawer
to reorganize it.
Here are the specific requirements for your program:
•
It must behave identically to the
OriginalShapeDrawer
. If it doesn't behave identically, you will lose between 5 and 10 points, depending on how different
your pr
ogram's output is from the original.
•
You should introduce a minimum of 4 new methods, not including the main method. Each method must be something that is used during
the execution of
the program.
•
At least two of your methods should have parameters that af
fect the behavior of the method.
•
At least two of your methods should have returns, and the return values must be used in some significant way by the code that calls the methods.
•
Your program should be clearly organized. Individual methods should have a cle
ar purpose, and it should be easy to see what the purpose is from their
name and/or comments describing the method. The behavior of the whole program should be clear from the sequence of method cal
ls in the main method.
Bonus:
Besides re-
organizing the code, an additional goal for the project is to reduce the overall length of the code.
The TA will
give the best “3” submissions (i.e.,
the
shortest code that meets all of the requirements
) 10 points extra.
Keep in mind that for t
his assignment, LENGTH = NUMBER OF SEMI
-COLONS (;) PLUS NUMBER OF ARITHMETIC OPERATORS (+, -
, *, /, %) IN YOUR CODE.
So comments don't count towards the length of the code (you should still put comments in your code!), method headers don't count (you shoul
d still add new methods
if they help organize the code!), and blank lines or lines that consist only of { or } don't count (so you should still inden
t your code in accordance with good style!).
Also, the class constants declared outside of any method wil
l not count towards the length of the program, so please keep those in. It's good programming practice
to use class constants instead of plain numbers in your program anyway.
Instructions:
•
Under
project
Lab09
you’ve created for Part 1, add a new Java main class
called
RefactoredShapeDrawer.java
•
Be sure to document your code
(add comments on top of your java file).
•
In the comments
add your name, date, course, homework number, and
statement of problem.
•
Once you are done, upload
RefactoredShapeDrawer.java
through Blackboard
under Lab 09 link
.
•
You do NOT need to email the
OriginalShapeDrawer
.java file
Explanation / Answer
import java.awt.Color; import simplegui.SimpleGUI; public class OriginalShapeDrawer { public static final int DEFAULT_SIZE = 100; public static final Color DEFAULT_COLOR = Color.BLACK; public static final int SPACE_BETWEEN = 10; public static void main(String [] args) { SimpleGUI sg = new SimpleGUI(false); sg.print("Please enter a background color"); String colorString = sg.keyReadString(); Color bgColor = DEFAULT_COLOR; if("white".equalsIgnoreCase(colorString)) { bgColor = Color.WHITE; } else if("red".equalsIgnoreCase(colorString)) { bgColor = Color.RED; } else if("orange".equalsIgnoreCase(colorString)) { bgColor = Color.ORANGE; } else if("yellow".equalsIgnoreCase(colorString)) { bgColor = Color.YELLOW; } else if("green".equalsIgnoreCase(colorString)) { bgColor = Color.GREEN; } else if("blue".equalsIgnoreCase(colorString)) { bgColor = Color.BLUE; } else if("gray".equalsIgnoreCase(colorString)) { bgColor = Color.GRAY; } else if("magenta".equalsIgnoreCase(colorString)) { bgColor = Color.MAGENTA; } else if("cyan".equalsIgnoreCase(colorString)) { bgColor = Color.CYAN; } else if("pink".equalsIgnoreCase(colorString)) { bgColor = Color.PINK; } else { sg.print("couldn't understand your color, so defaulting to black"); } sg.setBackgroundColor(bgColor); sg.print("Please enter a width"); double width = sg.keyReadDouble(); if(width < 0) { width = -width; } else if(width==0) { width = DEFAULT_SIZE; } sg.print("Please enter a length"); double length1 = sg.keyReadDouble(); if(length1 < 0) { length1 = -length1; } else if(length1==0) { length1 = DEFAULT_SIZE; } sg.print("Please enter a transparency"); double transparency1 = sg.keyReadDouble(); if(transparency1 < 0) { transparency1 = 0; } else if(transparency1 > 1) { transparency1 = 1; } sg.print("Please enter a color"); colorString = sg.keyReadString(); Color color1 = DEFAULT_COLOR; if("white".equalsIgnoreCase(colorString)) { color1 = Color.WHITE; } else if("red".equalsIgnoreCase(colorString)) { color1 = Color.RED; } else if("orange".equalsIgnoreCase(colorString)) { color1 = Color.ORANGE; } else if("yellow".equalsIgnoreCase(colorString)) { color1 = Color.YELLOW; } else if("green".equalsIgnoreCase(colorString)) { color1 = Color.GREEN; } else if("blue".equalsIgnoreCase(colorString)) { color1 = Color.BLUE; } else if("gray".equalsIgnoreCase(colorString)) { color1 = Color.GRAY; } else if("magenta".equalsIgnoreCase(colorString)) { color1 = Color.MAGENTA; } else if("cyan".equalsIgnoreCase(colorString)) { color1 = Color.CYAN; } else if("pink".equalsIgnoreCase(colorString)) { color1 = Color.PINK; } else { sg.print("couldn't understand your color, so defaulting to black"); } sg.drawFilledBox(0, 0, width, length1, color1, transparency1, null); sg.drawFilledBox(width+SPACE_BETWEEN, 0, width, length1, color1, transparency1 / 2.0, null); sg.drawFilledBox(2 * (width+SPACE_BETWEEN), 0, width, length1, color1, 1 - transparency1 / 2.0, null); sg.drawFilledBox(3 * (width+SPACE_BETWEEN), 0, width, length1, color1, transparency1 / 4.0, null); sg.drawFilledBox(4 * (width+SPACE_BETWEEN), 0, width, length1, color1, 1 - transparency1 / 4.0, null); sg.print("Please enter a length"); double length2 = sg.keyReadDouble(); if(length2 < 0) { length2 = -length2; } else if(length2==0) { length2 = DEFAULT_SIZE; } sg.print("Please enter a transparency"); double transparency2 = sg.keyReadDouble(); if(transparency2 < 0) { transparency2 = 0; } else if(transparency2 > 1) { transparency2 = 1; } sg.print("Please enter a color"); colorString = sg.keyReadString(); Color color2 = DEFAULT_COLOR; if("white".equalsIgnoreCase(colorString)) { color2 = Color.WHITE; } else if("red".equalsIgnoreCase(colorString)) { color2 = Color.RED; } else if("orange".equalsIgnoreCase(colorString)) { color2 = Color.ORANGE; } else if("yellow".equalsIgnoreCase(colorString)) { color2 = Color.YELLOW; } else if("green".equalsIgnoreCase(colorString)) { color2 = Color.GREEN; } else if("blue".equalsIgnoreCase(colorString)) { color2 = Color.BLUE; } else if("gray".equalsIgnoreCase(colorString)) { color2 = Color.GRAY; } else if("magenta".equalsIgnoreCase(colorString)) { color2 = Color.MAGENTA; } else if("cyan".equalsIgnoreCase(colorString)) { color2 = Color.CYAN; } else if("pink".equalsIgnoreCase(colorString)) { color2 = Color.PINK; } else { sg.print("couldn't understand your color, so defaulting to black"); } sg.drawFilledBox(0, length1 / 2.0, width, length2, color2, transparency2, null); sg.drawFilledBox(width+SPACE_BETWEEN, length1 / 2.0, width, length2, color2, transparency2 / 2.0, null); sg.drawFilledBox(2 * (width+SPACE_BETWEEN), length1 / 2.0, width, length2, color2, 1 - transparency2 / 2.0, null); sg.drawFilledBox(3 * (width+SPACE_BETWEEN), length1 / 2.0, width, length2, color2, transparency2 / 4.0, null); sg.drawFilledBox(4 * (width+SPACE_BETWEEN), length1 / 2.0, width, length2, color2, 1 - transparency2 / 4.0, null); sg.print("Please enter a length"); double length3 = sg.keyReadDouble(); if(length3 < 0) { length3 = -length3; } else if(length3==0) { length3 = DEFAULT_SIZE; } sg.print("Please enter a transparency"); double transparency3 = sg.keyReadDouble(); if(transparency3 < 0) { transparency3 = 0; } else if(transparency3 > 1) { transparency3 = 1; } sg.print("Please enter a color"); colorString = sg.keyReadString(); Color color3 = DEFAULT_COLOR; if("white".equalsIgnoreCase(colorString)) { color3 = Color.WHITE; } else if("red".equalsIgnoreCase(colorString)) { color3 = Color.RED; } else if("orange".equalsIgnoreCase(colorString)) { color3 = Color.ORANGE; } else if("yellow".equalsIgnoreCase(colorString)) { color3 = Color.YELLOW; } else if("green".equalsIgnoreCase(colorString)) { color3 = Color.GREEN; } else if("blue".equalsIgnoreCase(colorString)) { color3 = Color.BLUE; } else if("gray".equalsIgnoreCase(colorString)) { color3 = Color.GRAY; } else if("magenta".equalsIgnoreCase(colorString)) { color3 = Color.MAGENTA; } else if("cyan".equalsIgnoreCase(colorString)) { color3 = Color.CYAN; } else if("pink".equalsIgnoreCase(colorString)) { color3 = Color.PINK; } else { sg.print("couldn't understand your color, so defaulting to black"); } sg.drawFilledBox(0, (length1 + length2 / 2.0) / 2.0, width, length3, color3, transparency3, null); sg.drawFilledBox(width+SPACE_BETWEEN, (length1 + length2 / 2.0) / 2.0, width, length3, color3, transparency3 / 2.0, null); sg.drawFilledBox(2 * (width+SPACE_BETWEEN), (length1 + length2 / 2.0) / 2.0, width, length3, color3, 1 - transparency3 / 2.0, null); sg.drawFilledBox(3 * (width+SPACE_BETWEEN), (length1 + length2 / 2.0) / 2.0, width, length3, color3, transparency3 / 4.0, null); sg.drawFilledBox(4 * (width+SPACE_BETWEEN), (length1 + length2 / 2.0) / 2.0, width, length3, color3, 1 - transparency3 / 4.0, null); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.