Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.

Similar presentations


Presentation on theme: "Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution."— Presentation transcript:

1 Java Review Structure of a graphics program

2 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

3 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

4 Computer Graphics and User Interfaces Structure of a Java Program public class { // body of class goes here // Data members // Constructors // Methods }

5 Computer Graphics and User Interfaces The main method public static void main( String [] args) { // code to make the program do // what it needs to }

6 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

7 Computer Graphics and User Interfaces Creating Objects You declare an object ; JFrame frame; Create an object using new = new (); frame = new JFrame(); Some constructors have parameters –provide appropriate arguments when you call them frame = newFrame( "frame title");

8 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. ( ); frame.setTitle( "My Picture");

9 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

10 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

11 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)

12 Computer Graphics and User Interfaces Defining a Frame class public class extends JFrame { // constructor used to create a Frame object public { // create a panel setContentPane( panel); }

13 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

14 Computer Graphics and User Interfaces Defining a Panel class public class extends JPanel { // constructor if needed public void paintComponent( Graphics g) { // code for drawing here } }

15 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); }

16 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.

17 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

18 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

19 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);

20 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

21 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

22 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);


Download ppt "Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution."

Similar presentations


Ads by Google