590 likes | 1.13k Views
Graphics and Java2D Joe Komar Overview Graphics contexts and graphics objects Color control Font control Drawing lines, rectangles, ovals, arcs, polygons, and polylines Java2D API Java2D Shapes Graphics Coordinate System 0,0 is the upper left corner
E N D
Graphics and Java2D Joe Komar Komar Associates
Overview • Graphics contexts and graphics objects • Color control • Font control • Drawing lines, rectangles, ovals, arcs, polygons, and polylines • Java2D API • Java2D Shapes Komar Associates
Graphics Coordinate System • 0,0 is the upper left corner • Each coordinate point represents one pixel • All coordinate values are positive x 0,0 X y x,y Y Komar Associates
Coordinates Applet import java.awt.*; import java.applet.*; public class Coordinates extends Applet { public void paint (Graphics page){ setSize (300,200); page.drawRect (0, 0, 299, 199); page.drawLine (50, 50, 250, 100); page.drawString ("<50, 50>", 25, 45); page.drawString ("<250, 100>", 225, 115); }// method paint }// class Coordinates Komar Associates
Output of Coordinates Applet Komar Associates
Graphics • Graphics “context” enables drawing on the screen • Graphics object manages a graphics context • Graphics is an abstract class implemented on each platform • Component class is primary superclass for Applet and JApplet Komar Associates
paint method of Component • Accepts a Graphics object • Must be overridden • When Applet runs, calls init, start, then paint methods • repaint method called by programmer, which calls the Component’s update then paint method Komar Associates
Colors • Color class is part of java.awt • Colors are defined by a mixture of Red, Blue, and Green • RGB • defined by three numbers ranging from (0,0,0) for black to (255,255,255) for white • over 16 million colors available • many systems cannot distinguish among so many colors • Predefined colors (final static Color objects) • e.g., Color.blue, Color.green, Color.cyan Komar Associates
Defining Colors • Color (int red, int green, int blue) • Integer values between 0 and 255 • Color (float red, float green, float blue) • Decimal values between 0.0 and 1.0 • Color (int rgb) • bits 0-7 represent blue, 8-15 represent green, 16-23 represent red Komar Associates
Defining Colors • Color brown = new Color (107, 69, 38); • brown.getBlue() returns 38 (getRed and getGreen) • brown.brighter() • returns a bit brighter Color • brown.darker() • returns a bit darker Color Komar Associates
Nature Applet import java.awt.*; import java.applet.*; public class Nature extends Applet { public void paint (Graphics page) { setBackground (Color.darkGray); page.setColor (Color.red); page.drawRect (10, 15, 125, 85); page.setColor (Color.green); page.drawString ("Nature's first green", 25,45); page.setColor (Color.yellow); page.drawString ("is gold", 50, 75); }// method paint }// class Nature Komar Associates
Nature Applet Results Komar Associates
ShowColors Example // Fig. 11.5: ShowColors.java // Demonstrating Colors import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ShowColors extends JFrame { public ShowColors() { super( "Using colors" ); setSize( 400, 130 ); show(); } Komar Associates
ShowColors Example public void paint( Graphics g ) { // set new drawing color using integers g.setColor( new Color( 255, 0, 0 ) ); g.fillRect( 25, 25, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(), 130, 40 ); // set new drawing color using floats g.setColor( new Color( 0.0f, 1.0f, 0.0f ) ); g.fillRect( 25, 50, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(), 130, 65 ); // set new drawing color using static Color objects g.setColor( Color.blue ); g.fillRect( 25, 75, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(), 130, 90 ); Komar Associates
ShowColors Example // display individual RGB values Color c = Color.magenta; g.setColor( c ); g.fillRect( 25, 100, 100, 20 ); g.drawString( "RGB values: " + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue(), 130, 115 ); } Komar Associates
ShowColors Example public static void main( String args[] ) { ShowColors app = new ShowColors(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } } Komar Associates
ShowColors Example Komar Associates
JColorChooser Dialog Box • static method showDialog of class JColorChooser presents dialog • Can choose by color swatch; hue, saturation and brightness (HSB); or by red, green, blue (RGB) • Returns the Color object chosen • See example run... Komar Associates
Fonts • Uses currently supported fonts on the system • Constructor takes String font-name, int style, int size • Styles - Font.PLAIN, Font.BOLD, Font.ITALIC • Size is number of points (1/72nd of an inch) Komar Associates
Fonts Example // Fig. 11.9: Fonts.java // Using fonts import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Fonts extends JFrame { public Fonts() { super( "Using fonts" ); setSize( 420, 125 ); show(); } Komar Associates
Fonts Example public void paint( Graphics g ) { // set current font to Serif (Times), bold, 12pt // and draw a string g.setFont( new Font( "Serif", Font.BOLD, 12 ) ); g.drawString( "Serif 12 point bold.", 20, 50 ); // set current font to Monospaced (Courier), // italic, 24pt and draw a string g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) ); g.drawString( "Monospaced 24 point italic.", 20, 70 ); // set current font to SansSerif (Helvetica), // plain, 14pt and draw a string g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) ); g.drawString( "SansSerif 14 point plain.", 20, 90 ); Komar Associates
Fonts Example // set current font to Serif (times), bold/italic, // 18pt and draw a string g.setColor( Color.red ); g.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) ); g.drawString( g.getFont().getName() + " " + g.getFont().getSize() + " point bold italic.", 20, 110 ); } Komar Associates
Fonts Example public static void main( String args[] ) { Fonts app = new Fonts(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } } Komar Associates
Fonts Example Komar Associates
Font Metrics • Height -- total size • Ascent -- basic character size • Leading -- above character • Descent -- below character • Methods to get these as well as getFontMetrics to return a FontMetrics object Komar Associates
Drawing Shapes • Lines, ovals, rectangles, arcs, polygons, and polylines • circle is a specific kind of oval • square is a specific kind of rectangle • polygons include any many-sided shapes • polylines are lines connected end-to-end • Most shapes can be drawn filled or unfilled • e.g. drawOval versus fillOval Komar Associates
Ovals • drawOval (int x, int y, int width, int height) • fillOval (int x, int y, int width, int height) width height Komar Associates
Ovals as Spinning Disk import java.awt.*; import java.applet.*; public class Rotating_Disk extends Applet { public void paint (Graphics page) { int width = 0, height = 40; int x = 100, y = 100, warp = 1; page.setXORMode (getBackground()); for (int change = 1; change < 1000; change++) { width += warp * 2; x -= warp; if (width == 0 || width == 40) warp *= -1; page.fillOval (x, y, width, height); for (int pause = 1; pause <= 500000; pause++); page.fillOval (x, y, width, height); }// for loop }// method paint }// class Rotating_Disk Komar Associates
Spinning Disk In Action Rotating Disk Applet Komar Associates
Rectangles • drawRect (int x, int y, int width, int height) • fillRect (int x, int y, int width, int height) • clearRect (int x, int y, int width, int height) • drawRoundRect (int x, int y, int width, int height, int arc_width, int arc_height) • fillRoundRect (int x, int y, int width, int height, int arc_width, int arc_height) • draw3DRect (int x, int y, int width, int height, boolean raised) • draw3DRect (int x, int y, int width, int height, boolean raised) Komar Associates
Rounded and 3D Rectangles import java.awt.*; import java.applet.*; public class Rectangles extends Applet { public void paint (Graphics page) { page.drawRoundRect (10,10,50,50,25,25); page.setColor (Color.red); page.fillRoundRect (90,10,40,40,10,30); page.setColor (Color.blue); page.fillRoundRect (150,30,50,20,15,15); page.setColor (Color.orange); page.fill3DRect (10,70,50,50,true); page.draw3DRect (70,70,50,50,false); }// method paint }// class Rectangles Komar Associates
Rectangle Results Komar Associates
Arc Angle Start Angle height Width Arcs • drawArc (int x, int y, int width, int height, int start_angle, int arc_angle) • fillArc (int x, int y, int width, int height, int start_angle, int arc_angle) Komar Associates
Arcs Applet import java.awt.*; import java.applet.*; public class Arcs extends Applet { public void paint (Graphics page) { page.drawArc (10,10,50,50,45,225); page.drawArc (70,10,30,70,-180,180); page.setColor (Color.red); page.fillArc (130,10,60,60,-180,-90); page.setColor (Color.blue); page.fillArc (190,10,50,50,45,270); page.setColor (Color.green); page.fillArc (250,10,80,40,-225,180); }// method paint }// Class Arcs Komar Associates
Arcs Applet Results Komar Associates
Polygons • Polygon class can be used to define a polygon • addPoint method adds points to the polygon • Graphics class contains methods to draw a polygon • drawPolygon(int[ ] xpoints, int[ ] ypoints, int numpoints) • drawPolygon(Polygon poly) • fillPolygon(int[ ] xpoints, int[ ] ypoints, int numpoints) • fillPolygon(Polygon poly) • Polygons are always closed shapes -- if the last point specified does not close the shape, it is automatically closed Komar Associates
Polygons Applet import java.awt.*; import java.applet.*; public class Polygons extends Applet { private int[] xset1 = {110,110,115,120,150,135}; private int[] yset1 = {10,40,30,50,15,7}; private int[] xset2 = {195,170,170,195,220,220}; private int[] yset2 = {10,20,40,50,40,20}; private int[] xset3 = {110,150,110,150,110}; private int[] yset3 = {70,70,110,110,70}; private Polygon poly = new Polygon(); Komar Associates
Polygons Applet public void paint (Graphics page) { page.drawPolygon (xset1, yset1, 6); page.drawPolygon (xset2, yset2, 6); page.setColor (Color.red); page.fillPolygon (xset3, yset3, 5); poly.addPoint (170, 70); poly.addPoint (170, 90); poly.addPoint (190, 110); poly.addPoint (220, 90); page.setColor (Color.blue); page.fillPolygon (poly); }// method paint }// class Polygons Komar Associates
Polygons Applet Results Komar Associates
Polylines • drawPolyline (int[ ] xpoints, int[ ] ypoints, int numpoints) • Similar to polygon except a polyline is not a closed shape Komar Associates
Java2D API • Additional drawing capabilities beyond first java.awt release • Use Graphics2D objects • Graphics2D is a subclass of Graphics • Downcast Graphics object to Graphics2D object • Graphics2D g2 = (Graphics2D) g; • Use setPaint, draw, fill methods of Graphics2D Komar Associates
Graphics2D Example // Fig. 11.22: Shapes.java // Demonstrating some Java2D shapes import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; public class Shapes extends JFrame { public Shapes() { super( "Drawing 2D shapes" ); setSize( 425, 160 ); show(); } Komar Associates
Graphics2D Example public void paint( Graphics g ) { // create 2D by casting g to Graphics2D Graphics2D g2d = ( Graphics2D ) g; // draw 2D ellipse filled with a blue-yellow gradient g2d.setPaint( new GradientPaint( 5, 30, // x1, y1 Color.blue, // initial Color 35, 100, // x2, y2 Color.yellow, // end Color true ) ); // cyclic g2d.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) ); Komar Associates
Graphics2D Example // draw 2D rectangle in red g2d.setPaint( Color.red ); g2d.setStroke( new BasicStroke( 10.0f ) ); g2d.draw( new Rectangle2D.Double( 80, 30, 65, 100 ) ); Komar Associates
Graphics2D Example // draw 2D rounded rectangle with a buffered background BufferedImage buffImage = new BufferedImage( 10, 10, BufferedImage.TYPE_INT_RGB ); Graphics2D gg = buffImage.createGraphics(); gg.setColor( Color.yellow ); // draw in yellow gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle gg.setColor( Color.black ); // draw in black gg.drawRect( 1, 1, 6, 6 ); // draw a rectangle gg.setColor( Color.blue ); // draw in blue gg.fillRect( 1, 1, 3, 3 ); // draw a filled rectangle gg.setColor( Color.red ); // draw in red gg.fillRect( 4, 4, 3, 3 ); // draw a filled rectangle Komar Associates
Graphics2D Example // paint buffImage onto the JFrame g2d.setPaint( new TexturePaint( buffImage, new Rectangle( 10, 10 ) ) ); g2d.fill( new RoundRectangle2D.Double( 155, 30, 75, 100, 50, 50 ) ); Komar Associates
Graphics2D Example // draw 2D pie-shaped arc in white g2d.setPaint( Color.white ); g2d.setStroke( new BasicStroke( 6.0f ) ); g2d.draw( new Arc2D.Double( 240, 30, 75, 100, 0, 270, Arc2D.PIE ) ); Komar Associates
Graphics2D Example // draw 2D lines in green and yellow g2d.setPaint( Color.green ); g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) ); float dashes[] = { 10 }; g2d.setPaint( Color.yellow ); g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, dashes, 0 ) ); g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) ); } Komar Associates
Graphics2D Example public static void main( String args[] ) { Shapes app = new Shapes(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } } Komar Associates
Graphics2D Example Komar Associates