Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Similar presentations


Presentation on theme: "COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014."— Presentation transcript:

1 COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014

2 COMP 110: Spring 20092 Announcements Lab 8 has been graded Milestone 1 has been graded

3 COMP 110: Spring 20093 Questions?

4 COMP 110: Spring 20094 Today in COMP 110 Go over Lab 8 Java Graphics Programming Demo

5 COMP 110: Spring 20095 Lab 8

6 COMP 110: Spring 20096 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

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

8 COMP 110: Spring 20098 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); } Wheres the main method?

9 COMP 110: Spring 20099 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 Well use it as an easy way to get some graphics capability

10 COMP 110: Spring 200910 Applets Applets do not have a main method They cant 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

11 COMP 110: Spring 200911 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

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

13 COMP 110: Spring 200913 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 the text on pgs 33-34 canvas.drawArc(150, 160, 100, 50, 180, 180);

14 COMP 110: Spring 200914 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

15 COMP 110: Spring 200915 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);

16 COMP 110: Spring 200916 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);

17 COMP 110: Spring 200917 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); }

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

19 COMP 110: Spring 200919 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

20 COMP 110: Spring 200920 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);

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

22 COMP 110: Spring 200922 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); }

23 COMP 110: Spring 200923 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:")

24 COMP 110: Spring 200924 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 + "!");

25 COMP 110: Spring 200925 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 + "!");

26 COMP 110: Spring 200926 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.

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

28 COMP 110: Spring 200928 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"); } } }

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

30 COMP 110: Spring 200930 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");

31 COMP 110: Spring 200931 Programming Demo Improving our House Fill the house with a color Draw a door Draw a window

32 COMP 110: Spring 200932 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)

33 COMP 110: Spring 200933 Programming Demo Programming

34 COMP 110: Spring 200934 Wednesday Begin reviewing for final


Download ppt "COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014."

Similar presentations


Ads by Google