java review structure of a graphics program. computer graphics and user interfaces java is...
Post on 22-Dec-2015
219 views
TRANSCRIPT
Java Review
Structure of a graphics program
Computer Graphics and User Interfaces
Java is Object-Oriented
• A program uses objects to model the solution to the problem being solved• Classes are used to define new data types needed by the program
– Java comes with a large collection of classes that have already been written
•Inheritance can be used to build on existing classes
Computer Graphics and User Interfaces
Java Programs
• A java program consists of one or more classes• Each class is defined in a file whose name is the name of the class followed by the extension .java
– The class named Demo2D is defined in the file Demo2D.java
• One class must contain a main method•The program executes by creating objects and calling their methods
Computer Graphics and User Interfaces
Structure of a Java Program
public class <classname> {// body of class goes here// Data members// Constructors// Methods}
Computer Graphics and User Interfaces
The main method
public static void main( String [] args) {
// code to make the program do// what it needs to
}
Computer Graphics and User Interfaces
Constructors
• All instantiable classes have at least one constructor– the constructor is used to create an
object– the name of the constructor is the
same as the name of the class– a constructor does not have a return
type
Computer Graphics and User Interfaces
Creating Objects
• You declare an object <classname> <objectname>;JFrame frame;
• Create an object using new <objectname> = new <classname>();frame = new JFrame();
• Some constructors have parameters– provide appropriate arguments when you call
themframe = newFrame( "frame title");
Computer Graphics and User Interfaces
Calling methods
• Call a method by giving the object name and the method name followed by a list of arguments if needed<objectname>.<methodname>(<args>);
frame.setTitle( "My Picture");
Computer Graphics and User Interfaces
Graphical Programs
• Graphical programs need a window– Frame and JFrame are used for
stand-alone graphical applications– Applet and JApplet are used for
programs that run in a browser
Computer Graphics and User Interfaces
Java Graphics Libraries
• AWT classes (e.g. Frame, Applet) were provided with original version of Java– import from java.awt package
• Swing classes (e.g. JFrame, JApplet) were added later to provide better cross-platform behavior– import from javax.swing package
Computer Graphics and User Interfaces
Using JFrame
• Create a class that extends JFrame • A JFrame has a content pane that
serves as – a container for other GUI components – a place to draw graphics
• Create a panel to use as the content pane– use setContentPane( panel)
Computer Graphics and User Interfaces
Defining a Frame class
public class <classname> extends JFrame {// constructor used to create a Frame object
public <classname>{// create a panelsetContentPane( panel); }
}
Computer Graphics and User Interfaces
Creating a panel
• Define a class that extends JPanel• Override the paintComponent method which
controls what is drawn in the panel• Create a panel using new
PanelClass panel
= new PanelClass()• Set the content pane of the frame to the panel
Computer Graphics and User Interfaces
Defining a Panel class
public class <classname> extends JPanel {// constructor if needed
public void paintComponent( Graphics g) {
// code for drawing here }}
Computer Graphics and User Interfaces
The main method
public static void main( String [] args) { FrameClass frame = new FrameClass(); frame.setVisible( true); frame.setDefaultClosingOperation(
Jframe.EXIT_ON_CLOSE);}
Computer Graphics and User Interfaces
Graphics Class
• Graphics class creates an object that can be used as a context for drawing
• The state of a Graphics object includes such properties as foreground color and font.
• Positions within the Graphics context are measured from the upper left corner and they have units of pixels.
Computer Graphics and User Interfaces
Graphics Methods
• Get and set methods are provided for properties like color, font, stroke, …
• There are methods for drawing lines, rectangles, ovals, …
• There is a method for drawing text• There is a method for displaying images
Computer Graphics and User Interfaces
Graphics2D extends Graphics
• It has more methods than Graphics• You can cast a Graphics to a
Graphics2D to use these methods
Graphics2D g2 = (Graphics2D)g• There is a method for drawing Shape
objects
Computer Graphics and User Interfaces
Shapes
• Shape is an interface used as a supertype for various graphical shapes– Rectangle– Ellipse2D– Polygon
• Drawing a shape on Graphics2D g2
g2.draw( shapeObject);
Displaying a String
• The drawString method can be used to
display text in a Graphics context.
void drawString( String text,
int x, int y;
* The position of the text relative to x and y
is shown in the figure
Computer Graphics and User Interfaces
Color Class
• The java.awt.Color class allows us to create a color
object.
• Color class has public constants for common colors:
– Gray scale: Color.black
– Primary colors: Color.red Color.green Color.blue
– Secondary colors: Color.yellow Color.cyan Color.magenta
– Others: Color.orange
Computer Graphics and User Interfaces
Custom Colors
• Create custom colors by specifying three
values ranging from 0 to 255 for red,
green, and blue. Color pinkColor
= new Color(255,175,175);