Download Graphics and Objects in Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
Java Applications
• Consist of a class definition
• Example
public class name {
instance variables
methods (like main)
public static void main(String args[]) {
statements
}
}
• Most Java programs use and define
multiple classes
1
Object Oriented Programming in Java
• Objects simulate real world entities
• Object Oriented Programming (OOP)
– Encapsulates
• Instance variables (attributes)
• Methods (behaviors)
– Allows objects to communicate
• Well-defined interfaces
– Classes define what an object is
– Objects are instances of classes
1
Java API
• Java is comprised of many predefined
classes
– See Java
APIhttp://java.sun.com/j2se/1.5.0/docs/api/index.ht
ml
• We have already used some of these
– System
– Scanner
– Math
• We will use and create many others
1
Naming Conventions
• Classes simulate entities
– Identify using noun phrases
– Capatalize first letter and first letter of other words
• Instance variables hold attributes
– Identify using noun phrases
– Use lower case first letter and uppercase first letter of
other words
• Methods represent actions
– Identify using a verb phrase
– Use lower case first letter and uppercase first letter of
other words
1
JFrame Class
• A JFrame is a top-level window with a title
and a border
• Used to create graphical applications
– We add other components to our JFrame
• Deals with operating system issues
1
JPanel Class
• A JPanel is a container for
– Holding other graphical objects
– Specialized drawing
• Has a paintComponent method
– By default, paintcomponent has empty body
– Override (redefine) paintComponent in our class
• Done using the extends keyword
• A feature of object oriented programming, called
inheritance
• Draws itself by calling the paintComponent
method when created
1
Graphical Java Applications
• Consist of two or more class definitions
• See Figures 4.19 and 4.20
• Notes
– DrawPanelTest
• Is the graphical application
• uses DrawPanel class
• Uses a JFrame class
– DrawPanel
• Adds specialization to a JPanel
• Graphical operations performed in PaintComponent()
• Uses Graphics class
1
Painting
• Method paintComponent
– Draws graphics on screen
– void indicates paintComponent returns nothing when
finishes task
– paintComponent gets parameters automatically
• Graphics object used by paintComponent
– Mimic paintComponent's first line
• super.paintComponent(g)
• Calls version of method from superclass
• Should be first statement in paintComponent method
• Typical of what we need to to when we inherit from an
existing class
1
Java Screen Coordinates
• Java coordinate system
– Measured in pixels (picture elements)
– Upper left is (0,0)
+x
(0, 0)
X a xis
(x , y )
+y
Y a xis
1
Painting using the Graphics Class
• See the Graphics class API for more details
• Called using Graphics object g and dot (.)
followed by method name, then parentheses
with arguments
• Parentheses define parameter list
– Methods receive data from calling methods to perform
tasks
– Parameters are defined by position in list
– Type and numbers of parameters must match
specification
1
Drawing Strings
• drawString(String str, int x, int y)
– str - the string to be drawn.
– x - the x coordinate.
– y - the y coordinate.
• Draws the text given by the specified string
– Uses the current font and color
– The baseline of the leftmost character is at position
(x, y) in this graphics context's coordinate system.
1
Drawing Lines
• DrawLine(int x1, int y1, int x2, int y2)
–
–
–
–
x1 - the first point's x coordinate.
y1 - the first point's y coordinate.
x2 - the second point's x coordinate.
Y2 - the second point's y coordinate.
• Draws a line
– using the current color
– between the points (x1, y1) and (x2, y2)
1
Drawing Rectangles
• DrawRect(int x, int y, int width, int height)
–
–
–
–
x - the x coordinate of the rectangle to be drawn.
y - the y coordinate of the rectangle to be drawn.
width - the width of the rectangle to be drawn.
Height - the height of the rectangle to be drawn.
• Draws the outline of the specified
rectangle.
– The left and right edges of the rectangle are at x
and x + width.
– The top and bottom edges are at y and y + height.
1
Drawing Rounded Rectangles
• drawRoundRect(int x, int y, int width, int
height, int arcWidth, int arcHeight)
–
–
–
–
–
x - the x coordinate of the rectangle to be drawn.
y - the y coordinate of the rectangle to be drawn.
width - the width of the rectangle to be drawn.
height - the height of the rectangle to be drawn.
arcWidth - the horizontal diameter of the arc at the
four corners.
– arcHeight - the vertical diameter of the arc at the
four corners.
• Draws an outlined round-cornered
rectangle.
1
Drawing Filled Rectangles
• fillRect(int x, int y, int width, int height)
– Fills the rectangle
– Uses same parameters as drawRect
• fillRoundRect(int x, int y, int width, int
height, int arcWidth, int arcHeight)
– Fills the round-cornered rectangle.
– Uses same parameters as drawRoundRect
1
Drawing Ovals
• drawOval(int x, int y, int width, int height)
– x - the x coordinate of the upper left corner of the
oval to be drawn.
– y - the y coordinate of the upper left corner of the
oval to be drawn.
– width - the width of the oval to be drawn.
– Height - the height of the oval to be drawn.
• Draws the outline of an oval.
– The result is a circle or ellipse that fits within the
rectangle specified by the x, y, width, and height
arguments.
– The oval covers an area that is width + 1 pixels
wide and height + 1 pixels tall.
1
Drawing Arcs
• drawArc(int x, int y, int width, int height, int
startAngle, int arcAngle)
– x - the x coordinate of the upper-left corner of the
arc to be drawn.
– y - the y coordinate of the upper-left corner of the
arc to be drawn.
– width - the width of the arc to be drawn.
– height - the height of the arc to be drawn.
– startAngle - the beginning angle.
– ArcAngle - the angular extent of the arc, relative to
the start angle.
1
Drawing Arcs
• Draws the outline of a circular or elliptical
arc covering the specified rectangle.
– The resulting arc begins at startAngle and extends
for arcAngle degrees, using the current color.
– Angles are interpreted such that
• 0 degrees is at the 3 o'clock position.
• A positive value indicates a counter-clockwise rotation
• A negative value indicates a clockwise rotation.
– The center of the arc is the center of the rectangle
whose origin is (x, y) and whose size is specified by
the width and height arguments.
1
Drawing Filled Ovals and Arcs
• fillOval(int x, int y, int width, int height)
– Uses same parameters as drawOval
• fillArc(int x, int y, int width, int height, int
startAngle, int arcAngle)
– Uses same parameters as drawArc
1
Color
• setColor(Color c)
– c - the new rendering color.
• Sets this graphics context's current color to
the specified color.
• Uses this color for drawing until it is
changed by another SetColor call
1
Color Class
• Java uses a Color class to model colors
– Uses the tristimulus red, green, blue (RGB) model
• We can create a Color object in several
ways including
– Color(float r, float g, float b)
• Creates an RGB color with the specified red, green,
and blue values in the range (0.0 - 1.0).
– Color(int r, int g, int b)
• Creates an RGB color with the specified red, green,
and blue values in the range (0 - 255).
• See the Color API specification for more
details
1
Anatomy of a Java Class
• A Java class has several parts
– A name
– An optional set of instance variables
– An optional set of methods
1
Java Class Names
• A Java class name
– Identifies the class to other java entities
– Should have an upper case first letter to follow
conventions
• Example
public class SillyClass {}
– This is the trivial class
– It stores nothing
– It can manipulate nothing
1
Java Instance Variables
• Any set of variables can be defined
• Define before any methods
• These can be used in all methods
• Sometimes are called global variables
• Some classes might not have any methods
1
Java Methods
• Define how the class interacts with others
• Methods have parameters
– Similar to mathematical functions
– Parameters define the contents of a message
• Methods have a return value (can be void)
• Several special types of methods
–
–
–
–
–
Constructor
Accessor
Mutator
Main
toString
• A class may not have any methods
1
Constructor Methods
• Defines how an instance (object) of the
class can be created
• Defined using the form
public ClassName(parameters) { stuff }
• Classes create objects with the new
keyword followed by class name
– Often inintialize a subset of the instance variables
• Most, but not all, classes have constructors
• Examples
Scanner input = new Scanner(System.in);
JFrame app = new JFrame();
1
Accessor Methods
• Defines how an instance (object) of the
class can be accessed
• Defined using the form
public type methodName(parameters)
• Since these are actions, a verb-phrase is
used to define the method name
• Often are of the form getStuff()
1
Mutator Methods
• Defines how an instance (object) of the
class can be modified
• Defined using the form
public type methodName(parameters)
• Since these are actions, a verb-phrase is
used to define the method name
• Often are of the form setStuff() with a return
type of void
1
Main Method
• The main method defines how the program
starts
• Always has the form
public static void main(String args[ ]) {
statements;
}
1
toString Method
• The toString method defines how the class
can be converted to a human readable
string of characters
• Always has the form
public String toString() {
statements;
return someString;
}