Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110: Spring 20091 Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday.

Similar presentations


Presentation on theme: "COMP 110: Spring 20091 Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday."— Presentation transcript:

1 COMP 110: Spring 20091 Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday

2 COMP 110: Spring 20092 Today in COMP 110 Java Graphics Programming Demo

3 COMP 110: Spring 20093 Computer Graphics Computer Graphics is about producing images with a computer This can be useful in a variety of ways User interfaces (dialog boxes, icons, windows) Special effects (movies, games, etc) Image editing (photoshop) Visualizing scientific data

4 COMP 110: Spring 20094 Graphics in Java Java provides some useful classes & functionality for adding graphics to your programs

5 COMP 110: Spring 20095 Your First Graphics Program import javax.swing.JApplet; import java.awt.Graphics; public class HappyFace extends JApplet { public void paint(Graphics canvas) { canvas.drawOval(100, 50, 200, 200); canvas.fillOval(155, 100, 10, 20); canvas.fillOval(230, 100, 10, 20); canvas.drawArc(150, 160, 100, 50, 180, 180); } Where’s the main method?

6 COMP 110: Spring 20096 Applets public class HappyFace extends JApplet Our HappyFace class inherits from the class JApplet This means that our program is an applet instead of a normal application An applet is program that is meant to be run in a web browser We’ll use it as an easy way to get some graphics capability

7 COMP 110: Spring 20097 Applets Applets do not have a main method They can’t be run like a normal application (using the red running man icon in jGRASP We need to use the apple icon to run the applet

8 COMP 110: Spring 20098 Drawing to Screen public void paint(Graphics canvas) { canvas.drawOval(100, 50, 200, 200); canvas.fillOval(155, 100, 10, 20); canvas.fillOval(230, 100, 10, 20); canvas.drawArc(150, 160, 100, 50, 180, 180); } Applets use a method called paint in order to draw to screen This method is called for you automatically when you start the applet

9 COMP 110: Spring 20099 Screen Coordinate System (0,0) x-axis y-axis (100,50) 50 pixels 100 pixels

10 COMP 110: Spring 200910 Drawing Shapes //draw oval at position 100, 50, of width and height 200 canvas.drawOval(100, 50, 200, 200); //draw a “filled-in” oval at position 155, 100 with w=10, h=20 canvas.fillOval(155, 100, 10, 20); //draw a “filled-in” oval at position x=230, y=100 with w=10, h=20 canvas.fillOval(230, 100, 10, 20); //draw an arc, details in API canvas.drawArc(150, 160, 100, 50, 180, 180);

11 COMP 110: Spring 200911 The Graphics Class What is the canvas object? An object of the Graphics class The graphics class knows how to draw things like shapes to the screen

12 COMP 110: Spring 200912 Setting the Color The Graphics class also provides the capability to change the color that shapes are drawn with Example //draw a yellow oval canvas.setColor(Color.YELLOW); canvas.drawOval(100, 50, 200, 200); //now draw a black oval canvas.setColor(Color.BLACK); canvas.drawOval(100, 50, 200, 200);

13 COMP 110: Spring 200913 Drawing Text to the Screen We can display text on the screen using the drawString method drawString(string, xPos, yPos) Example //print at string and position (60, 75) canvas.drawString("This will be printed to the screen", 60, 75);

14 COMP 110: Spring 200914 String Drawing Example import javax.swing.JApplet; import java.awt.Graphics; public class HappyFace extends JApplet { public void paint(Graphics canvas) { canvas.drawOval(100, 50, 200, 200); canvas.fillOval(155, 100, 10, 20); canvas.fillOval(230, 100, 10, 20); canvas.drawArc(150, 160, 100, 50, 180, 180); canvas.drawString("This is a happy face!", 50, 50); }

15 COMP 110: Spring 200915 Drawing Polygons A polygon is a closed figure made up of line segments that do NOT cross

16 COMP 110: Spring 200916 Drawing Polygons You can draw a polygon by giving the locations of the corners There are two functions you can use drawPolygon Draws an unfilled polygon (just the edges) fillPolygon Draw a filled polygon p1 p2 p3 p4 p5

17 COMP 110: Spring 200917 Drawing Polygons Syntax canvas.drawPolygon(X_Array, Y_Array, Num_Points) canvas.fillPolygon(X_Array, Y_Array, Num_Points) Example int[] xHouse = {150, 150, 200, 250, 250}; int[] yHouse = {100, 40, 20, 40, 100}; canvas.drawPolygon(xHouse, yHouse, xHouse.length);

18 COMP 110: Spring 200918 Creating Dialog Boxes Dialog boxes are special windows that can be used to take input from the user or to display output

19 COMP 110: Spring 200919 Dialog Boxes Dialog boxes can be created using the JOptionPane class import javax.swing.JOptionPane; public class JOptionPaneDemo { public static void main(String[] args) { String name = JOptionPane.showInputDialog("Enter your name:"); JOptionPane.showMessageDialog(null, "Hi " + name + "!"); System.exit(0); }

20 COMP 110: Spring 200920 Input Dialogs Input dialogs take input from the user in the form of a string We use the showInputDialog method of the JOptionPane class Example String name = JOptionPane.showInputDialog("Enter your name:")

21 COMP 110: Spring 200921 Output Dialogs Output dialogs are used to display a message to the user We use the showMessageDialog method of the JOptionPane class Example JOptionPane.showMessageDialog(null, "Hi " + name + "!");

22 COMP 110: Spring 200922 Methods of JOptionPane If showInputDialog and showMessage Dialog are called using the class name JOptionPane, what kind of methods must they be? Static, a JOptionPane object is never created String name = JOptionPane.showInputDialog("Enter your name:"); JOptionPane.showMessageDialog(null, "Hi " + name + "!");

23 COMP 110: Spring 200923 Exiting the Program public static void main(String[] args) { String name = JOptionPane.showInputDialog("Enter your name:"); JOptionPane.showMessageDialog(null, "Hi " + name + "!"); System.exit(0); } At the end of the program we called System.exit(0) This is necessary to get the program end when using things like windows/dialog boxes etc.

24 COMP 110: Spring 200924 Confirmation Windows We can also create dialog boxes that provide the user with the options “Yes” and “No” These are called confirmation windows

25 COMP 110: Spring 200925 Confirmation Windows import javax.swing.JOptionPane; public class JOptionPaneDemo2 { public static void main(String[] args) { while(true) { int answer = JOptionPane.showConfirmDialog(null, "End Program?", "Click Yes or No:", JOptionPane.YES_NO_OPTION); if(answer == JOptionPane.YES_OPTION) System.exit(0); else if(answer == JOptionPane.NO_OPTION) System.out.println("One more time"); else System.out.println("Error"); } } }

26 COMP 110: Spring 200926 Creating the Window int answer = JOptionPane.showConfirmDialog(null, "End Program?", "Click Yes or No:", JOptionPane.YES_NO_OPTION);

27 COMP 110: Spring 200927 Checking the Result if(answer == JOptionPane.YES_OPTION) System.exit(0); else if(answer == JOptionPane.NO_OPTION) System.out.println("One more time"); else System.out.println("Error");

28 COMP 110: Spring 200928 Programming Demo Expand the House Fill the house with a color Draw a door Draw a window Add Text

29 COMP 110: Spring 200929 Programming Demo (150, 100) (150, 40) (200, 20) (250, 40) (250, 100) (0,0) x-axis y-axis (175, 100) (175, 60) (200, 60) (200, 100) (220, 60) (220, 80)(240, 80) (240, 60) (150, 120)

30 COMP 110: Spring 200930 Programming Demo Programming

31 COMP 110: Spring 200931 Wednesday Quiz


Download ppt "COMP 110: Spring 20091 Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday."

Similar presentations


Ads by Google