Week 3 Object-oriented programming JOptionPane Random Graphics 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 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 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 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) 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 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 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 n keyboard 1112 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.*; URLImagePopUp • The program will display a popup of a requested CS 1112 class member URLImagePopUp • The program begins with several import statements worth noting import javax.swing.*; import java.net.*; import java.io.*; import java.util.*; 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.*; 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.*; 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.*; URLImagePopUp import statements • The program begins with several import statements worth noting import javax.swing.*; import java.net.*; import java.io.*; import java.util.*; // gets JOptionPane // gets URL // gets IOException // gets Scanner 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"; 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 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 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 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 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 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 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 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 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 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 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(); 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 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 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 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 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" 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 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 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 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 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 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 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 ); 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 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 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 } 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. 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 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 Random numbers • Java class Random provides the means to create a random number generator • The library is available when importing java.util.* 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 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]) 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 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 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 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 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 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 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 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] 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 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 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 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 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 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 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 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 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 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 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 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 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 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] 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 00100 01100 01010 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 91517 37076 54031 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 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 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 ); 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 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 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 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 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 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 ); 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 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 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 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 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 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 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 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(); 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 ); 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 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 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 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 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 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 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 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 ); 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 ); 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 ); 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 ); 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 ); 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 ); 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 ); 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 ); 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 ); 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 ); 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); 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); 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 xcoordinates 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 ); 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 xcoordinates 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 ); 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 xcoordinates 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 ); 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 xcoordinates 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 );