thorax232

Java Drawing Multiple Shapes (1 of 2)

Jul 27th, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. /*
  2.  * The FigurePanel class in Listing 13.3 can display lines, rectangles, round-cornered rectangles and ovals. Add appropriate new code in the class to display arcs
  3.  * and polygons. Write a test program to display the shapes as shown in Figure 13.28a using the new FigurePanel class.
  4.  */
  5. import java.awt.*;
  6.  
  7. import javax.swing.*;
  8.  
  9. @SuppressWarnings("serial")
  10. public class ImproveFigurePanel extends JFrame
  11. {
  12.     public ImproveFigurePanel()
  13.     {
  14.         setLayout(new GridLayout(2, 5, 5, 5));
  15.         add(new FigurePanel(FigurePanel.LINE));
  16.         add(new FigurePanel(FigurePanel.RECTANGLE));
  17.         add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
  18.         add(new FigurePanel(FigurePanel.OVAL));
  19.         add(new FigurePanel(FigurePanel.RECTANGLE, true));
  20.         add(new FigurePanel(FigurePanel.ROUND_RECTANGLE, true));
  21.         add(new ArcsPanel());
  22.         add(new ArcsPanel(true));
  23.         add(new PolygonPanel());
  24.         add(new PolygonPanel(true));
  25.     }
  26.    
  27.     public static void main(String[] args)
  28.     {
  29.         ImproveFigurePanel frame = new ImproveFigurePanel();
  30.         frame.setSize(400, 200);
  31.         frame.setTitle("TestFigurePanel");
  32.         frame.setLocationRelativeTo(null);
  33.         frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  34.         frame.setVisible(true);
  35.     }
  36. }
  37.  
  38. @SuppressWarnings("serial")
  39. // ArcsPanel class
  40. class ArcsPanel extends JPanel
  41. {
  42.     private boolean filled;
  43.  
  44.     public ArcsPanel()
  45.     {
  46.     }
  47.  
  48.     public ArcsPanel(boolean filled)
  49.     {
  50.         this.filled = filled;
  51.     }
  52.  
  53.     @Override // Draw four blades of a fan
  54.     protected void paintComponent(Graphics g)
  55.     {
  56.         super.paintComponent(g);
  57.        
  58.         int xCenter = getWidth() / 2;
  59.         int yCenter = getHeight() / 2;
  60.         int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
  61.        
  62.         int x = xCenter - radius;
  63.         int y = yCenter - radius;
  64.         g.setColor(Color.green);
  65.  
  66.         if(filled)
  67.         {
  68.             g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
  69.             g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
  70.             g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
  71.             g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30);
  72.         }
  73.         else
  74.         {
  75.             g.drawArc(x, y, 2 * radius, 2 * radius, 0, 30);
  76.             g.drawArc(x, y, 2 * radius, 2 * radius, 90, 30);
  77.             g.drawArc(x, y, 2 * radius, 2 * radius, 180, 30);
  78.             g.drawArc(x, y, 2 * radius, 2 * radius, 270, 30);
  79.         }
  80.     }
  81. }
  82.  
  83. @SuppressWarnings("serial")
  84. // PolygonPanel class
  85. class PolygonPanel extends JPanel
  86. {
  87.     private boolean filled;
  88.    
  89.     public PolygonPanel()
  90.     {
  91.     }
  92.    
  93.     public PolygonPanel(boolean filled)
  94.     {
  95.         this.filled = filled;
  96.     }
  97.    
  98.     protected void paintComponent(Graphics g)
  99.     {
  100.         super.paintComponent(g);
  101.        
  102.         int xCenter = getWidth() / 2;
  103.         int yCenter = getHeight() / 2;
  104.         int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
  105.         g.setColor(Color.yellow);
  106.        
  107.         // Create new polygon object
  108.         Polygon polygon = new Polygon();
  109.        
  110.         // Add points to the polygon in this order
  111.         polygon.addPoint(xCenter + radius, yCenter);
  112.         polygon.addPoint((int)(xCenter + radius *
  113.                 Math.cos(2 * Math.PI / 6)), (int)(yCenter - radius *
  114.                 Math.sin(2 * Math.PI / 6)));
  115.         polygon.addPoint((int)(xCenter + radius *
  116.                 Math.cos(2 * 2 * Math.PI / 6)), (int)(yCenter - radius *
  117.                 Math.sin(2 * 2 * Math.PI / 6)));
  118.         polygon.addPoint((int)(xCenter + radius *
  119.                 Math.cos(3 * 2 * Math.PI / 6)), (int)(yCenter - radius *
  120.                 Math.sin(3 * 2 * Math.PI / 6)));
  121.         polygon.addPoint((int)(xCenter + radius *
  122.                 Math.cos(4 * 2 * Math.PI / 6)), (int)(yCenter - radius *
  123.                 Math.sin(4 * 2 * Math.PI / 6)));
  124.         polygon.addPoint((int)(xCenter + radius *
  125.                 Math.cos(5 * 2 * Math.PI / 6)), (int)(yCenter - radius *
  126.                 Math.sin(5 * 2 * Math.PI / 6)));
  127.                
  128.         // Draw the polygon
  129.         if(filled)
  130.             g.fillPolygon(polygon);
  131.         else
  132.             g.drawPolygon(polygon);
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment