Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is...
-
date post
22-Dec-2015 -
Category
Documents
-
view
219 -
download
2
Transcript of Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is...
![Page 1: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/1.jpg)
Java Review
Structure of a graphics program
![Page 2: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/2.jpg)
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
![Page 3: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/3.jpg)
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
![Page 4: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/4.jpg)
Computer Graphics and User Interfaces
Structure of a Java Program
public class <classname> {// body of class goes here// Data members// Constructors// Methods}
![Page 5: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/5.jpg)
Computer Graphics and User Interfaces
The main method
public static void main( String [] args) {
// code to make the program do// what it needs to
}
![Page 6: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/6.jpg)
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
![Page 7: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/7.jpg)
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");
![Page 8: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/8.jpg)
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");
![Page 9: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/9.jpg)
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
![Page 10: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/10.jpg)
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
![Page 11: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/11.jpg)
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)
![Page 12: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/12.jpg)
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); }
}
![Page 13: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/13.jpg)
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
![Page 14: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/14.jpg)
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 }}
![Page 15: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/15.jpg)
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);}
![Page 16: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/16.jpg)
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.
![Page 17: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/17.jpg)
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
![Page 18: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/18.jpg)
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
![Page 19: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/19.jpg)
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);
![Page 20: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/20.jpg)
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
![Page 21: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/21.jpg)
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
![Page 22: Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d7d5503460f94a5fd97/html5/thumbnails/22.jpg)
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);