// TArc.java /* * * */ import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class TArc extends JApplet { // Canvas for drawing an arc DrawingCanvas canvas; // Combo boxes for arc type and fill color selections JComboBox arcBox, fillBox; // Sliders to control arc parameters JSlider slider, sliderX, sliderY, sliderW, sliderH, sliderT0, sliderT; // Arrays to be used for the arc and color selection comboboxes String[] arcLabels = {"Open", "Chord", "Pie"}; int[] arcTypes = {Arc2D.OPEN, Arc2D.CHORD, Arc2D.PIE}; String[] colorLabels = {"Black", "White", "Red", "Green", "Blue"}; Color[] colors = {Color.black, Color.white, Color.red, Color.green, Color.blue}; public void init() { // 1. Get the content pane Container container = getContentPane(); // 2. Create the drawing canvas canvas = new DrawingCanvas(); // 3. Create sliders to control arc parameters by using the method // defined in snippet-9 setSlider(0, canvas.getWidth(), canvas.getWidth()/4, canvas.getWidth()/2, canvas.getWidth()/4); sliderX = slider; setSlider(0, canvas.getHeight(), canvas.getHeight()/4, canvas.getHeight()/2, canvas.getHeight()/4); sliderY = slider; setSlider(0, canvas.getWidth(), canvas.getWidth()/2, canvas.getWidth()/2, canvas.getWidth()/4); sliderW = slider; setSlider(0, canvas.getHeight(), canvas.getHeight()/2, canvas.getHeight()/2, canvas.getHeight()/4); sliderH = slider; setSlider(0, 360, 45, 180, 45); sliderT0 = slider; setSlider(0, 360, 135, 180, 45); sliderT = slider; // 4. Add the labels and sliders to a panel JPanel panel1 = new JPanel(); panel1.setLayout(new GridLayout(3,3)); panel1.add(new JLabel("Location (x,y): ", JLabel.RIGHT)); panel1.add(sliderX); panel1.add(sliderY); panel1.add(new JLabel("Size (w,h): ", JLabel.RIGHT)); panel1.add(sliderW); panel1.add(sliderH); panel1.add(new JLabel("Angles (Th0, Th): ", JLabel.RIGHT)); panel1.add(sliderT0); panel1.add(sliderT); container.add(panel1, BorderLayout.NORTH); // 5 Create combo boxes for selecting arc type and fill color arcBox = new JComboBox(arcLabels); arcBox.setSelectedIndex(0); arcBox.setAlignmentX(Component.LEFT_ALIGNMENT); arcBox.addActionListener(new ComboBoxListener()); fillBox = new JComboBox(colorLabels); fillBox.setSelectedIndex(0); fillBox.setAlignmentX(Component.LEFT_ALIGNMENT); fillBox.addActionListener(new ComboBoxListener()); // 6. Add the comboboxes to another panel JPanel panel2 = new JPanel(); panel2.setLayout(new GridLayout(1, 4)); panel2.add(new JLabel("Arc Type: ", JLabel.RIGHT)); panel2.add(arcBox); panel2.add(new JLabel("Fill Type: ", JLabel.RIGHT)); panel2.add(fillBox); // 7. Add panel-2 to the container container.add(panel2, BorderLayout.SOUTH); // 8. Add the drawing canvas container.add(canvas); } // 9. Method to create sliders with necessary configuration public void setSlider(int min, int max, int init, int mjrTkSp, int mnrTkSp) { slider = new JSlider(JSlider.HORIZONTAL, min, max, init); slider.setPaintTicks(true); slider.setMajorTickSpacing(mjrTkSp); slider.setMinorTickSpacing(mnrTkSp); slider.setPaintLabels(true); slider.addChangeListener(new SliderListener()); } // 10. Definition of the drawing canvas class DrawingCanvas extends Canvas { Arc2D arc; double x,y, w,h, T0,T; // location, size, start angle, extent Color fillColor; int arcType; Rectangle2D boundingRec = null; // 11. Constructor public DrawingCanvas() { // Assign a size to the canvas setSize(350,350); // canvas width and height // Initialize the arc parameters for display when // the applet first showed up. x = getWidth()/4; y = getHeight()/4; w = getWidth()/2; h = getHeight()/2; T0 = 0; T = 135; // Start angle and extent arcType = Arc2D.OPEN; fillColor = Color.black; // Assign white color to the canvas setBackground(Color.white); } // 12. The overriding paint method public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; // 13. Create an arc using the parameters for location (x, y), // size (w, h), start angle and angular extent (T0, T), and // arc type such as open, pie, or chord. arc = new Arc2D.Double(x,y, w,h, T0,T, arcType); if (fillColor == Color.white || arcType == Arc2D.OPEN) { g2D.setColor(Color.black); g2D.draw(arc); } else { g2D.setColor(fillColor); g2D.fill(arc); } // 14. Display the bounding rectangle using highlight squares boundingRec = arc.getBounds2D(); drawHighlightSquares(g2D, boundingRec); } // 15. Method to draw highlight squares public void drawHighlightSquares(Graphics2D g2D, Rectangle2D r) { double x = r.getX(); double y = r.getY(); double w = r.getWidth(); double h = r.getHeight(); g2D.setColor(Color.black); g2D.fill(new Rectangle.Double(x-3.0, y-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w*0.5-3.0, y-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w-3.0, y-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x-3.0, y+h*0.5-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w-3.0, y+h*0.5-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x-3.0, y+h-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w*0.5-3.0, y+h-3.0, 6.0, 6.0)); g2D.fill(new Rectangle.Double(x+w-3.0, y+h-3.0, 6.0, 6.0)); } } // 16. Define the combo box listener to draw the selected arc type or // to fill the selected color. class ComboBoxListener implements ActionListener { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox) e.getSource(); if(cb == arcBox) { canvas.arcType = arcTypes[cb.getSelectedIndex()]; } else if (cb == fillBox) { canvas.fillColor = colors[cb.getSelectedIndex()]; } // Update the canvas with the new settings canvas.repaint(); } } // 17. Define the slider listener to alter different parameters // of the arc class SliderListener implements ChangeListener { public void stateChanged(ChangeEvent e) { JSlider slider = (JSlider) e.getSource(); if (slider == sliderX) canvas.x = slider.getValue(); else if (slider == sliderY) canvas.y = slider.getValue(); else if (slider == sliderW) canvas.w = slider.getValue(); else if (slider == sliderH) canvas.h = slider.getValue(); else if (slider == sliderT0) canvas.T0 = slider.getValue(); else if (slider == sliderT) canvas.T = slider.getValue(); // Update the drawing with new values of arc parameters canvas.repaint(); } } }