190 likes | 392 Views
Java Unit 5: Applets and Graphics. Drawing and Filling Geometric Shapes. Java comes with more than just points and lines. Within the Graphics2D class, there are also methods for drawing rectangles, circles, and ellipses.
E N D
Java Unit 5: Applets and Graphics Drawing and Filling Geometric Shapes
Java comes with more than just points and lines. • Within the Graphics2D class, there are also methods for drawing rectangles, circles, and ellipses.
Graphics2D g2 = (Graphics2D) g;Rectangle2D.Double myRectangle = new Rectangle2D.Double(10, 20, 30, 40);g2.draw(myRectangle);myRectangle = new Rectangle2D.Double(50, 20, 30, 40);g2.fill(myRectangle);
Rectangle2D and Ellipse2D • Rectangle2D is a class from which rectangle shaped objects derive from. • They are constructed like so: • Rectangle2D.Double mRect = new Rectangle2D.Double(x, y, width, height); • X and Y refer to the upper-left corner of the rectangle. • Ellipse2D also works the same way, except only its Double and Float classes can be used: • Ellipse2D.Double mEllipse = new Ellipse2D.Double(x, y, width, height); • There is no ‘Ellipse’ class that derives from Ellipse2D. • Also note that there is no way to define the ‘center’ of the ellipse. Only the upper-left hand corner.
Rectangle2D and Inheritance • Rectangle2D works similarly to Point2D. • There are Double and Float classes for it, accessed by using Rectangle2D followed by a dot. • There is also a Rectangle class that extends the Rectangle2D class.
Draw and fill • The previous example used two methods of drawing. • g2.draw(mRectangle); • g2.fill(mRectangle); • ‘Draw’ methods typically draw the outline of a shape, while ‘fill’ methods will fill the entire body of the shape. • Graphics2D also has methods for drawing shapes without defining the rectangle in its own variable. • G2.drawRect(x, y, width, height); • G2.fillRect(x, y, width, height);
Much Excite! • Tired of dull and boring shape drawings? • The Java API has a built-in Color class, too! • Include it in your project today with: • import java.awt.Color;
Color doesn’t follow naming conventions!? • There is a way to fetch pre-defined colors from the Color class. There are constants built into Color, named according to the color they should represent. • Examples: • Color.red, Color.blue, Color.magenta, Color.white… • Notice that even though they are constants, they are completely spelled with lower case letters.
Creating your own colors • Colors can be constructed like: • Color myCol = new Color(float r, float g, float b); • r, g, and b are values between 0.0 and 1.0, with 0.0 being no color, and 1.0 being the maximum amount of one color that can be used. • Color myCol = new Color(0.75F, 0.0F, 0.25F); • Notice that we are using floats instead of doubles for this constructor. • It is important to remember that all ‘float’ values end with an ‘F’. This denotes that it is a float value, and not a double value. Without it, there would be a compiler error.
Using Colors • This involves our Graphics2D object, g2. • As already noted, you can think of Graphics as a pen. • Before you draw a shape, there is a method called ‘setColor()’ which you can call to change the color of what you’re drawing. • g2.setColor(Color.red);g2.draw(myRectangle); • This would draw a red rectangle.
Words!! • Of course, it’s also possible to draw text onto graphical output, as well as defining fonts for drawing. • Graphics2D’s drawString method: • G2.drawString(String text, int x, int y); • When defining x and y, you are defining where the text’s left-hand base line is (typically, the bottom of the text), and not the upper-left hand corner of where text is being drawn.
Fonts!! • You may want to change the font that we use in drawing. • Import this class: • import java.awt.Font; • Among the system supplied fonts in Java are: • Serif • SansSerif • Monospaced • Dialog • DialogInput
Fonts!! • The fonts can also be set to different sizes, which are given in measurements of ‘points’. • Fonts can also be given different characteristics such as bold and italic.
Fonts in Action!! • Font myFont = new Font(“Serif”, Font.BOLD, 16);g2.setFont(myFont);g2.drawString(“My text”, 20, 30); • The constructor for Font takes in a string containing the name of the font, an integer constant defined in the Font class, and the point size as an integer. • Just like with colors, we are calling the setFont() method to set the font before we draw the text. • Finally, we draw the string like usual.
Text Positioning • Unfortunately, it will be difficult to determine where exactly text will go when you define its position. • Getting it in the right place may require trial and error. • Just keep adjusting the position until it looks right.
Final Example An example applet of the graphical techniques covered is on our website at math.uaa.alaska.edu/~android/java/unit5. You could try compiling it yourself to see how all of this graphical wizardry works.
Things to try doing: • Change the positioning, width, and height of the shapes. • Changing the colors. • Changing colors at different points within the code. • Change the font, point size, or characteristics of the text drawn