import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JPanel; import javax.swing.JComponent; // the four tools things we'll use to draw import java.awt.geom.Line2D; // single lines import java.awt.geom.Ellipse2D; // ellipses and circles import java.awt.Rectangle; // squares and rectangles import java.awt.geom.GeneralPath; // combinations of lines and curves /** A component that draws a BowTie by Phill Conrad @author Phill Conrad @version for CS10, S09, UCSB, 04/29/2009 */ public class BowTieComponent extends JComponent { /** The paintComponent method is always required if you want * any graphics to appear in your JComponent. * * It MUST be called a paintComponent, and must take * a Graphics g as its parameter. * * @param g required graphics parameter */ public void paintComponent(Graphics g) { // Recover Graphics2D--we always do this. // See sections 2.12, p. 60-61 for an explanation Graphics2D g2 = (Graphics2D) g; // The following code is adapated from // http://java.comsci.us/examples/awt/geom/GeneralPath.html // The original version used an Applet. java.awt.geom.GeneralPath bowtie; java.awt.geom.GeneralPath filledBowTie; bowtie = new java.awt.geom.GeneralPath (); bowtie.moveTo (50, 50); bowtie.lineTo (50, 150); bowtie.lineTo (200, 50); bowtie.lineTo (200, 150); bowtie.lineTo (50, 50); // bowtie.moveTo (75, 50); //bowtie.lineTo (175, 50); //bowtie.moveTo (75, 150); //bowtie.lineTo (175, 150); g2.draw (bowtie); // You can also draw one that is filled in: filledBowTie = new java.awt.geom.GeneralPath (); filledBowTie.moveTo (250, 50); filledBowTie.lineTo (250, 150); filledBowTie.lineTo (400, 50); filledBowTie.lineTo (400, 150); filledBowTie.lineTo (250, 50); //filledBowTie.moveTo (275, 50); //filledBowTie.lineTo (375, 50); //filledBowTie.moveTo (275, 150); //filledBowTie.lineTo (375, 150); g2.fill (filledBowTie); } }