Arc demonstration: scale, move, rotate, sheer : Shape « 2D Graphics GUI « Java






Arc demonstration: scale, move, rotate, sheer

Arc demonstration: scale, move, rotate, sheer
      
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ArcApp extends JPanel {
  MyCanvas canvas;

  JComboBox arcBox, fillBox;

  JSlider sliderX, sliderY, sliderWidth, sliderHeight, sliderT0, sliderT;

  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 ArcApp() {
    super(new BorderLayout());
    canvas = new MyCanvas();
        int width = 600;
        int height = 55;
    sliderX = setSlider(0, width, width / 4, width / 2, width / 4);
    sliderY = setSlider(0, height, height / 4,
        height / 2, height / 4);
    sliderWidth = setSlider(0, width, width / 2, width / 2, width / 4);
    sliderHeight = setSlider(0, height, height / 2,
        height / 2, height / 4);
    sliderT0 = setSlider(0, 360, 45, 180, 45);
    sliderT = setSlider(0, 360, 135, 180, 45);

    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(sliderWidth);
    panel1.add(sliderHeight);
    panel1.add(new JLabel("Angles (Th0, Th): ", JLabel.RIGHT));
    panel1.add(sliderT0);
    panel1.add(sliderT);

    add(panel1, BorderLayout.NORTH);

    arcBox = new JComboBox(arcLabels);
    arcBox.setSelectedIndex(0);
    arcBox.setAlignmentX(Component.LEFT_ALIGNMENT);
    arcBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox) e.getSource();
        canvas.arcType = arcTypes[cb.getSelectedIndex()];
        canvas.repaint();
      }
    });

    fillBox = new JComboBox(colorLabels);
    fillBox.setSelectedIndex(0);
    fillBox.setAlignmentX(Component.LEFT_ALIGNMENT);
    fillBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox) e.getSource();
        canvas.fillColor = colors[cb.getSelectedIndex()];
        canvas.repaint();
      }
    });

    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);

    add(panel2, BorderLayout.SOUTH);
    add(canvas,BorderLayout.CENTER);
  }

  public JSlider setSlider(int min, int max, int init, int mjrTkSp,
      int mnrTkSp) {
    JSlider slider = new JSlider(JSlider.HORIZONTAL, min, max, init);
    slider.setPaintTicks(true);
    slider.setMajorTickSpacing(mjrTkSp);
    slider.setMinorTickSpacing(mnrTkSp);
    slider.setPaintLabels(true);
    slider.addChangeListener(new SliderListener());
    return slider;
  }

  class MyCanvas extends JLabel {
    Arc2D arc;

    double x, y, w, h, startAngle, extent; 

    Color fillColor;

    int arcType;

    Rectangle2D boundingRec = null;

    public MyCanvas() {
      x = 700 / 4;
      y = 550 / 4;
      w = 600 / 2;
      h = 550 / 2;
      startAngle = 0;
      extent = 135; 
      arcType = Arc2D.OPEN;
      fillColor = Color.black;
      setBackground(Color.white);
    }

    public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2D) g;
      g2D.setColor(Color.white);
      g2D.fill(new Rectangle(getBounds()));
      
      arc = new Arc2D.Double(x, y, w, h, startAngle, extent, arcType);
      if (fillColor == Color.white || arcType == Arc2D.OPEN) {
        g2D.setColor(Color.black);
        g2D.draw(arc);
      } else {
        g2D.setColor(fillColor);
        g2D.fill(arc);
      }

      boundingRec = arc.getBounds2D();
      drawHighlightSquares(g2D, boundingRec);
    }

    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));
    }
  }

  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 == sliderWidth)
        canvas.w = slider.getValue();
      else if (slider == sliderHeight)
        canvas.h = slider.getValue();
      else if (slider == sliderT0)
        canvas.startAngle = slider.getValue();
      else if (slider == sliderT)
        canvas.extent = slider.getValue();
      canvas.revalidate();
      canvas.repaint();
    }
  }
  public static void main (String[] a){
    JFrame f = new JFrame();
    f.getContentPane().add(new ArcApp());
    f.setDefaultCloseOperation(1);
    f.setSize(700, 550);
    f.setVisible(true);  
  }
}

           
         
    
    
    
    
    
  








Related examples in the same category

1.Creating Basic Shapes
2.fillRect (int, int, int, int) method draws a solid rectangle
3.Creating a Shape Using Lines and Curves
4.Combining Shapes
5.Draw rectangles, use the drawRect() method. To fill rectangles, use the fillRect() method
6.Draw lineDraw line
7.Draw a PolygonDraw a Polygon
8.Draw an oval outline
9.Draw a (Round)rectangleDraw a (Round)rectangle
10.Fill a polygonFill a polygon
11.Fill a solid oval
12.Fill a (Round)rectangleFill a (Round)rectangle
13.Change fontChange font
14.Draw rectangle 2Draw rectangle 2
15.Draw ArcDraw Arc
16.Draw EllipseDraw Ellipse
17.Fill a Rectangle 2Fill a Rectangle 2
18.Fill Arc 2Fill Arc 2
19.Draw textDraw text
20.Draw unicode string Draw unicode string
21.Shape combineShape combine
22.EffectsEffects
23.Mouse drag and drop to drawMouse drag and drop to draw
24.Hypnosis SpiralHypnosis Spiral
25.GlyphVector.getNumGlyphs()
26.Resize a shape
27.Rectangle with rounded corners drawn using Java 2D Graphics API
28.Compares two ellipses and returns true if they are equal or both null.
29.Compares two lines are returns true if they are equal or both null.
30.Creates a diagonal cross shape.
31.Creates a diamond shape.
32.Creates a new Stroke-Object for the given type and with.
33.Creates a region surrounding a line segment by 'widening' the line segment.
34.Creates a triangle shape that points downwards.
35.Creates a triangle shape that points upwards.
36.Generate Polygon
37.Polygon with float coordinates.
38.Polyline 2D
39.Serialises a Shape object.
40.Tests two polygons for equality. If both are null this method returns true.
41.Union two rectangles
42.Calculate Intersection Clip
43.Draws a shape with the specified rotation about (x, y).
44.Checks, whether the given rectangle1 fully contains rectangle 2 (even if rectangle 2 has a height or width of zero!).
45.Reads a Point2D object that has been serialised by the writePoint2D(Point2D, ObjectOutputStream)} method.
46.Returns a point based on (x, y) but constrained to be within the bounds of a given rectangle.
47.RectListManager is a class to manage a list of rectangular regions.
48.Fill Rectangle2D.Double and Ellipse2D.DoubleFill Rectangle2D.Double and Ellipse2D.Double
49.This program demonstrates the various 2D shapesThis program demonstrates the various 2D shapes