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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » ShapeScreenshots 
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(03604518045);
    sliderT = setSlider(036013518045);

    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridLayout(33));
    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 = (JComboBoxe.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 = (JComboBoxe.getSource();
        canvas.fillColor = colors[cb.getSelectedIndex()];
        canvas.repaint();
      }
    });

    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridLayout(14));
    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 = (Graphics2Dg;
      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.06.06.0));
      g2D
          .fill(new Rectangle.Double(x + w * 0.5 3.0, y - 3.06.0,
              6.0));
      g2D.fill(new Rectangle.Double(x + w - 3.0, y - 3.06.06.0));
      g2D
          .fill(new Rectangle.Double(x - 3.0, y + h * 0.5 3.06.0,
              6.0));
      g2D.fill(new Rectangle.Double(x + w - 3.0, y + h * 0.5 3.06.0,
          6.0));
      g2D.fill(new Rectangle.Double(x - 3.0, y + h - 3.06.06.0));
      g2D.fill(new Rectangle.Double(x + w * 0.5 3.0, y + h - 3.06.0,
          6.0));
      g2D.fill(new Rectangle.Double(x + w - 3.0, y + h - 3.06.06.0));
    }
  }

  class SliderListener implements ChangeListener {
    public void stateChanged(ChangeEvent e) {
      JSlider slider = (JSlidere.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(700550);
    f.setVisible(true);  
  }
}

           
       
Related examples in the same category
1. fillRect (int, int, int, int) method draws a solid rectangle
2. Draw lineDraw line
3. Draw a PolygonDraw a Polygon
4. Draw an oval outline
5. Draw a (Round)rectangleDraw a (Round)rectangle
6. Fill a polygonFill a polygon
7. Fill a solid oval
8. Fill a (Round)rectangleFill a (Round)rectangle
9. Change fontChange font
10. Draw rectangle 2Draw rectangle 2
11. Draw ArcDraw Arc
12. Draw EllipseDraw Ellipse
13. Fill a Rectangle 2Fill a Rectangle 2
14. Fill Arc 2Fill Arc 2
15. Draw textDraw text
16. Draw unicode string Draw unicode string
17. Shape combineShape combine
18. EffectsEffects
19. Mouse drag and drop to drawMouse drag and drop to draw
20. Hypnosis SpiralHypnosis Spiral
21. Shape Test Shape Test
www__.__j__a_v__a2___s._co___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.