21 Days:
Wk1
. Wk1Ref
. Applts
. Swing
. Grph
. D13-14
. D15-21
. D22-28
cc12: Miscl . SQL . EJB . Vocab . (NotesHelp) . J2EE . Web misl: 21 Days(src) . Dictionary(foldoc) . Unicode . Tables . Print . html misl: F.Allimant . Swing (J.H.University) |
Java Home
Refererences and Links.
-----Feb/??/2003 ??? :
Notes.
DAY 12: Color, Fonts, and Graphics. (pg.297-328: 32 pages)
===Introduction and Graphics Classes (pg.297) - For graphics, use classes of java.awt and javax.swing packages. - With Java2D classes, you can do better visual effects, and with little code. Such as: » Anti-aliased objs » Gradient fill patterns » Draw lines of varied widths. - java.awt's Graphics class contains most of the methods for basic drawing. - "Objects of this class represent an environment in which something can be drawn, whether it's an applet window, a section in a graphical user interface, or a printer." » ie, A Graphics or Graphics2D object REPRESENTS AN AREA BEING DRAWN TO. - Can draw: arcs, circles, lines, ovals, rectangles (polygons), and text. - An applet can be thought of as a canvas to draw on. » The Graphics obj, already created. It is a parameter to one of the paint() methods. » To draw on an applet: "import java.awt.*;" (and maybe often: "import java.util.*;"). ===Creating a Drawing Surface (pg.298) - JPanel can be drawn on. /***(quoted from: Sun's documentation)*********************************************** ===protected void paintComponent(Graphics g) - myNote: Method for "javax.swing.JPanel" (superclass is "javax.swing.JComponent"). » D21NOTE: paintComponent(Graphics) method called automatically when component needs redisplayed. JPanel's paintComponent(Graphics) has SAME PURPOSE AS APPLET'S paint(Graphics). - Calls the UI delegate's paint method ... - IF you override this in a subclass you should NOT make permanent changes to the passed in Graphics. For example, you should not alter the clip Rectangle or modify the transform. If you need to ... - Parameters: g - the Graphics object to protect - See Also: paint(java.awt.Graphics), ComponentUI ===Container getContentPane() - myNote: Method of "javax.swing.JFrame". - Returns the contentPane object for this frame. » D21Note: The contentPane obj represents the part of the frame that can contain other components. ************************************************************************************/ - To add JPanel to JFrame window: "myJFrame.getContentPane().add(myJPanel);" - paintComponent(Graphics) is available for class JPanel and is actually from its superclass, Class JComponent. » By subclassing JPanel further to create your own class, you can override the paintComponent method and place all graphic calls in this method. ===Casting a Graphics2D Object (pg.299) - To draw, use a Graphics obj or a Graphics2D obj. To choose "which class...depends on whether you'll be using the Java2D graphics features introduced in Java 2." - To use Java2D features from a "Graphics obj", you must create a new "Graphics2D obj". For example: Graphics2D graphics2obj = (Graphics2D)graphics_Obj; - To use Graphics2D methods, do not mix with Graphics calls. (pg.318.middle) ===An Application - Map.java (pg.299) - Start of Map.java. I called it MapStart.java. - Note: Redraw seems to not work right, yet. And adding look and feel did not help. ===Coordinates (pg.301) - Pixels are the units. They are integer. - The origion (0,0) is at the top left. . Positive x values go towards right. . Positive y values go towards bottom. ===Draw and Fill (pg.301) - Most shapes can be drawn or filled. For both, the outline is done in the current color. - In the next chapter, Day 13, we use the Image class to display gif and jpg files. - comp2D.setBackground(Color.lightGray); // Will NOT redraw bckGrnd with just this one line. comp2D.clearRect(x,y,width,height); // Will clear the indicated rectangle. ===Shapes: Line, Rectangle, Polygon, Oval, and Arc (pg.302) - Most or all of the following draw methods belong to class java.awt.Graphics. - public abstract void drawLine(int 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." (sun docs) . Place the call in JPanel's paintComponent(Graphics) or APPLET'S paint(Graphics). - public void drawRect(int x,int y,int width,int height); - public abstract void fillRect(int x,int y,int width,int height); . x, y - indicates is the top-left of the rectangle. - public abstract void drawRoundRect(int x,int y,int width,int height ,int arcWidth,int arcHeight); - public abstract void fillRoundRect(int x,int y,int width,int height ,int arcWidth,int arcHeight); - public abstract void drawPolygon(int[] xPoints,int[] yPoints,int nPoints); - public abstract void fillPolygon(int[] xPoints,int[] yPoints,int nPoints); . Compiler error results if, nPoints does not match, number points in x and y arrays. . Tip: If xAy is an array, then xAy.length returns an int array length. . Before Java version 2, drawPolygon() worked like drawPolyline() does now. - public void drawPolygon(Polygon p); - public void fillPolygon(Polygon p); . Polygon objects allows, adding points individually. - public abstract void drawPolyline(int[] xPoints,int[] yPoints,int nPoints); . Does not conect the last point to the first point. - public abstract void drawOval(int x,int y,int width,int height); - public abstract void fillOval(int x,int y,int width,int height); . Draws circles and ovals. . x, y - indicates is the top-left of area where oval is drawn. - public abstract void drawArc(int x,int y,int width,int height,int startAngle,int arcAngle); - public abstract void fillArc(int x,int y,int width,int height,int startAngle,int arcAngle); . int startAngle, - Starting place of arc. In arc's plane (not display's) 90 even for slanted circles (ie, ovals). +------+------+ | | | . int arcAngle - Degree length of arc. | | | If negative (0 to 359) --> clockwise direction. | | | If positive (0 to -359) --> counter-clockwise. 180 + + + 0 | . See: element.html for good arc test code. | | degrees | | | | | | +------+------+ 270 degrees ===Copy and Clear (pg.310) - public abstract void copyArea(int x,int y,int width,int height,int dx,int dy); . Copies an area of the window to another of the window. (must be visible, (probably)) - public abstract void clearRect(int x,int y,int width,int height); - public Dimension getSize(); . "Returns the size of this component in the form of a Dimension object." . int height - "The height dimension; negative values can be used." . int width - "The width dimension; negative values can be used." (Sun docs) . In Java 1.0, getSize() is called size(). ===Text and Fonts (pg.311) - java.awt.Font is the class used to store font name, style, and point size. . The class FontMetrics has methods to find exact dimensions of different fonts. This info would help better format text placement, etc. - Java's built in fonts: TimesRoman, Helvetica, Courier, Dialog, and DialogInput. - With Java 2, it is better to use generic names for the fonts, incase font not there: . Before: TimesRoman, Helvetica , and Courier . . Java 2: serif , sanserif , and monospaced. - Font styles: Font.PLAIN, Font.BOLD, and Font.ITALIC. (Can be added together) - setFont(java.awt.Font) method can be used on several object types. - public abstract void drawString(String str,int x,int y); . y - baseline of text output. ===Color (pg.314) - Classes of interest: Color and ColorSpace. - Constructor Color(r,g,b) - r,g,b can be either (int: 0 to 255) or (float: 0.0 to 1.0). - The class java.awt.color.ColorSpace "contains methods that transform colors in a specific color space to/from sRGB and to/from a well-defined CIEXYZ color space. The purpose ... is to support conversions between any two color spaces at a reasonably high degree of accuracy." (Sun docs) . Use of sRGB is usually adequate. - Call setColor() on the Graphics or Graphics2D object that represents area drawing to. - RBG values: black(0,0,0), red(255,0,0), yellow(255,255,0), magenta(255,0,255), cyan(0,255,255), pink(255,175,175), gray(128,128,128), orange(255,200,0), white(255,255,255). - setForeground() - (like setColor()) called on user-interface components to change their color, such as buttons or a window. - void setBackground(Color c); - "Sets the background color of this component." . Change background color attribute of applet window, frame, etc. - void setForeground(Color c); - "Sets the foreground color of this component." . Called on user-interface components to change their color, such as buttons or a window. (similar to setColor()) . Can be used (or setColor()) in applet's init() method, to set color for drawing methods. - public abstract Color getColor(); - Gets Graphics obj's current color. - public Color getForeground(); - Gets Component's obj's Foreground color. - public Color getBackground(); - Gets Component's obj's Background color. - Classes that setBackground() AND setForeground() methods are both found in: java.awt.Component. java.awt.Component.AccessibleAWTComponent. java.awt.List.AccessibleAWTList.AccessibleAWTListChild. java.awt.MenuComponent.AccessibleAWTMenuComponent. javax.accessibility.AccessibleComponent. javax.swing.JComponent. javax.swing.JList.AccessibleJList.AccessibleJListChild. javax.swing.JTable.AccessibleJTable.AccessibleJTableCell. javax.swing.JTree.AccessibleJTree.AccessibleJTreeNode. javax.swing.table.DefaultTableCellRenderer. javax.swing.table.JTableHeader.AccessibleJTableHeader.AccessibleJTableHeaderEntry. javax.swing.text.StyleConstants. - Classes that the setBackground() method is found in, but not setForeground(): java.awt.Graphics2D. java.awt.TextComponent. javax.swing.tree.DefaultTreeCellRenderer. org.w3c.dom.css.CSS2Properties. org.w3c.dom.html.HTMLBodyElement. ===Advanced Graphics (of Java 2D) (pg.317) - Java2D features include: . "Special fill patterns such as gradients and patterns" . "Strokes that define the width and style of a drawing stroke" . "Anti-aliasing to smooth edges of drawn objects" B19.pg317. ===Coordinate Spaces (pg.317) - New Term: Coordinate space - Any 2D area describable by x,y coordinates. - Before Java 2, only the devices coordinate space was used. Java 2 requires another called the user coordinate space. . In Java 2, when creating an object to be drawn on, you refer to the user's coordinate space. Its origion can be moved. And rotation can be done at the origion. ===Setting Rendering Attributes (pg.318) - Non-2D drawing only have one attribute that is adjustable. It is color. - With 2D, you can set color, line width, fill patterns, transparency, and many more. - Fill Paterns. - Java 2D fill methods can use: solid color, gradient fill, texture, or a pattern of your own. - public abstract void setPaint(Paint paint); . Can use Graphics2D setPaint() to define the fill pattern used for fill methods. - New Term: Gradient fill - A gradual shift of color, from point to point. . Acyclic gradient - The gradual color shift occurs once, pt to pt. . Cyclic gradient - The gradual color shift occurs pt to pt and repeats. - GradientPaint(float x1,float y1,Color c1,float x2,float y2,Color c2,boolean cyclic); "Constructs either a cyclic or acyclic GradientPaint object ..." (Sun docs) - // Example: How to select a custom fill pattern. GradientPaint myAcyclicGP = new GradientPaint(0F,0F,Color.white,100F,45F,Color.blue); comp2D.setPaint(myAcyclicGP); - // Example: How to change the pen width, endcap style, and junture style. BasicStroke pen = BasicStroke(2.0F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); setStroke(pen); ===Creating Objects and Drawing Them (pg.321) - ??? See Book and read intro to this subject. It looks like you the rendering may not apply to the draw and fill methods covered up to this point. ??? - "Drawn ojs in Java2D are created by defining them as geometric shapes using the java.awt.geom package classes." B19.pg.321 - Lines (pg.321), Rectangles (pg.321), Ellipses (pg.321), Arcs (pg.322), Polygons (pg.322). . Example: Line2D.Float ln = new Line2D.Float(60F,5F,13F,28F); . Example: Rectangle2D.Float ln = new Rectangle2D.Float(60F,5F,13F,28F); - Drawing: "After you have defined the rendering... and created the object to be drawn, you're ready to draw something in all its 2D glory." B19.pg.323 . All shapes use Graphics2D's draw(Shape s) and fill(Shape s). . Strings use drawString(String str,float x,float y). - A 2D Drawing example (in an applet): Map2D.java. B19.pg.324. ===Summary and QA (pg.326) - Non-2D drawing require use of Graphics class methods that describe the shape. - Java2D uses two methods, draw() and fill(), to output. - Java does not keep track of any "current point". You need to know the x,y of each output. ************************************************************************************* B19 - Teach Yourself Java 2 in 21 Days. 2nd ed. Lemay and Cadenhead. © 2001. *************************************************************************************