/* * ========================================================================== * DemoClipping.java : This applet demonstrates clipping along a circle * positioned at the center of a canvas. The program clips the already * clipped portion when the radio button "Clip Further" is selected. If the * radio button "Clip" is selected again, the program clips the canvas as in * the beginning. * * This program is an applet implementation of a standalone program presented * in S. Pantham (1999). * * Adapted from : Pantham S., Pure JFC Swing, 1999. * Modified by : Mark Austin March, 2001 * ========================================================================== */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.awt.geom.*; public class DemoClipping extends JApplet { DemoClippingCanvas canvas; JRadioButton clipButton, clipFurButton; public void init() { // 1. Get the content pane Container contentPane = getContentPane(); contentPane.setBackground( Color.white ); // 2. Create a display canvas and add it to the frame canvas = new DemoClippingCanvas(); contentPane.add(canvas); // 3. Create a panel and add mutually exclusive radio buttons; // then add the panel to the frame's content panel JPanel panel = new JPanel(); panel.setBorder(new TitledBorder( "Clip or Clip Further Using the Radio Buttons...")); panel.setLayout( new GridLayout(1, 2) ); clipButton = new JRadioButton("Clip", true); clipButton.addActionListener(new RadioButtonListener()); clipFurButton = new JRadioButton("Clip Further"); clipFurButton.addActionListener(new RadioButtonListener()); ButtonGroup group = new ButtonGroup(); group.add(clipButton); group.add(clipFurButton); panel.add(clipButton); panel.add(clipFurButton); contentPane.add(BorderLayout.SOUTH, panel); } // 4. Radio button listener class RadioButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { JRadioButton temp = (JRadioButton) e.getSource(); if (temp.equals(clipButton)) { canvas.clip = true; canvas.clipFurther = false; canvas.repaint(); } else if (temp.equals(clipFurButton)) { canvas.clipFurther = true; canvas.repaint(); } } } } // 5. Canvas to paint the "Hello! Graphics World!" text class DemoClippingCanvas extends Canvas { boolean clip = true; boolean clipFurther = false; DemoClippingCanvas() { setBackground( Color.white ); } public void paint(Graphics g) { // 6. Set up the 2D graphics context Graphics2D g2 = (Graphics2D) g; // 7. Retrieve the size of the canvas int w = getSize().width; int h = getSize().height; // 8. if the clip button is selected or when the frame is // initially loaded, clip along or show a circle using // the setClip() method. if (clip) { Ellipse2D e = new Ellipse2D.Float(w/4.0f, h/4.0f, w/2.0f, h/2.0f); g2.setClip(e); // Draw a filled rectangle with red color. Rectangle is // of the size of the canvas. g2.setColor(Color.red); g2.fillRect(0, 0, w, h); } // 9. if the clipFurther button is selected, use clip() method // to clip along a rectangle over the existing circle. if (clipFurther) { Rectangle r = new Rectangle(w/2, h/2, w/2, h/2); g2.clip(r); g2.setColor(Color.green); g2.fillRect(0, 0, w, h); } } }