Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Notes – Introduction to Graphics In order to display graphics in Java, we will begin by learning how to make applets. For starters, let’s learn the skeleton of creating an applet. import java.applet.*; import java.awt.*; public class <appletClassName> extends Applet { public void init() {} public void paint(Graphics g) {} } Understanding the Structure of an Applet (1) Import two new java packages. These packages provide us with tons of Objects and methods that we can use. (2) Create a new subclass of Applet, which must implement one of the following: init(), start(), or p aint(Graphics g). Unlike the Java applications we’ve been used to, you no longer need to create a m ain method. (3) The init method is called to initialize the applet. (4) The paint method is the method by which the program displays information to the screen. Try out this simple example of an applet: Begin by creating a new java file with the following code: import java.applet.*; import java.awt.*; import java.awt.Color; // in order to create Colors public class HelloWorld extends Applet { public void init() { setBackground(Color.yellow); } public void paint(Graphics g) { g.setColor(Color.red); g.drawString(“Hello World!”, 5, 25); } } You can still use for-loops, while-loops, and all the other concepts you’ve learned already. Try altering your HelloWorld applet’s paint method to the following: public void paint(Graphics g) { g.setColor(Color.blue); for(int i = 0; i <= 100; i+=10) g.drawString(“Hello World!”, 5, i); } First run of HelloWorld: After adding the for-loop: Check out the API on Java’s Graphics class, located in the package java.awt in the JavaDocs http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html Here are a few of the methods you can find in the API: Method Summary abstract void drawArc(int abstract void drawLine(int abstract void drawOval(int abstract void drawPolygon(int[] void x, int y, int width, int height, int startAngle, int arcAngle) Draws the outline of a circular or elliptical arc covering the specified rectangle. x1, int y1, int x2, int y2) Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system. x, int y, int width, int height) Draws the outline of an oval. xPoints, int[] yPoints, int nPoints) Draws a closed polygon defined by arrays of x and y coordinates. drawRect(int x, int y, int width, int height) Draws the outline of the specified rectangle. abstract drawRoundRect(int x, int y, int width, int height, int arcWidth, void int arcHeight) Draws an outlined round-cornered rectangle using this graphics context's current color. abstract void void drawString(String str, int x, int y) Draws the text given by the specified string, using this graphics context's current font and color. fill3DRect(int x, int y, int width, int height, boolean raised) Paints a 3-D highlighted rectangle filled with the current color. abstract void fillArc(int abstract void fillOval(int x, int y, int width, int height, int startAngle, int arcAngle) Fills a circular or elliptical arc covering the specified rectangle. x, int y, int width, int height) Fills an oval bounded by the specified rectangle with the current color. abstract void fillPolygon(int[] abstract void fillRect(int xPoints, int[] yPoints, int nPoints) Fills a closed polygon defined by arrays of x and y coordinates. x, int y, int width, int height) Fills the specified rectangle. abstract fillRoundRect(int x, int y, int width, int height, int arcWidth, void int arcHeight) Fills the specified rounded corner rectangle with the current color. Here are some of the Colors you can use when setting the background c)) or setting the pen color (g.setColor(Color c)): black blue cyan darkGray gray green lightGray magenta orange pink (setBackground(Color red white yellow Also, it is important to realize that Java Graphics are numbered with the origin (0,0) in the top left corner. Now, the confusing part is that instead of going (rows, cols) as we did in the Marine Biology Casestudy, Java graphics are numbered (x-cord, y-cord). Consider an applet of width 400 and height 300 (both width and height are measured in pixels) (0,0) X increases this way (399,0) (0,299) (399,299) Comprehension Questions: 1. 2. 3. 4. 5. In order to create an Applet, what two packages do you need to import? In order to create an Applet, what class must you extend? What method initializes an applet? In applets, instead of a main method, what method do you need to implement? Can you run an applet the way we ran Java Applications? How do you see what an applet does? 6. Can we still use for-loops, while-loops, etc? 7. How do you print text to the screen?