1 / 22

Drawing Text

Drawing Text. x. y = 100. A Graphics object is passed as a parameter, and the Graphics method drawString is called. MyJApplet overrides method paint( ). y. x = 20. A First Applet. MyJApplet. public class MyJApplet extends JApplet { public void paint (Graphics g) {

dom
Download Presentation

Drawing Text

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Drawing Text

  2. x y = 100 A Graphics object is passed as a parameter, and the Graphics method drawString is called. MyJApplet overrides method paint( ) y x = 20 A First Applet MyJApplet publicclass MyJApplet extends JApplet { publicvoid paint (Graphics g) { g.drawString(“hello, world!”, 20, 100); } } hello, world! MyJApplet inherits from JApplet The string “hello, world!” is drawn in a “box” whose upper left-hand corner is at coordinates (20, 100)

  3. Class JApplet JApplet is a “heavyweight” class in javax.swing that inherits from its awt precursor (Applet). To create your own Applet you will first extend JApplet. Class JApplet has a number of predefined methods that are “inherited” by the class derived from the base class, JApplet. Some of these methods are empty and will need to be overridden by the the author of the derived class when used. Two of these methods are init( ) and paint( ). Method init( ) is used to “register” components such as JButton and JTextField that are placed on an applet. Method paint( ) is inherited from Component, and gives instructions for coloring and drawing objects on the Component.

  4. Applets Class MyJApplet that we described in the second slide does not contain any instance variables that have to be initialized nor any Component objects that have to be registered. There is no need to override method init( ). Method paint( ) is called by the browser in which the applet is running and causes the string “hello, world!” to be drawn on the screen.

  5. 2. Call function drawString( ) 3. Fill in the arguments 5. Return back to the calling location The Anatomy of a Program 1. Browser runs applet -- executes method paint( ) import java.awt.*; //To include class Graphics into the program public class MyJApplet extends JApplet { public void paint (Graphics g) { g.drawString(“hello, world!”, 20, 100); } } class Graphics { ….. public void drawString(String s, int x, int y) { //code to draw the string at indicated location } } 4. Execute the method

  6. Class Font We can choose the name, size, and style of the font for the strings that we paint on our applet. There are three basic fonts that should be supported on any platform. Font Name Appearance Java Name Helvetica Helvetica SansSerif TimesRoman Serif TimesRoman Monospaced Courier Courier There are three Font class constants that are used to specify the style of the font. Font.BOLD Font.ITALIC Font.PLAIN

  7. Font name style Size in pixel Class Font You can create a new Font object using the Font constructor. (If you do not specify a Font object, Java will choose a default font of its own.) Font myFont = new Font(“Monospaced”, Font.BOLD, 20); g.setFont(myFont); g.drawString(“This string is in courier bold - 20 pixels”,10, 10);

  8. Font Class Alternatively one can construct a new Font object in the Graphics setFont( ) method. g.setFont(new Font(“SansSerif”,font.ITALIC, 18)); You can combine styles by adding them. g.setFont(new Font(“SansSerif”, font.BOLD + font.ITALIC, 12)); g.drawString(“I am Helvetica, Bold, and Italicized”,10, 20);

  9. The Graphics Class Class Graphics includes the following methods that are available for use by an application programmer: publicvoid drawString (String s, int x, int y) { } publicvoid drawLine(int x1, int y1, int x2, int y2) { } publicvoid drawRect(int x, int y, int width, int height) { } publicvoid fillRect (int x, int y, int width, int height) { } publicvoid setColor(Color c) { } Produces a line that connects the two end-points Parameters include the coordinates of the upper left-hand corner, width and length Fills the rectangle with the current drawing color Sets the current drawing color as indicated public void clearRect(int x, int y, int width, int height) { } Clears area of rectangle by painting it the background color

  10. width height Graphics 2 - dimensional shapes are drawn inside of a specified rectangular « bounds box ». drawOval (int x1, int y1, int width, int height) { } (x1, y1)

  11. width height Graphics 2 - dimensional shapes are drawn inside of a specified rectangular « bounds box ». fillOval (int x1, int y1, int width, int height) { } (x1, y1)

  12. Graphics An arc is formed by drawing an oval between a start angle and a finish angle. The start angle is measured from the positive x-axis and is expressed in degrees. The end angle is expressed in degrees from the start angle. Angles extend from the center of the bounds box. drawArc(int x, int y, int width, int height, int startAngle, int endAngle) { } drawArc(20, 100, 200, 50, 210, 120); 200 (20, 100) x 50 210o 120o arc A smiley face

  13. Type declaration amount of green and blue amount of red identifier Memory for an object is dynamically allocated using the new operator Color Color is a class in the package java.awt. When drawing a picture we will need to create some Color objects. To draw an apple, we will need three colors: red for the fruit, brown for the stem, green for the leaf. We may create three Color objects by specifying the amount of each primary color (red, green, blue) we want to use – a n integer betwee 0 and 255. Color fruitColor = new Color(255, 0, 0);

  14. Color We may set the color in our Graphics object to that of our fruitColor: Color fruitColor = new Color(255, 0, 0); g.setColor(fruitColor); Alternatively we can set the color in our graphics object using a color constant provided by the class Color: g.setColor(Color.red); Other color constants include: cyan magenta orange pink white darkGray black blue green lightGray gray yellow

  15. Color One may draw a blue rectangle using the following code in method paint( ). public class DrawRectangle extends JApplet { public void paint(Graphics g) { Color rectColor = new Color(0, 0, 255); g.setColor(rectColor); g.fillRect(20, 20, 200, 100); } }

  16. Applet 100 200 (20, 20)

  17. Draw an Apple in your Applet

  18. Drawing an Apple in an Applet In order to obtain the exact colors one wants for a particular application, one should use Color constructors that take three integers between 0 and 255 as parameters. These three integers represent the amount of red, green, and blue components that are used in a color. The color 0, 0, 0 is black, The color 255, 255, 255 is white, and The color 128, 128, 128 is medium gray.

  19. Drawing an Apple in an Applet To obtain the same colors as in our picture of the apple, we will need to make the following declarations: Color appleColor = new Color(238, 23, 30); The deep red color of the apple is 94% red, but contains some shadings of green and blue as well. Color stemColor = new Color (92, 67, 35); A deep brown. Color leafColor = new Color(23, 117, 37); A dark green.

  20. Graphics Use the graphics methods we have previously listed to construct an apple in an applet. import javax.swing.*; import java.awt.*; publicclass AppleDrawing extends JApplet { public void paint(Graphics g) { //set the color of the outline to black, the color of the apple to red, and the stem to brown Color outline = new Color(0, 0, 0); Color appleColor = new Color(238,23,30); Color stemColor = new Color(92,67,35); Color leafColor = new Color(23, 117,37); g.setColor(appleColor); g.fillOval(75 ,30, 250, 350); g.setColor(stemColor); g.fillRect(195, 10, 10, 30); g.setColor(leafColor); g.fillOval(205, 30, 50, 20); g.setColor(new Color(outline)); g.drawOval(75, 30, 250, 350); g.drawRect(195,10,10, 30); //stem g.drawOval(205, 30, 50, 20); //leaf } }

  21. Graphics Use the graphics methods we have previously listed to construct a face in an applet. Complete the following code. import javax.swing.*; import java.awt.*; publicclass FaceDrawing extends JApplet { public void paint(Graphics g) { //add your own code here //construct a face as indicated on the next slide } }

  22. Graphics Locate the eyes relative to the bounds box for the face. The nose is formed by connecting three lines into a triangle. The top of the nose is in the middle of the face. Bounds boxes for the two eyes -- draw a white oval with a blue or brown circle inside. Two bounds boxes to locate the ears relative to the face. The ears shoud be ovals that have the same color as the face and are drawn before the face. Draw an arc inside of a bounds box to form the mouth. You may make your face a smiley face or a sad face. You may add other facial features such as eyebrows, hair, a mustache, or goatee as you choose.

More Related