See Graphics for details about the Graphics class.
// LinesRectsOvals.java // Drawing lines, rectangles and ovals import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LinesRectsOvals extends JPanel { public void paint( Graphics g ) { super.paint(g); g.translate(0,-25); g.setColor( Color.red ); g.drawLine( 5, 30, 350, 30 ); g.setColor( Color.blue ); g.drawRect( 5, 40, 90, 55 ); g.fillRect( 100, 40, 90, 55 ); g.setColor( Color.cyan ); g.fillRoundRect( 195, 40, 90, 55, 50, 50 ); g.drawRoundRect( 290, 40, 90, 55, 20, 20 ); g.setColor( Color.yellow ); g.draw3DRect( 5, 100, 90, 55, true ); g.fill3DRect( 100, 100, 90, 55, false ); g.setColor( Color.magenta ); g.drawOval( 195, 100, 90, 55 ); g.fillOval( 290, 100, 90, 55 ); } public static void main( String args[] ) { JFrame frame = new JFrame("Drawing lines, rectangles and ovals" ); LinesRectsOvals panel = new LinesRectsOvals(); frame.setSize( 400, 165 ); Container c = frame.getContentPane(); c.add(panel); frame.show(); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }
// DrawArcs.java // Drawing Arcs import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DrawArcs extends JPanel { public void paint( Graphics g ) { super.paint(g); g.translate(0,-25); // start at 0 and sweep 360 degrees g.setColor( Color.yellow ); g.drawRect( 15, 35, 80, 80 ); g.setColor( Color.black ); g.drawArc( 15, 35, 80, 80, 0, 360 ); // start at 0 and sweep 110 degrees g.setColor( Color.yellow ); g.drawRect( 100, 35, 80, 80 ); g.setColor( Color.black ); g.drawArc( 100, 35, 80, 80, 0, 110 ); // start at 0 and sweep -270 degrees g.setColor( Color.yellow ); g.drawRect( 185, 35, 80, 80 ); g.setColor( Color.black ); g.drawArc( 185, 35, 80, 80, 0, -270 ); // start at 0 and sweep 360 degrees g.fillArc( 15, 120, 80, 40, 0, 360 ); // start at 270 and sweep -90 degrees g.fillArc( 100, 120, 80, 40, 270, -90 ); // start at 0 and sweep -270 degrees g.fillArc( 185, 120, 80, 40, 0, -270 ); } public static void main( String args[] ) { JFrame frame = new JFrame("Drawing Arcs" ); DrawArcs panel = new DrawArcs(); frame.setSize( 300, 170 ); Container c = frame.getContentPane(); c.add(panel); frame.show(); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }
// DrawPolygons.java // Drawing polygons import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawPolygons extends JPanel { public void paint( Graphics g ) { super.paint(g); g.translate(-5,-30); int xValues[] = { 20, 40, 50, 30, 20, 15 }; int yValues[] = { 50, 50, 60, 80, 80, 60 }; Polygon poly1 = new Polygon( xValues, yValues, 6 ); g.drawPolygon( poly1 ); int xValues2[] = { 70, 90, 100, 80, 70, 65, 60 }; int yValues2[] = { 100, 100, 110, 110, 130, 110, 90 }; g.drawPolyline( xValues2, yValues2, 7 ); int xValues3[] = { 120, 140, 150, 190 }; int yValues3[] = { 40, 70, 80, 60 }; g.fillPolygon( xValues3, yValues3, 4 ); Polygon poly2 = new Polygon(); poly2.addPoint( 165, 135 ); poly2.addPoint( 175, 150 ); poly2.addPoint( 270, 200 ); poly2.addPoint( 200, 220 ); poly2.addPoint( 130, 180 ); g.fillPolygon( poly2 ); } public static void main( String args[] ) { JFrame frame = new JFrame("Drawing Polygons"); DrawPolygons panel = new DrawPolygons(); frame.setSize( 275, 230 ); Container c = frame.getContentPane(); c.add(panel); frame.show(); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }
See FontMetrics
// Fonts.java // Using fonts import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Fonts extends JPanel { public void paint( Graphics g ) { super.paint(g); g.translate(-5,-30); // 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 ); // 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 ); } public static void main( String args[] ) { JFrame frame = new JFrame("Using fonts" ); Fonts panel = new Fonts(); frame.setSize( 420, 125 ); Container c = frame.getContentPane(); c.add(panel); frame.show(); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }
// Metrics.java // Demonstrating methods of class FontMetrics and // class Graphics useful for obtaining font metrics import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Metrics extends JPanel { public void paint( Graphics g ) { super.paint(g); g.translate(-5,-20); g.setFont( new Font( "SansSerif", Font.BOLD, 12 ) ); FontMetrics fm = g.getFontMetrics(); g.drawString( "Current font: " + g.getFont(), 10, 40 ); g.drawString( "Ascent: " + fm.getAscent(), 10, 55 ); g.drawString( "Descent: " + fm.getDescent(), 10, 70 ); g.drawString( "Height: " + fm.getHeight(), 10, 85 ); g.drawString( "Leading: " + fm.getLeading(), 10, 100 ); Font font = new Font( "Serif", Font.ITALIC, 14 ); fm = g.getFontMetrics( font ); g.setFont( font ); g.drawString( "Current font: " + font, 10, 130 ); g.drawString( "Ascent: " + fm.getAscent(), 10, 145 ); g.drawString( "Descent: " + fm.getDescent(), 10, 160 ); g.drawString( "Height: " + fm.getHeight(), 10, 175 ); g.drawString( "Leading: " + fm.getLeading(), 10, 190 ); } public static void main( String args[] ) { JFrame frame = new JFrame("Demonstrating FontMetrics" ); Metrics panel = new Metrics(); frame.setSize( 510, 210 ); Container c = frame.getContentPane(); c.add(panel); frame.show(); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }
Maintained by John Loomis, last updated 1 June 2000