Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 3 Object-oriented programming JOptionPane Random Graphics.

Similar presentations


Presentation on theme: "Week 3 Object-oriented programming JOptionPane Random Graphics."— Presentation transcript:

1 Week 3 Object-oriented programming JOptionPane Random Graphics

2 Why Java One of things that make Java a handy language for digital problem solving is its rich set of built-in libraries To name just a few of the thousands that are available – Libraries for input manipulation – Libraries for representing information as lists and dictionaries – Libraries to provide access and manipulation of files and folders on your computer and on the Internet – Libraries for creating windows, colors, images, labels,... – Libraries for generating random numbers

3 Objects Java treats objects different than numbers and boolean values Numbers and boolean values just exist – you can enter them as literals and get other values using arithmetic and logical operators Objects have to be created The creation of an object has two parts – Get the memory to store the object – Configure the memory to represent the desired object – Programmers typically use the term construct instead of configure

4 For example We have been using Scanner for a while now. A typical Scanner definition is Scanner keyboard = new Scanner( System.in ); The new in the statement is a Java keyword and operator – A new operation causes Java to get memory for storing a new object – Java can tell how much memory is needed by the identifier following keyword new. The identifier is the type of object to be created

5 For example We have been using Scanner’s for a while now. A typical Scanner definition is Scanner keyboard = new Scanner( System.in ); For our example Java gets enough memory to represent a Scanner The values in the parentheses following the object type are generally called arguments or parameters The arguments tell Java how to construct the new object. In our example the argument System.in specifies the input source for the new Scanner (the keyboard of the computer running the program)

6 For example We have been using Scanner’s for a while now. A typical Scanner definition is Scanner keyboard = new Scanner( System.in ); The value of the expression new Scanner( System.in ) is the location (address in memory) where a newly constructed Scanner object is stored. The location value produced by the expression is then used to initialize variable keyboard

7 For example We have been using Scanner’s for a while now. A typical Scanner definition is Scanner keyboard = new Scanner( System.in ); The value of the expression new Scanner( System.in ) is the location (address in memory) where a newly constructed Scanner object is stored. The location value produced by the expression is then used to initialize variable keyboard Understanding the above is subtle and crucial to realize While the value of a numeric variable is a number, the value of an object variable is not an object but a pointer where to find an object

8 For example Let’s expand our example to also define and int variable n and a boolean variable b int n = 1112; Scanner keyboard = new Scanner( System.in ); The correct picture of memory boxes for the two variables is below. It shows that the value of variable keyboard is not a Scanner but a pointer where to find a Scanner n1112 keyboard

9 JOptionPane Class JOptionPane supports the creation of popup windows The class provides many methods that will create and do popups for you The popup we will often find useful is one that includes a picture in its popup JOptionPane was not part of the original version of Java JOptionPane is an add-on that can be gotten by including the following import statement at the start of your program import javax.swing.*;

10 URLImagePopUp The program will display a popup of a requested CS 1112 class member

11 URLImagePopUp The program begins with several import statements worth noting import javax.swing.*; import java.net.*; import java.io.*; import java.util.*;

12 URLImagePopUp import statements The program begins with several import statements worth noting import javax.swing.*; // gets JOptionPane import java.net.*; import java.io.*; import java.util.*;

13 URLImagePopUp import statements The program begins with several import statements worth noting import javax.swing.*; // gets JOptionPane import java.net.*; // gets URL import java.io.*; import java.util.*;

14 URLImagePopUp import statements The program begins with several import statements worth noting import javax.swing.*; // gets JOptionPane import java.net.*; // gets URL import java.io.*; // gets IOException import java.util.*;

15 URLImagePopUp import statements The program begins with several import statements worth noting import javax.swing.*; // gets JOptionPane import java.net.*; // gets URL import java.io.*; // gets IOException import java.util.*; // gets Scanner

16 URLImagePopUp constants The program defines two constants // base web folder public static final String CS1112_PEOPLE_URL = "http://www.cs.virginia.edu/cs1112/people/"; // photo filename public static final String JPG_NAME = "selfie.jpg";

17 URLImagePopUp constants The program defines two constants // base web folder public static final String CS1112_PEOPLE_URL = "http://www.cs.virginia.edu/cs1112/people/"; // photo filename public static final String JPG_NAME = "selfie.jpg"; CS1112_PEOPLE_URL names the web folder for current CS 1112 class members. The folder has a subfolder for each class member

18 URLImagePopUp constants The program defines two constants // base web folder public static final String CS1112_PEOPLE_URL = "http://www.cs.virginia.edu/cs1112/people/"; // photo filename public static final String JPG_NAME = "selfie.jpg"; CS1112_PEOPLE_URL names the web folder for current CS 1112 class members. The folder has a subfolder for each class member JPG_NAME names the web file given to the uploaded class member pictures

19 URLImagePopUp constants The program defines two constants // base web folder public static final String CS1112_PEOPLE_URL = "http://www.cs.virginia.edu/cs1112/people/"; // photo filename public static final String JPG_NAME = "selfie.jpg"; The constants when coupled with one of our ID’s specifies a web location; the below string is web location of my uploaded picture CS1112_PEOPLE_URL + "jpc" + "/" + JPG_NAME

20 URLImagePopUp constants The program defines two constants // base web folder public static final String CS1112_PEOPLE_URL = "http://www.cs.virginia.edu/cs1112/people/"; // photo filename public static final String JPG_NAME = "selfie.jpg"; The constants when coupled with one of our ID’s specifies a web location; the below string is web location of my uploaded picture CS1112_PEOPLE_URL + "jpc" + "/" + JPG_NAME http://www.cs.virginia.edu/cs1112/people/jpc/selfie.jpg

21 URLImagePopUp constants The program defines two constants // base web folder public static final String CS1112_PEOPLE_URL = "http://www.cs.virginia.edu/cs1112/people/"; // photo filename public static final String JPG_NAME = "selfie.jpg"; The constants when coupled with one of our ID’s specifies a web location; the below string is web location of my uploaded picture CS1112_PEOPLE_URL + "jpc" + "/" + JPG_NAME http://www.cs.virginia.edu/cs1112/people/jpc/selfie.jpg

22 URLImagePopUp throws IOException The method main() definition starts differently than in previous programs public static void main( String[] args ) throws IOException { The phrase throws IOException has been added

23 URLImagePopUp throws IOException The method main() definition starts differently than in previous programs public static void main( String[] args ) throws IOException { The phrase throws IOException has been added The throws IOException is necessary because of what the code is going to attempt – get access to something out on the web – a class selfie

24 URLImagePopUp throws IOException The method main() definition starts differently than in previous programs public static void main( String[] args ) throws IOException { The phrase throws IOException has been added The throws IOException is necessary because of what the code is going to attempt – get access to something out on the web – a class selfie If the user mistypes the ID then the web location the program attempts to access does not exist

25 URLImagePopUp throws IOException The method main() definition starts differently than in previous programs public static void main( String[] args ) throws IOException { The phrase throws IOException has been added The throws IOException is necessary because of what the code is going to attempt – get access to something out on the web – a class selfie If the user mistypes the ID then the web location the program attempts to access does not exist An errant access attempt generates an input error condition or in Java parlance throws an IOException

26 URLImagePopUp throws IOException The method main() definition starts differently than in previous programs public static void main( String[] args ) throws IOException { The phrase throws IOException has been added The throws IOException is necessary because of what the code is going to attempt – get access to something out on the web – a class selfie If the user mistypes the ID then the web location the program attempts to access does not exist An errant access attempt generates an input error condition or in Java parlance throws an IOException Programs that can have input errors need to tell Java of the possibility

27 URLImagePopUp input processing The beginning of the body of method main() is something you are now use to – get some input from the program user Scanner stdin = new Scanner( System.in ); System.out.print( "Email id for person of interest: " ); String id = stdin.next();

28 URLImagePopUp input processing The beginning of the body of method main() is something you are now use to – get some input from the program user Scanner stdin = new Scanner( System.in ); System.out.print( "Email id for person of interest: " ); String id = stdin.next(); A Scanner stdin that gets its input from the keyboard is gotten

29 URLImagePopUp input processing The beginning of the body of method main() is something you are now use to – get some input from the program user Scanner stdin = new Scanner( System.in ); System.out.print( "Email id for person of interest: " ); String id = stdin.next(); A Scanner stdin that gets its input from the keyboard is gotten A prompt is printed that tells the user what to enter

30 URLImagePopUp input processing The beginning of the body of method main() is something you are now use to – get some input from the program user Scanner stdin = new Scanner( System.in ); System.out.print( "Email id for person of interest: " ); String id = stdin.next(); A Scanner stdin that gets its input from the keyboard is gotten A prompt is printed that tells the user what to enter The input is read and assigned to String variable id

31 URLImagePopUp input processing The beginning of the body of method main() is something you are now use to – get some input from the program user Scanner stdin = new Scanner( System.in ); System.out.print( "Email id for person of interest: " ); String id = stdin.next(); A Scanner stdin that gets its input from the keyboard is gotten A prompt is printed that tells the user what to enter The input is read and assigned to String variable id – In case you do not remember the Scanner method next() processes (reads and returns) the next nonblank entry the user enters

32 URLImagePopUp input processing The beginning of the body of method main() is something you are now use to – get some input from the program user Scanner stdin = new Scanner( System.in ); System.out.print( "Email id for person of interest: " ); String id = stdin.next(); If the user enters mst3k then the memory boxes look like below (Remember both stdin and id are object variables) keyboard id "mst3k"

33 URLImagePopUp popup description The program then defines three variables String msg = "Aren't I cute?"; String banner = "Hello from " + id; String location = CS1112_PEOPLE_URL + id + "/" + JPG_NAME; The first two msg and banner will be directly used in making the popup – they are respectively the popup message and title bar text

34 URLImagePopUp popup description The program then defines three variables String msg = "Aren't I cute?"; String banner = "Hello from " + id; String location = CS1112_PEOPLE_URL + id + "/" + JPG_NAME; The first two msg and banner will be directly used in making the popup – they are respectively the popup message and title bar text The last variable location specifies the Internet location for the selfie of interest

35 URLImagePopUp popup description The program uses the location variable in creating a URL object URL photoURL = new URL( page ); URL is Java’s representation for something on the web. URL is an abbreviation for Uniform Resource Locator Everything on the web has to have a unique web location

36 URLImagePopUp popup description The program uses the location variable in creating a URL object. URL photoURL = new URL( page ); URL is Java’s representation for something on the web. URL is an abbreviation for Uniform Resource Locator Everything on the web has to have a unique web location Just as a Scanner construction need to indicate an input source, the constructing of a URL needs a web source location

37 URLImagePopUp popup description The program uses the location variable in creating a URL object. URL photoURL = new URL( page ); URL is Java’s representation for something on the web. URL is an abbreviation for Uniform Resource Locator Everything on the web has to have a unique web location Just as a Scanner construction need to indicate an input source, the constructing of a URL needs a web source location If it turns out that page does not represent a valid web source, when the program attempts to access the illegal location, Java terminates the program and indicates an IOException has occurred

38 URLImagePopUp popup description The program uses the URL as the input source in constructing an ImageIcon ImageIcon image = new ImageIcon( photoURL ); ImageIcon is Java’s basic representation for an image In constructing a new ImageIcon we need to specify where to get the pixels that make up the image A web location is a valid input source for an ImageIcon

39 URLImagePopUp popup description At this point we have all of the elements necessary for making a popup – the message to be displayed, the text for the title bar, and the image for the popup String msg = "Aren't I cute?"; String banner = "Hello from " + id;... ImageIcon image = new ImageIcon( photoURL );

40 URLImagePopUp popup description JOptionPane can create many kinds of popups – information messages, warnings, selections,... Our interest is an information message popup JOptionPane provides a bunch of constants for indicating the possible popup types – to indicate an interest in an information popup use JOptionPane.INFORMATION_MESSAGE

41 URLImagePopUp popup generation The JOptionPane method showMessageDialog() will display a popup The appearance of the popup is specified through the arguments passed to the method The method expects values for the following and in the indicated order 1.Where should the popup be displayed in the window 2.What is the message of the popup 3.What is the title text of the popup 4.What kind of popup should it be 5.What image should be displayed as part of the popup

42 URLImagePopUp popup generation Our popup command for URLImagePopup is the following JOptionPane.showMessageDialog ( null // what window to center on msg, // what is the popup message banner, // what is the popup title JOptionPane.INFORMATION_MESSAGE, // what type of message image ); // what is the popup image }

43 URLImagePopUp popup generation Our popup command for URLImagePopup is the following JOptionPane.showMessageDialog ( null // what window to center on msg, // what is the popup message banner, // what is the popup title JOptionPane.INFORMATION_MESSAGE, // what type of message image ); // what is the popup image } null is a Java keyword and that indicates the lack of an object.

44 URLImagePopUp popup generation Our popup command for URLImagePopup is the following JOptionPane.showMessageDialog ( null // what window to center on msg, // what is the popup message banner, // what is the popup title JOptionPane.INFORMATION_MESSAGE, // what type of message image ); // what is the popup image } null is a Java keyword and that indicates the lack of an object. When null is the first parameter to a showMessageDialog() it means rather than centering on a particular window, the popup is centered on the middle of the screen

45

46 Random numbers Random numbers are used in lots of software Two examples that always come to my mind are electronic games and stock trading software – In games they are used so players do not experience the same situation each time they play – In stock trading analysis they are used to model changing financial markets

47 Random numbers Java class Random provides the means to create a random number generator The library is available when importing java.util.*

48 Random numbers Java class Random provides the means to create a random number generator The library is available when importing java.util.* By default the construction of Random generator produces a different sequence of numbers every time

49 Random numbers Java class Random provides the means to create a random number generator The library is available when importing java.util.* By default the construction of Random generator produces a different sequence of numbers every time Some of the capabilities of a Random generator are producing random integers, logical values, and decimal values from the interval (0, 1])

50 Random numbers Java class Random provides the means to create a random number generator The library is available when importing java.util.* By default the construction of Random generator produces a different sequence of numbers every time it is used. Some of the capabilities of a Random generator are producing random integers, logical values, and decimal values from the interval (0, 1]) – CS 1112 tends to only use examples involving random integers

51 Random numbers Program RandomNumbers creates and uses a default Random generator to produce five random int values – Multiple runs of the program display different integer sequences

52 RandomNumbers The important statements in program RandomNumbers are Random dice = new Random( ); int a = dice.nextInt(); int b = dice.nextInt(); int c = dice.nextInt(); int d = dice.nextInt(); int e = dice.nextInt(); The initialization expression for the Random variable dice is new Random( ) The expression gets memory for the generator and performs default construction. Default occurs when there are no arguments in the ( ) in a new expression

53 RandomNumbers The important statements in program RandomNumbers are Random dice = new Random( ); int a = dice.nextInt(); int b = dice.nextInt(); int c = dice.nextInt(); int d = dice.nextInt(); int e = dice.nextInt(); A default-constructed Random generator will produce a different random number sequence then other default-constructed Random generators The ability to be different provides the variability we want

54 RandomNumbers The important statements in program RandomNumbers are Random dice = new Random( ); int a = dice.nextInt(); int b = dice.nextInt(); int c = dice.nextInt(); int d = dice.nextInt(); int e = dice.nextInt(); I typically call my Random generator variable dice because the throwing of dice is a random sequence

55 RandomNumbers The important statements in program RandomNumbers are Random dice = new Random( ); int a = dice.nextInt(); int b = dice.nextInt(); int c = dice.nextInt(); int d = dice.nextInt(); int e = dice.nextInt(); A Random generator has a method nextInt() that takes no parameters

56 RandomNumbers The important statements in program RandomNumbers are Random dice = new Random( ); int a = dice.nextInt(); int b = dice.nextInt(); int c = dice.nextInt(); int d = dice.nextInt(); int e = dice.nextInt(); A Random generator has a method nextInt() that takes no parameters When a Random generator such as dice gets the message nextInt() it produces a random int value; i.e., a value as small as -2147483648 and as large as 2147483647

57 RandomNumbers The important statements in program RandomNumbers are Random dice = new Random( ); int a = dice.nextInt(); int b = dice.nextInt(); int c = dice.nextInt(); int d = dice.nextInt(); int e = dice.nextInt(); A Random generator has a method nextInt() that takes no parameters When a Random generator such as dice gets the message nextInt() it produces a random int value; i.e., a value as small as -2147483648 and as large as 2147483647; i.e., in the interval (- 2147483648, 2147483647]

58 RandomNumbers The important statements in program RandomNumbers are Random dice = new Random( ); int a = dice.nextInt(); int b = dice.nextInt(); int c = dice.nextInt(); int d = dice.nextInt(); int e = dice.nextInt(); Below are the numbers outputted from one program run 832098310 -1708435978 -2132655842 -2101213972 808269398

59 RandomNumbers The important statements in program RandomNumbers are Random dice = new Random( ); int a = dice.nextInt(); int b = dice.nextInt(); int c = dice.nextInt(); int d = dice.nextInt(); int e = dice.nextInt(); Below are the (different) numbers outputted from another program run -1139735074 4555273 -2079226278 681790015 -651527058

60 RandomNumbers The important statements in program RandomNumbers are Random dice = new Random( ); int a = dice.nextInt(); int b = dice.nextInt(); int c = dice.nextInt(); int d = dice.nextInt(); int e = dice.nextInt(); Below are the (again different) numbers outputted from yet another program run 605338243 515810532 -238143103 2087856151 -1102462255

61 Random numbers Program RandomNumbersBaseN also creates and uses a default Random generator to produce five random int values. Its random values are all numbers in the interval 0 … n-1 (the value of n is gotten from the program user) Multiple runs of the program display different integer sequences

62 Random numbers Program RandomNumbersBaseN also creates and uses a default Random generator to produce five random int values. Its random values are all numbers in the interval 0 … n-1 (the value of n is gotten from the program user) Multiple runs of the program display different integer sequences Its ability to tailor the values will prove to be very important

63 Random numbers Program RandomNumbersBaseN also creates and uses a default Random generator to produce five random int values. Its random values are all numbers in the interval 0 … n-1 (the value of n is gotten from the program user) Multiple runs of the program display different integer sequences Its ability to tailor the values will prove to be very important By the way the interval 0 … n-1 is written mathematically as (0, n]. The leading ( indicates includes, the trailing ] indicates excludes

64 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); The program gets a value n from the keyboard. That value is used an argument to a nextInt() method expecting an int argument

65 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); The program gets a value n from the keyboard. That value is repeatedly used an argument to a nextInt() expecting an int argument

66 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); This nextInt() expecting an int argument uses the value of its argument to limit the interval from the random numbers are generated

67 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); The argument value specifies a base. The generated random numbers are limited to integers from that numeric base

68 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); For argument n, the numbers are limited to base n – If n is 2 then binary numbers are generated; i.e., 0 or 1

69 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); For argument n, the numbers are limited to base n – If n is 10 then binary numbers are generated; i.e., 0, 1, 2... 9

70 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); For argument n, the numbers are limited to base n – If n is 256 then binary numbers are generated; i.e., 0, 1, 2... 255

71 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); For argument n, the numbers are limited to base n – In general the numbers come from the interval 0... n-1; i.e., (0, n]

72 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); Program runs where the user supplied 2 for n produced random numbers 0 0 1 0 0 0 1 1 0 0 0 1 0 1 0

73 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); Program runs where the user supplied 10 for n produced random numbers 9 1 5 1 7 3 7 0 7 6 5 4 0 3 1

74 RandomNumbersBaseN The important statements in program RandomNumbersBaseN are int n = keyboard.nextInt(); Random dice = new Random( ); int a = dice.nextInt( n ); int b = dice.nextInt( n ); int c = dice.nextInt( n ); int d = dice.nextInt( n ); int e = dice.nextInt( n ); Program runs where the user supplied 10 for n produced random numbers 64 111 51 97 74 186 229 32 198 190 97 221 67 212 220

75 Graphics rendering Besides being able to produce popup windows, Java provides support for producing regular windows with all sorts of window elements JFrame is the Java representation for a general purpose window When we need windows I will provide the code to set up the window, you will be in charge of the fun part – producing the content of the window

76 Window setup Our basic Java instructions for setting up a window are JFrame window = new JFrame( "Rendering" ); window.setSize( WIDTH, HEIGHT ); window.setAlwaysOnTop( true ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

77 Window setup Our basic Java instructions for setting up a window are JFrame window = new JFrame( "Rendering" ); window.setSize( WIDTH, HEIGHT ); window.setAlwaysOnTop( true ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); The first instruction constructs a new JFrame whose title bar has the text Rendering

78 Window setup Our basic Java instructions for setting up a window are JFrame window = new JFrame( "Rendering" ); window.setSize( WIDTH, HEIGHT ); window.setAlwaysOnTop( true ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); The second instruction sets the size of the window. Here WIDTH and HEIGHT are constants defined at the beginning of the program

79 Window setup Our basic Java instructions for setting up a window are JFrame window = new JFrame( "Rendering" ); window.setSize( WIDTH, HEIGHT ); window.setAlwaysOnTop( true ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); JFrame provides methods for controlling the behavior of a window. The third instruction causes your computer to always keep this window on top of your other windows I include this instruction because if the window does not popup to the top you will probably suspect your code is broken as you do not see the window

80 Window setup Our basic Java instructions for setting up a window are JFrame window = new JFrame( "Rendering" ); window.setSize( WIDTH, HEIGHT ); window.setAlwaysOnTop( true ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); The fourth instruction tells Java to end your program when the window disappears – something I think is what you would expect

81 Content pane Once we have a window we are going to want to be able to interact with its content pane The content pane is the part below the title bar People in CS 1112 are using many different types of computers and operating systems with many personalizations. The instructions coming up allow everybody to generate and consistently see what they put on to the content pane in a similar manner to others in the class I view the instructions as setting up a canvas to be the content pane of your window I will always supply these statements to any graphical program you are to build

82 Content pane setup Our instructions for setting up the content pane are BufferedImage surface = new BufferedImage( WIDTH, HEIGHT, RGB ); ImageIcon canvas = new ImageIcon( surface ); JLabel pane = new JLabel( canvas ); window.setContentPane( pane ); window.pack(); window.setVisible( true );

83 Content pane setup Our instructions for setting up the content pane are BufferedImage surface = new BufferedImage( WIDTH, HEIGHT, RGB ); ImageIcon canvas = new ImageIcon( surface ); JLabel pane = new JLabel( canvas ); window.setContentPane( pane ); window.pack(); window.setVisible( true ); The BufferedImage supplies a drawing surface that for our image

84 Content pane setup Our instructions for setting up the content pane are BufferedImage surface = new BufferedImage( WIDTH, HEIGHT, RGB ); ImageIcon canvas = new ImageIcon( surface ); JLabel pane = new JLabel( canvas ); window.setContentPane( pane ); window.pack(); window.setVisible( true ); The BufferedImage supplies a drawing surface that for our image The ImageIcon takes that surface and makes into a canvas

85 Content pane setup Our instructions for setting up the content pane are BufferedImage surface = new BufferedImage( WIDTH, HEIGHT, RGB ); ImageIcon canvas = new ImageIcon( surface ); JLabel pane = new JLabel( canvas ); window.setContentPane( pane ); window.pack(); window.setVisible( true ); The BufferedImage supplies a drawing surface that for our image The ImageIcon takes that surface and makes into a canvas The JLabel mounts the canvas in a way that it is now suitable as a content pane

86 Content pane setup Our instructions for setting up the content pane are BufferedImage surface = new BufferedImage( WIDTH, HEIGHT, RGB ); ImageIcon canvas = new ImageIcon( surface ); JLabel pane = new JLabel( canvas ); window.setContentPane( pane ); window.pack(); window.setVisible( true ); Method setContentPane() does what you would expect it to do – it tells its window to set its content pane to be the graphical object argument being passed to the method

87 Content pane setup Our instructions for setting up the canvas pane are BufferedImage surface = new BufferedImage( WIDTH, HEIGHT, RGB ); ImageIcon canvas = new ImageIcon( surface ); JLabel pane = new JLabel( canvas ); window.setContentPane( pane ); window.pack(); window.setVisible( true ); Method pack() tells the window to set its size to match the dimensions of the content pane

88 Content pane setup Our instructions for setting up the canvas pane are BufferedImage surface = new BufferedImage( WIDTH, HEIGHT, RGB ); ImageIcon canvas = new ImageIcon( surface ); JLabel pane = new JLabel( canvas ); window.setContentPane( pane ); window.pack(); window.setVisible( true ); The setVisible() instruction causes the window to be made visible – by default windows are invisible. They start off that way so the window can be assembled before it is viewed

89 Content pane setup Our instructions for setting up the canvas pane are BufferedImage surface = new BufferedImage( WIDTH, HEIGHT, RGB ); ImageIcon canvas = new ImageIcon( surface ); JLabel pane = new JLabel( canvas ); window.setContentPane( pane ); window.pack(); window.setVisible( true ); The setVisible() instruction causes the window to be made visible – by default windows are invisible. They start off that way so the window can be assembled before it is viewed

90 Graphics rendering Now that the content pane is set up, we are ready to paint (render) an image onto it Everything paintable in Java has a method getGraphics() The getGraphics() method returns an object type Graphics. A Graphics object has a rich set of instructions for making images Our call names the Graphics object artist Graphics artist = surface.getGraphics();

91 Painting the background Our program next sets the entire canvas to our chosen background color artist.setColor( BACKGROUND_COLOR ); artist.fillRect( 0, 0, WIDTH, HEIGHT );

92 Painting the background Our program next sets the entire canvas to our chosen background color artist.setColor( BACKGROUND_COLOR ); artist.fillRect( 0, 0, WIDTH, HEIGHT ); Graphics method setColor() takes a Color as its argument Until another setColor() occurs all rendering commands will use that color in accomplishing their tasks

93 Painting the background Our program next sets the entire canvas to our chosen background color artist.setColor( BACKGROUND_COLOR ); artist.fillRect( 0, 0, WIDTH, HEIGHT ); Graphics method fillRect() paints a filled in rectangle onto the image. The method expects four arguments

94 Painting the background Our program next sets the entire canvas to our chosen background color artist.setColor( BACKGROUND_COLOR ); artist.fillRect( 0, 0, WIDTH, HEIGHT ); Graphics method fillRect() paints a filled in rectangle onto the image. The method expects four arguments. In order they are 1.The x-location where to start painting 2.The y-location where to start painting 3.The width of the rectangle to be painted 4.The height of the rectangle to be painted

95 Painting the background Our program next sets the entire canvas to our chosen background color artist.setColor( BACKGROUND_COLOR ); artist.fillRect( 0, 0, WIDTH, HEIGHT ); Graphics method fillRect() paints a filled in rectangle onto the image. The method expects four arguments. In order they are 1.The x-location where to start painting 2.The y-location where to start painting 3.The width of the rectangle to be painted 4.The height of the rectangle to be painted

96 Painting the background Our program next sets the entire canvas to our chosen background color artist.setColor( BACKGROUND_COLOR ); artist.fillRect( 0, 0, WIDTH, HEIGHT ); In graphics the origin of an image is its upper left hand color. So the x-location and y-location arguments tell the fillRect() method how far over and how far down to start the rendering

97 Painting the background Our program next sets the entire canvas to our chosen background color artist.setColor( BACKGROUND_COLOR ); artist.fillRect( 0, 0, WIDTH, HEIGHT ); The width and height arguments tell the fillRect() method how far to the right and how far down to paint is filled in rectangle

98 Painting the background Our program next sets the entire canvas to our chosen background color artist.setColor( BACKGROUND_COLOR ); artist.fillRect( 0, 0, WIDTH, HEIGHT ); So all together the arguments cause a filled-in rectangle to be painted that covers the entire surface

99 drawRect() Using the currently set color, method drawRect() draws on its image the outline of a box specified by its four arguments. The first two arguments give the location for the drawing. The second two arguments give the width and height of the rectangle to be drawn Using green, the below example draws at (40, 80) the outline of a 50 x 75 rectangle int x1 = 40; int y1 = 80; int w1 = 50 int h1 = 75; artist.setColor( Color.GREEN ); artist.drawRect( x1, y1, w1, h1 );

100 drawRect() Using the currently set color, method drawRect() draws on its image the outline of a box specified by its four arguments. The first two arguments give the location for the drawing. The second two arguments give the width and height of the rectangle to be drawn Using green, the below example draws at (40, 80) the outline of a 50 x 75 rectangle int x1 = 40; int y1 = 80; int w1 = 50 int h1 = 75; artist.setColor( Color.GREEN ); artist.drawRect( x1, y1, w1, h1 );

101 fillRect() The program again uses method fillRect() to paint at (100, 20) a filled-in 100 x 25 orange rectangle int x2 = 100; int y2 = 20; int w2 = 100; int h2 = 25; artist.setColor( Color.ORANGE ); artist.fillRect( x2, y2, w2, h2 );

102 fillRect() The program again uses method fillRect() to paint at (100, 20) a filled-in 100 x 25 orange rectangle int x2 = 100; int y2 = 20; int w2 = 100; int h2 = 25; artist.setColor( Color.ORANGE ); artist.fillRect( x2, y2, w2, h2 );

103 drawOval() Using the currently set color, method drawOval() draws on its image the outline of an oval fitting the box specified by its four arguments. The first two arguments give the location for the drawing. The second two arguments specify the width and height of the rectangle in which to draw the oval Using magenta, the below example draws at (120, 65) the outline of the oval fitting a 150 x 90 rectangle int x3 = 120; int y3 = 65; int w3 = 150; int h3 = 90; artist.setColor( Color.MAGENTA ); artist.drawOval( x3, y3, w3, h3 );

104 drawOval() Using the currently set color, method drawOval() draws on its image the outline of an oval fitting the box specified by its four arguments. The first two arguments give the location for the drawing. The second two arguments specify the width and height of the rectangle in which to draw the oval Using magenta, the below example draws at (120, 65) the outline of the oval fitting a 150 x 90 rectangle int x3 = 120; int y3 = 65; int w3 = 150; int h3 = 90; artist.setColor( Color.MAGENTA ); artist.drawOval( x3, y3, w3, h3 );

105 fillOval() Using the currently set color, method fillOval() paints on its image the filled-oval fitting the box specified by its four arguments. The first two arguments give the location for the painting. The second two arguments specify the width and height of the rectangle in which to fit the oval Using blue, the below example paints at (300, 25) the filled-in oval fitting a 50 x 75 rectangle int x4 = 300; int y4 = 25; int w4 = 50; int h4 = 75; artist.setColor( Color.BLUE ); artist.fillOval( x4, y4, w4, h4 );

106 fillOval() Using the currently set color, method fillOval() paints on its image the filled-oval fitting the box specified by its four arguments. The first two arguments give the location for the painting. The second two arguments specify the width and height of the rectangle in which to fit the oval Using blue, the below example paints at (300, 25) the filled-in oval fitting a 50 x 75 rectangle int x4 = 300; int y4 = 25; int w4 = 50; int h4 = 75; artist.setColor( Color.BLUE ); artist.fillOval( x4, y4, w4, h4 );

107 drawString() Using the currently set color and font, method drawString() draws a string on its image using its three arguments. The first argument gives the string to be rendered. The second two arguments give the location for the drawing Using cyan, the below example draws at (300, 155) the string "We are the best!" int x5 = 300; int y5 = 155; String s5 = "We are the best!"; artist.setColor( Color.CYAN ); artist.drawString( s5, x5, y5 );

108 drawString() Using the currently set color and font, method drawString() draws a string on its image using its three arguments. The first argument gives the string to be rendered. The second two arguments give the location for the drawing Using cyan, the below example draws at (300, 155) the string "We are the best!" int x5 = 300; int y5 = 155; String s5 = "We are the best!"; artist.setColor( Color.CYAN ); artist.drawString( s5, x5, y5 );

109 drawLine() Using the currently set color, method drawLine() draws on its image the line specified by its four arguments. The first two arguments give the starting location of the line. The second two arguments give ending location of the line Using white, the below example draws a line from (300, 165) to (400, 165) int x6 = 300; int y6 = 165; int x7= 400; int y7 = 165; artist.setColor( Color.WHITE ); artist.drawLine( x6, y6, x7, y7);

110 drawLine() Using the currently set color, method drawLine() draws on its image the line specified by its four arguments. The first two arguments give the starting location of the line. The second two arguments give ending location of the line Using white, the below example draws a line from (300, 165) to (400, 165) int x6 = 300; int y6 = 165; int x7= 400; int y7 = 165; artist.setColor( Color.WHITE ); artist.drawLine( x6, y6, x7, y7);

111 drawPolygon() Using the currently set color, method drawPolygon() draws on its image the outline of the closed polygon specified by its three arguments. The first two arguments give respectively the x- coordinates and y-coordinates from which to get the polygon vertex locations. The last argument specifies the number of vertices Using yellow, the below example draws 6-sided polyon int[] x8 = { 350, 400, 425, 425, 400, 375 }; int[] y8 = { 20, 60, 100, 160, 125, 90 }; int n8 = 6; artist.setColor( Color.YELLOW); artist.drawPolygon( x8, y8, n8 );

112 drawPolygon() Using the currently set color, method drawPolygon() draws on its image the outline of the closed polygon specified by its three arguments. The first two arguments give respectively the x- coordinates and y-coordinates from which to get the polygon vertex locations. The last argument specifies the number of vertices Using yellow, the below example draws 6-sided polyon int[] x8 = { 350, 400, 425, 425, 400, 375 }; int[] y8 = { 20, 60, 100, 160, 125, 90 }; int n8 = 6; artist.setColor( Color.YELLOW); artist.drawPolygon( x8, y8, n8 );

113 fillPolygon() Using the currently set color, method fillPolygon() paints on its image the closed filled-in polygon specified by its three arguments. The first two arguments give respectively the x- coordinates and y-coordinates from which to get the polygon vertex locations. The last argument specifies the number of vertices Using pink, the below example paints a filled-in 4-sided polygon int[] x9 = { 175, 240, 190, 150 }; int[] y9 = { 80, 110, 130, 120 }; int n9 = 4; artist.setColor( Color.PINK ); artist.drawPolygon( x8, y8, n8 );

114 fillPolygon() Using the currently set color, method fillPolygon() paints on its image the closed filled-in polygon specified by its three arguments. The first two arguments give respectively the x- coordinates and y-coordinates from which to get the polygon vertex locations. The last argument specifies the number of vertices Using pink, the below example paints a filled-in 4-sided polygon int[] x9 = { 175, 240, 190, 150 }; int[] y9 = { 80, 110, 130, 120 }; int n9 = 4; artist.setColor( Color.PINK ); artist.drawPolygon( x8, y8, n8 );


Download ppt "Week 3 Object-oriented programming JOptionPane Random Graphics."

Similar presentations


Ads by Google