public class UI
extends java.lang.Object
The input panel will only be displayed if it is used - if your program doesn't have any buttons etc, then only the text and graphics panes will be visible.
UI provides methods for:
Modifier and Type | Method and Description |
---|---|
static void |
clearText()
Clear the contents of the text output region.
|
static void |
print(anything value)
Print a value to the text pane.
|
static void |
printf(String format,
Object... args)
The printf() method requires a format string (which
will contain "holes" specified with %'s) and additional arguments
which will be placed "in the holes", using the specified formatting.
|
static void |
println()
Start a new line on the text pane.
|
static void |
println(anything value)
Print a value to the text pane and start a new line.
|
static void |
printMessage(String s)
Print a message to the message line.
|
Modifier and Type | Method and Description |
---|---|
static String |
askString(String question)
Ask the user for a string.
|
static double |
askDouble(String question)
Ask the user for a number.
|
static int |
askInt(String question)
Ask the user for an integer.
|
static boolean |
askBoolean(String question)
Ask the user for a boolean value.
|
static String |
askToken(String question)
Ask the user for a single token.
|
static ArrayList<Double> |
askNumbers(String question)
Ask the user for a list of numbers.
|
static ArrayList<String> |
askStrings(String question)
Ask the user for a list of strings.
|
Modifier and Type | Method and Description |
---|---|
static String |
next()
Read and return the next token of the user's input.
|
static boolean |
nextBoolean()
Read the next token of the user's input.
Return true if it is "yes", "y", or "true", return false if it is "no", "n", or "false" (case insensitive). Throws an exception if it is anything else. |
static double |
nextDouble()
Read the next token of the user's input.
Return it as a double if it is a number. Throws an exception if it is not a number. |
static int |
nextInt()
Read the next token of the user's input.
Return it as an int if it is an integer. Throws an exception if it is not an integer. |
static String |
nextLine()
Read the remaining characters of the user's input up to (but not including) the next end-of-line
and return them as a string.
Reads and throws away the end-of-line character. If there are no characters on the line, then it returns an empty string (""). |
static boolean |
hasNext()
Returns true, but waits for the user to type something if necessary.
|
static boolean |
hasNextBoolean()
Returns true if the next token in the user input is "yes", "true", "no" or "false'".
Waits for the user to type something if necessary. |
static boolean |
hasNextDouble()
Returns true if the next token in the user input is a number.
Waits for the user to type something if necessary. |
static boolean |
hasNextInt()
Returns true if the next token in the user input is an integer.
Waits for the user to type something if necessary. |
static boolean |
hasNextLine()
Returns true, but waits for the user to type something if necessary.
|
Modifier and Type | Method and Description |
---|---|
static void |
clearGraphics()
Clear the contents of the graphics output region.
|
static void |
setColor(java.awt.Color col)
Set the color of the brush in the graphics output region.
This will be the color of any shapes drawn after this. See the Color class for how to specify a color.
|
static void |
setFontSize(double size)
Set the point size of the Font of String to be drawn in the graphics output region.
This will be the size of the font of any String drawn after this.
|
static void |
setLineWidth(double width)
Set the width of the lines (in pixels) for lines drawn in the graphics output region.
This will be the width of lines drawn with drawLine and the outlines
of shapes drawn after this.
|
static void |
setImmediateRepaint(boolean immediate)
Set the "immediate repaint" status of the UI drawing commands.
If the status is true (the default), then all the draw, fill, erase, invert methods will have an immediately visible effect. This is the simplest way of drawing If the status is false, then all the draw, fill, erase, invert methods will draw in the background, but will not update the graphics pane so that the effect will not be immediately visible. To show all the changes on the graphics pane, the program should call the repaintGraphics method (below). Setting immediate repaint to false allows faster and smoother animation, but is more complicated because the program must call repaintGraphics at the right time. |
static void |
repaintGraphics()
Repaint the contents of the graphics output region.
Used after drawing shapes if immediateRepaint was set to be false. |
static void |
drawLine(double x1, double y1, double x2, double y2) eraseLine(double x1, double y1, double x2, double y2) invertLine(double x1, double y1, double x2, double y2)
Draw/erase/invert a line in the graphics output
region from (x1, y1) to (x2, y2).
|
static void |
drawRect(double left, double top, double width, double height) fillRect(double left, double top, double width, double height) eraseRect(double left, double top, double width, double height) invertRect(double left, double top, double width, double height)
Draw/fill/erase/invert a rectangle in the graphics output region. left and top specify the position of the
top left corner of the rectangle, and width and height specify its size.
|
static void |
drawOval(double left, double top, double width, double height) fillOval(double left, double top, double width, double height) eraseOval(double left, double top, double width, double height) invertOval(double left, double top, double width, double height)
Draw/fill/erase/invert an oval in the graphics output region. left and top specify
the left edge and the top of the oval; width and height specify
the size of the oval.
|
static void |
drawString(String s, double x, double y) eraseString(String s, double x, double y) invertString(String s, double x, double y)
Draw/erase/invert the given string in the graphics output region at the position (left, baseline).
|
static void |
drawArc(double left, double top, double width, double height, double startAngle,
double arcAngle) fillArc(double left, double top, double width, double height, double startAngle,
double arcAngle) eraseArc(double left, double top, double width, double height, double startAngle,
double arcAngle) invertArc(double left, double top, double width, double height,
double startAngle, double arcAngle)
Draw/fill/erase/invert an arc in the graphics output region. An arc is a segment of an oval. The
first four parameters (left, top, width, height) specify the left, top
and size of the whole oval. startAngle and arcAngle specify
which segment of the oval will be drawn: the arc starts at startAngle
degrees (measured anti-clockwise from the x axis 0
degrees), and extends anti-clockwise for arcAngle degrees from the
startAngle. A filled arc will be "pie-shaped" - bounded by the arc and two lines
from each end of the arc to the center of the oval.
|
static void |
drawImage(String fileName, double left, double top) drawImage(String fileName, double left, double top, double width, double height) drawImage(java.awt.Image img, double left, double top) drawImage(java.awt.Image img, double left, double top, double width, double height)
Draw an image at the position (left,top). The image can be specified as the name of a file containing the image or as an Image object.
With the additional arguments, width and height, scale the image to these dimensions. |
static void |
eraseImage(String fileName, double left, double top) eraseImage(java.awt.Image img, double left, double top)
Erase an image at the position (left, top). The image can be specified as the name of a file containing the image or as an Image object.
Note, if the image was scaled, the image can be erased using eraseRect(left, top, width, height) |
static void |
drawPolygon(double[] xPoints, double[] yPoints, int nPoints) fillPolygon(double[] xPoints, double[] yPoints, int nPoints) erasePolygon(double[] xPoints, double[] yPoints, int nPoints) invertPolygon(double[] xPoints, double[] yPoints, int nPoints)
Draw/fill/erase/invert a polygon in the graphics output region.
The polygon is specified by two arrays containing the x coordinates and the y coordinates of the series of vertices of the polygon, and the number of vertices. |
Modifier and Type | Method and Description |
---|---|
static javax.swing.JButton |
addButton(String name,
UIButtonListener action)
Add a button to the input panel on the left side of the gui window.
action must be a function with no parameters. |
static void |
addSlider(String s,
double min,
double max,
ecs100.UISliderListener action) addSlider(String s,
double min,
double max,
double initial,
UISliderListener action)
Add a slider to the input panel on the left side of the gui window.
Must specify the name, and the minimum and maximum values of the slider. You may also specify the initial value. Note that the slider only returns integer values, so a range 0 to 10 works (with 10 possible values for the slider), but a range of 0 to 1 won't work, because there is only one possible value. action must be a function with one parameter - a double. |
static void |
addTextField(String s,
UITextFieldListener action)
Add a text field to the input panel on the left side of the gui window.
action must be a function with one String parameter. |
static void |
setMouseListener(UIMouseListener action)
Enable the user to use the mouse on the graphics pane, recognising presses, releases, and clicks.
action must be a function with three parameters - a String, and two doubles. |
static void |
setMouseMotionListener(UIMouseListener action)
Enable the user to use the mouse on the graphics pane, recognising drags and moves, in addition to presses, releases,
and clicks.
action must be a function with three parameters - a String, and two doubles. |
static void |
setKeyListener(UIKeyListener action)
Enable the user to press keys for intput on the graphics pane.
action must be a function with one String parameter. |
Modifier and Type | Method and Description |
---|---|
static void |
initialise()
Ensure that the UI window is initialised
|
static void |
sleep(double millis)
Causes the program to pause for a specified number of milliseconds.
|
static void |
quit()
Get rid of the interface windows, which will usually halt the program.
|
static void |
clearPanes()
Clear both the text pane and the graphics pane
|
static void |
setWindowSize(int width, int height)
Sets the width and height of the text and graphics area.
|
static void |
setDivider(double position)
Move the divider between the text and graphics
panes to the specified fraction. The value of position should
be between 0.0 (at the left side - only graphics visible) and 1.0 (at
right side - only text visible). A negative value means put it at the
initial default position.
This should be called after all the buttons etc have been added and the size of the text and graphics area has been set. |
static javax.swing.JFrame |
getFrame()
Return the JFrame of the UI window.
Only needed if you want to do more complicated stuff.
|
static java.awt.Graphics2D |
getGraphics()
Return the Graphics2D object underlying the
graphics pane. Only needed if you want to do more complicated
operations on the graphics pane than are provided by methods in this
UI class.
|
static void |
repaintAllGraphics()
Repaint all the contents of the graphics output region.
Ignores the "dirty" marker. |
static int |
getCanvasHeight() getCanvasWidth()
Returns the height or the width of the canvas in
the graphics pane. Note, this is the size of the actual drawing area,
not necessarily the part of it that is currently visible in the
window. Changing the size of the window will not change the size of
the canvas, it only changes how much of it is visible.
|
public static void initialise()
public static void clearGraphics()
public static void repaintGraphics()
public static void setColor(java.awt.Color col)
public static void setForeGround(java.awt.Color col)
public static void setFontSize(double size)
public static void setLineWidth(double width)
public static void drawLine(double x1, double y1, double x2, double y2)
public static void eraseLine(double x1, double y1, double x2, double y2)
public static void invertLine(double x1, double y1, double x2, double y2)Inverting a line will draw the line by "inverting" the color of the pixels underneath the outline in a way that inverting a second time will restore the original color.
public static void drawRect(double x, double y, double width, double height)
public static void fillRect(double x, double y, double width, double height)
public static void eraseRect(double x, double y, double width, double height)
public static void invertRect(double x, double y, double width, double height)
public static void drawString(String s, double x, double y)
public static void drawString(String s, double x, double y, boolean redraw)
public static void eraseString(String s, double x, double y)
public static void invertString(String s, double x, double y)
public static void drawOval(double x, double y, double width, double height)
public static void fillOval(double x, double y, double width, double height)
public static void eraseOval(double x, double y, double width, double height)
public static void invertOval(double x, double y, double width, double height)
public static void drawArc(double x, double y, double width, double height, double startAngle, double arcAngle)
public static void fillArc(double x, double y, double width, double height, double startAngle, double arcAngle)
public static void eraseArc(double x, double y, double width, double height, double startAngle, double arcAngle)
public static void invertArc(double x, double y, double width, double height, double startAngle, double arcAngle)
public static void drawImage(String fileName, double x, double y, double width, double height)
public static void drawImage(String fileName, double x, double y)
public static void drawImage(java.awt.Image img, double x, double y, double width, double height)
public static void drawImage(java.awt.Image img, double x, double y)
public static void eraseImage(String fileName, double x, double y)
public static void eraseImage(java.awt.Image img, double x, double y)
public static void drawPolygon(double[] xPoints, double[] yPoints, int nPoints)
public static void fillPolygon(double[] xPoints, double[] yPoints, int nPoints)
public static void erasePolygon(double[] xPoints, double[] yPoints, int nPoints)
public static void invertPolygon(double[] xPoints, double[] yPoints, int nPoints)
public static void clearText()
public static void print(String s)
public static void print(boolean b)
public static void print(char c)
public static void print(double d)
public static void print(int i)
public static void print(Object o)
public static void println()
public static void println(String s)
public static void println(boolean b)
public static void println(char c)
public static void println(double d)
public static void println(int i)
public static void println(Object o)
public static void printf(String format, Object... args)
public static String askToken(String question)
public static String askString(String question)
public static int askInt(String question)
public static double askDouble(String question)
public static boolean askBoolean(String question)
public static ArrayList<Double> askNumbers(String question)
public static ArrayList<String> askStrings(String question)
public static String next()
public static int nextInt()
public static double nextDouble()
public static boolean nextBoolean()
public static String nextLine()
public static boolean hasNext()
public static boolean hasNextInt()
public static boolean hasNextDouble()
public static boolean hasNextBoolean()
public static boolean hasNextLine()
public static void printMessage(String s)
public static javax.swing.JButton addButton(String name, ecs100.UIButtonListener action)
addButton returns the new button, in case you want to change the color or name of the button later.
public static void addTextField(String s, ecs100.UITextFieldListener action)
A text field allows the user to enter a string value by typing the string into the field.
The first argument is the label that the text field will be given.
The second argument is the action to perform when a value is entered
in the text field. action must have one String parameter.
The action can be a method (with one String parameter) to call, eg this::processName,
Or, the action can be a simple "lambda": eg (String v) ->
this.name = v, or a more complicated "lambda": eg (String v) -> {
this.name = v; UI.clearText(); this.listDetails(); }
public static void addSlider(String s, double min, double max, ecs100.UISliderListener action)
A slider allows the user to enter a numeric value by moving the slider knob along the slider.
The arguments are the name of the slider, the minimum and maximum vales of the slider
(integers) and the action to perform when a value is set with the
slider. action must have one double parameter.
The action can be a method (with one double parameter) to call, eg this::resetSize,
Or, the action can be a simple "lambda": eg (double v) ->
this.size = v, or a more complicated "lambda": eg (double v) -> {
this.reset(); this.size = v; UI.redraw(); }
public static void addSlider(String s, double min, double max, double initial, ecs100.UISliderListener action)
public static void setKeyListener(ecs100.UIKeyListener action)
The action will be passed a string specifying the key that the user typed, eg "A", "x", "+", "HOME", etc
public static void setMouseListener(ecs100.UIMouseListener action)
The argument is the action to respond to the mouse. action must
have three parameters: a String (the kind of mouse event), and two
doubles (the x and y coordinates where the event happened).
Typically, the action will be this::doMouse, where doMouse
is a method with the header
public void doMouse(String action, double x, double y)which will be passed three values:
public static void setMouseMotionListener(ecs100.UIMouseListener listener)
public void doMouse(String action, double x, double y)which will be passed three values:
public static void sleep(double millis)
public static void quit()
public static void clearPanes()
public static void setWindowSize(int width, int height)
public static void setDivider(double position)
public static javax.swing.JFrame getFrame()
public static java.awt.Graphics2D getGraphics()
public static int getCanvasHeight()
public static int getCanvasWidth()