370 likes | 502 Views
Exposure Advacned Graphics in Java, 2011 Edition. Chapter 1 Slides. Graphics Review. PowerPoint Presentation created by: Mr. John L. M. Schram from materials created by Mr. Leon Schram. Assumed Knowledge.
E N D
Exposure Advacned Graphics in Java, 2011 Edition Chapter 1 Slides Graphics Review PowerPoint Presentation created by: Mr. John L. M. Schram from materials created by Mr. Leon Schram
Assumed Knowledge This chapter contains information on Basic Graphics Concepts and Java awt Graphics class commands. The information in this chapter is intended for review purposes only. We will not spend much, if any, time going over this information as it is considered assumed knowledge. This course is called Advanced Graphics after all.
LearningGraphics Programming Learning graphics programming is not simply a fun issue. You will learn many sophisticated computer science concepts by studying graphics programs. Some of the most sophisticated programs are video games. Only very dedicated and knowledgeable programmers can write effective video games.
Graphics & Coordinate Geometry A graphics window uses a system of (X,Y) coordinates in a manner similar to the use of coordinates that you first learned in your math classes. The next slide shows an example of the Cartesian Coordinate System. In particular, note that the Cartesian system has four quadrants with the (0,0) coordinate (called the "origin") located in the center of the grid where the X-Axisand the Y-Axis intersect.
Computer Graphics Window NOTE: The “origin” is not in the center.
Computer Graphics Window We will refer to this type of Graphics Window as an “Applet Window”.
Applications vs. Applets With applications, which are all the files you have used so far, you compile and execute the same file. With applets, you compile the .java file, and you execute the .html file. Remember that appletsare designed to execute inside a webpage. This is why an .html file is required.
x1, y1 x2, y2 The drawLine Method drawLine(int x1, int y1, int x2, int y2) Draws a line from coordinate (x1,y1) to coordinate (x2,y2)
// Review01.java 07-23-10 by Leon Schram // This program demonstrates how to draw lines. // Lines are drawn from (X1,Y1) to (X2,Y2) with drawLine(X1,Y1,X2,Y2). import java.awt.*; import java.applet.*; public class Review01 extends Applet { public void paint(Graphics g) { g.drawLine(0,0,800,600); g.drawLine(0,600,800,0); g.drawLine(100,300,700,300); g.drawLine(400,100,400,500); } } Compile this file! <!-- 07-23-10 by Leon Schram --> <APPLET CODE = "Review01.class" WIDTH=800 HEIGHT=600> </APPLET> Execute this file!
x, y height The drawRect Method drawRect(int x, int y, int width, int height) Draws a rectangle with top-left corner at coordinate (x,y) using width and height dimensions. fillRect uses identical parameters, but fills in the rectangle. width
// Review02.java 07-23-10 by Leon Schram // This program introduces the rectangle command. // A rectangle is drawn from the top-left (X,Y) coordinate of a rectangle // followed by Width and Height using <drawRect(X,Y,Width,Height)>. // The <fillRect> command draws a rectangle filled with solid pixels. import java.awt.*; import java.applet.*; public class Review02 extends Applet { public void paint(Graphics g) { g.drawRect(50,50,100,100); g.drawRect(300,50,300,150); g.fillRect(50,400,100,100); g.fillRect(300,400,300,150); } }
x, y height width The drawOval Method drawOval(int x, int y, int width, int height) Draws an oval that is circumscribed by the rectangle with top-left corner at coordinate (x,y) using width and height dimensions. fillOval uses identical parameters, but fills in the oval.
// Review03.java 07-23-10 by Leon Schram // This program uses the <drawOval> method to draw ovals and circles. // The four parameters of the <drawOval> method are identical to the parameters // of the <drawRect> method. With <drawOval(X,Y,Width,Height)> (X,Y) is the // coordinate of the top-left corner of the rectangle that circumscribes the oval. // It also shows that the Graphics variable does not have to be "g". import java.awt.*; import java.applet.*; public class Review03 extends Applet { public void paint(Graphics screen) { screen.drawOval(50,50,100,100); screen.drawOval(300,50,300,50); screen.fillOval(50,400,100,100); screen.fillOval(300,400,300,150); } }
x, y 90 180 0, 360 270 width The drawArc Method drawArc(int x, int y, int width, int height, int start, int degrees) Draws part of an oval. The 1st 4 parameters are the same as drawOval. Start indicates the degree location of the beginning of the arc. Degrees indicates the number of degrees traveled by the arc. 0 degrees is at the 3:00 o’clock position and increases counter clockwise to 360 degrees. fillArc uses identical parameters, but “fills” in the arc. height
// Review04.java 07-23-10 by Leon Schram // This program uses the <drawArc> and <fillArcs> methods. // Method <drawArc(X,Y,Width,Height,Start,Degrees)> uses the first four // parameters in the same manner as the <drawOval> method. Start is the // degree value of the arc-start and Degrees is the number of degrees the arc travels. // Start (0 degrees) is at 3:00 o'clock and positive degrees travel counter-clockwise. import java.awt.*; import java.applet.*; public class Review04 extends Applet { public void paint(Graphics g) { g.drawArc(50,50,100,100,0,180); g.fillArc(200,50,100,100,0,270); g.drawArc(350,50,100,100,0,360); g.fillArc(500,50,100,100,0,-180); g.drawArc(50,250,100,200,0,180); g.fillArc(200,250,100,200,0,270); g.drawArc(350,250,200,100,0,360); g.fillArc(350,400,200,100,0,-180); } }
// Review05.java 07-23-10 by Leon Schram // This program demonstrates the significance of using parameters // in the correct sequence Review05.java is very similar to // Review03,java with rearranged parameters. import java.awt.*; import java.applet.*; public class Review05 extends Applet { public void paint(Graphics screen) { screen.drawOval(100,100,50,50); screen.drawOval(50,300,50,300); screen.fillOval(400,50,100,100); screen.fillOval(150,300,400,300); } }
Parameter Sequence MattersReview03.java vs. Review05.java screen.drawOval(50,50,100,100); screen.drawOval(300,50,300,50); screen.fillOval(50,400,100,100); screen.fillOval(300,400,300,150); screen.drawOval(100,100,50,50); screen.drawOval(50,300,50,300); screen.fillOval(400,50,100,100); screen.fillOval(150,300,400,300);
// Review06.java 07-23-10 by Leon Schram // This program demonstrates how to control the output display color with // the <Color> class and the <setColor> method. import java.awt.*; import java.applet.*; public class Review06 extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(50,50,100,100); g.setColor(Color.green); g.fillOval(200,50,100,100); g.setColor(Color.blue); g.fillOval(350,50,100,100); g.setColor(Color.orange); g.fillOval(500,50,100,100); g.setColor(Color.cyan); g.fillOval(50,200,100,100); g.setColor(Color.magenta); g.fillOval(200,200,100,100); g.setColor(Color.yellow); g.fillOval(350,200,100,100); g.setColor(Color.gray); g.fillOval(500,200,100,100); g.setColor(Color.lightGray); g.fillOval(50,350,100,100); g.setColor(Color.darkGray); g.fillOval(200,350,100,100); g.setColor(Color.pink); g.fillOval(350,350,100,100); g.setColor(Color.black); g.fillOval(500,350,100,100); } } <!-- 07-23-10 by Leon Schram --> <APPLET CODE = "Review06.class" WIDTH=800 HEIGHT=600> </APPLET>
The drawString Method drawString(String s, int x, int y) Draws a string s starting at the at coordinate (x,y). Hello there! x, y
// Review07.java 07-23-10 by Leon Schram // This program demonstrates the <drawString> method. // With <drawString("Hello World",x,y)>, the string Hello World // will be displayed starting at the [x,y] pixel coordinate. import java.awt.*; import java.applet.*; public class Review07 extends Applet { public void paint(Graphics g) { g.drawString("This string will display in default black at coordinate [200,250]", 200,250); g.setColor(Color.red); g.drawString("This string will display in red at coordinate [5,50]",5,50); g.setColor(Color.blue); g.drawString("This string will display in blue at coordinate [400,500]",400,500); } } <!-- 07-23-10 by Leon Schram --> <APPLET CODE = "Review07.class" WIDTH=800 HEIGHT=600> </APPLET>
// Review08.java 07-23-10 by Leon Schram // This program shows how you can create your own display colors by // creating color objects with different (Red, Green, Blue) values. // RGB values are in the [0..255] range. import java.awt.*; import java.applet.*; public class Review08 extends Applet { public void paint(Graphics g) { g.setColor(new Color(255,0,255)); g.fillRect(0,0,800,200); g.setColor(new Color(0,255,255)); g.fillRect(0,200,800,200); g.setColor(new Color(100,100,100)); g.fillRect(0,400,800,200); } }
// Review09.java 07-11-11 by Leon Schram // This program shows all the shades of Red, Green and Blue using the // <setColor> method with new RGB parameter values. import java.awt.*; import java.applet.*; public class Review09 extends Applet { public void paint(Graphics g) { for (int red = 0; red <= 255; red++) { g.setColor(new Color(red,0,0)); g.drawLine(red,0,red,600); } for (int green = 0; green <= 255; green++) { g.setColor(new Color(0,green,0)); g.drawLine(green+255,0,green+255,600); } for (int blue = 0; blue <= 255; blue++) { g.setColor(new Color(0,0,blue)); g.drawLine(blue+510,0,blue+510,600); } } }
// Review10.java 07-11-11 by Leon Schram // This program draws four squares with user-defined <Color> objects. import java.awt.*; import java.applet.*; public class Review10 extends Applet { public void paint(Graphics g) { Color myRed = new Color(255,0,64); Color myGreen = new Color(16,255,16); Color myBlue = new Color(64,64,255); Color myBrown = new Color(150,100,15); g.setColor(myRed); g.fillRect(20,100,100,100); g.setColor(myGreen); g.fillRect(140,100,100,100); g.setColor(myBlue); g.fillRect(260,100,100,100); g.setColor(myBrown); g.fillRect(380,100,100,100); } }
// Review11.java // 07-23-10 by Leon Schram // This program draws a pentagon with the <drawPolygon> method. // The <xCoord> array stores the X-Coordinate values. // The <yCoord> array stores the Y-Coordinate values. import java.awt.*; import java.applet.*; public class Review11 extends Applet { public void paint(Graphics g) { g.drawString("OPEN POLYGONS",50,50); int xCoord[] = {150,150,500,600,400}; int yCoord[] = {150,350,350,200,150}; g.drawPolygon(xCoord,yCoord,5); } }
// Review12.java // 07-23-10 by Leon Schram // This program draws a pentagon with the <fillPolygon> method. // The <xCoord> array stores the X-Coordinate values. // The <yCoord> array stores the Y-Coordinate values. import java.awt.*; import java.applet.*; public class Review12 extends Applet { public void paint(Graphics g) { g.drawString("FILLED POLYGONS",50,50); int xCoord[] = {150,150,500,600,400}; int yCoord[] = {150,350,350,200,150}; g.fillPolygon(xCoord,yCoord,5); } }
// Review13.java 07-11-11 by Leon Schram // This program demonstrates how to draw open polygons with the // <drawPolyLine> method. import java.awt.*; import java.applet.*; public class Review13 extends Applet { public void paint(Graphics g) { g.drawString("POLYLINES",30,30); int xCoord1[] = {100,200,300,300,500}; int yCoord1[] = {100,200,200,100,200}; g.drawPolyline(xCoord1,yCoord1,5); int xCoord2[] = {510,530,550,550,590,590,610,630,630,510}; int yCoord2[] = {560,540,560,590,590,540,520,540,630,630}; g.drawPolyline(xCoord2,yCoord2,10); } }
// Review14.java 07-23-10 by Leon Schram // This program displays 1000 random lines. import java.awt.*; import java.applet.*; import java.util.Random; public class Review14 extends Applet { public void paint(Graphics g) { Random rndInt = new Random(12345); for (int k = 1; k <= 1000; k++) { int x1 = rndInt.nextInt(800); int y1 = rndInt.nextInt(600); int x2 = rndInt.nextInt(800); int y2 = rndInt.nextInt(600); g.drawLine(x1,y1,x2,y2); } } }
// Review15.java 07-23-10 by Leon Schram // This program combines random squares with random color objects. import java.awt.*; import java.applet.*; import java.util.Random; public class Review15 extends Applet { public void paint(Graphics g) { Random rndInt = new Random(12345); for (int k = 1; k <= 1000; k++) { int x = rndInt.nextInt(800); int y = rndInt.nextInt(600); int red = rndInt.nextInt(256); int green = rndInt.nextInt(256); int blue = rndInt.nextInt(256); g.setColor(new Color(red,green,blue)); g.fillRect(x,y,50,50); } } }