thorax232

Java Drawing Multiple Shapes (2 of 2)

Jul 27th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. @SuppressWarnings("serial")
  5. public class FigurePanel extends JPanel
  6. {
  7.     // Declare constants
  8.     public static final int LINE = 1;
  9.     public static final int RECTANGLE = 2;
  10.     public static final int ROUND_RECTANGLE = 3;
  11.     public static final int OVAL = 4;
  12.    
  13.     private int type = 1;
  14.     private boolean filled = false;
  15.    
  16.     // Construct a default FigurePanel
  17.     public FigurePanel()
  18.     {
  19.     }
  20.    
  21.     // Construct a FigurePanel with the specified type
  22.     public FigurePanel(int type)
  23.     {
  24.         this.type = type;
  25.     }
  26.    
  27.     // Construct a FigurePanel with the specified type and filled
  28.     public FigurePanel(int type, boolean filled)
  29.     {
  30.         this.type = type;
  31.         this.filled = filled;
  32.     }
  33.    
  34.     @Override // Draw a figure on the panel
  35.     protected void paintComponent(Graphics g)
  36.     {
  37.         super.paintComponent(g);
  38.        
  39.         // Get the appropriate size for the figure
  40.         int width = getWidth();
  41.         int height = getHeight();
  42.        
  43.         switch(type)
  44.         {
  45.             case LINE: // Display two cross lines
  46.             g.setColor(Color.black);
  47.             g.drawLine(10, 10, width - 10, height - 10);
  48.             g.drawLine(width - 10, 10, 10, height - 10);
  49.             break;
  50.            
  51.             case RECTANGLE: // Display a rectangle
  52.             g.setColor(Color.blue);
  53.             if(filled)
  54.                 g.fillRect((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height));
  55.             else
  56.                 g.drawRect((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height));
  57.             break;
  58.            
  59.             case ROUND_RECTANGLE: // Display a round-cornered rectangle
  60.             g.setColor(Color.red);
  61.             if(filled)
  62.                 g.fillRoundRect((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height), 20, 20);
  63.             else
  64.                 g.drawRoundRect((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height), 20, 20);
  65.             break;
  66.            
  67.             case OVAL: // Display an oval
  68.             g.setColor(Color.black);
  69.             if(filled)
  70.                 g.fillOval((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height));
  71.             else
  72.                 g.drawOval((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height));
  73.             break;
  74.         }
  75.     }
  76.    
  77.     // Set a new figure type
  78.     public void setType(int type)
  79.     {
  80.         this.type = type;
  81.         repaint();
  82.     }
  83.    
  84.     // Return figure type
  85.     public int getType()
  86.     {
  87.         return type;
  88.     }
  89.    
  90.     // Set a new filled property
  91.     public void setFilled(boolean filled)
  92.     {
  93.         this.filled = filled;
  94.         repaint();
  95.     }
  96.    
  97.     // Check if the figure is filled
  98.     public boolean isFilled()
  99.     {
  100.         return filled;
  101.     }
  102.    
  103.     @Override // Specify preferred size
  104.     public Dimension getPreferredSize()
  105.     {
  106.         return new Dimension(80, 80);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment