Advertisement
Guest User

DrawFigure.java

a guest
Feb 26th, 2022
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. public class DrawFigure extends JPanel {
  2.     int type;
  3.     int length, width, height, radius;
  4.    
  5.     public DrawFigure() {
  6.         super();
  7.         type = 5;
  8.     }
  9.    
  10.     public DrawFigure(int myType, int myWidth, int myLength, int myHeight) { // Box and Rectangle
  11.         super();
  12.         type = myType;
  13.         length = myLength;
  14.         width = myWidth;
  15.         height = myHeight;
  16.     }
  17.    
  18.     public DrawFigure(int x, int y, int myType, int myWidth, int myLength, int myHeight) {
  19.         super();
  20.         type = myType;
  21.         length = myLength;
  22.         width = myWidth;
  23.         height = myHeight;
  24.     }
  25.  
  26.    
  27.     public DrawFigure(int myType, int myRadius, int myHeight) {
  28.         super();
  29.         type = myType;
  30.         radius = myRadius;
  31.         height = myHeight;
  32.     }
  33.    
  34.     public void paintComponent(Graphics g) {
  35.         super.paintComponent(g);
  36.        
  37.         if (type == 1) { // Draw Rectangle
  38.             g.drawRect(50,  110,  width,  length);
  39.            
  40.         } else if (type == 2) { // Draw Box
  41.             g.draw3DRect(50, 110, length, width, true);
  42.            
  43.             /*
  44.             g.setColor(Color.GREEN);
  45.             g.fillRect(50, 110, width, height);
  46.            
  47.             g.setColor(Color.BLACK);
  48.             g.drawLine(50, 110, length, height);
  49.             */
  50.             return;
  51.            
  52.         } else if(type == 3) { // Draw Circle
  53.             // You will need one draw command to make a circle
  54.             g.setColor(Color.RED);
  55.             g.drawOval(50, 110, radius, radius);
  56.             return;
  57.            
  58.         } else if(type == 4) { // Draw Cylinder
  59.             g.setColor(Color.BLACK);
  60.             g.drawOval(135,  65, radius, radius - radius / 2);
  61.            
  62.             // Base
  63.             g.setColor(Color.RED);
  64.             g.fillOval(135, 65 + height, radius, radius / 2);
  65.            
  66.             g.setColor(Color.BLACK);
  67.             g.drawLine(135, 65 + height + (height /4), 135, 135);
  68.            
  69.             g.setColor(Color.BLACK);
  70.             g.drawLine(135 + radius, 65 + height + (height /4), 135 + radius, 135);
  71.             return;
  72.            
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement