1. package intro;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. public class Rect extends JPanel {
  7.  
  8.     private static final long serialVersionUID = 1L;
  9.     private int myXStart = 20;
  10.     private int myYStart = 20;
  11.     private String myFill = "yes";
  12.     private int myWidth = 50;
  13.     private int myHeight = 50;
  14.     private String myForeColor = "Green";
  15.     private String myBackColor = "Blue";
  16.  
  17.     /* constructor */
  18.     public Rect() {
  19.         super();
  20.         initialize();
  21.     }
  22.  
  23.     /* methods */
  24.     private void initialize() {
  25.         this.setSize(300, 200);
  26.         this.setLayout(new GridBagLayout());
  27.     }
  28.  
  29.     public void paintComponent (Graphics g) {
  30.         super.paintComponent(g);
  31.        
  32.         if (myBackColor.equalsIgnoreCase("Green")) {
  33.             this.setBackground(Color.green);
  34.         }
  35.         else if (myBackColor.equalsIgnoreCase("Blue")) {
  36.             this.setBackground(Color.blue);
  37.         }
  38.         else if (myBackColor.equalsIgnoreCase("Yellow")) {
  39.             this.setBackground(Color.yellow);
  40.         }
  41.         if (myForeColor.equalsIgnoreCase("Green")) {
  42.             g.setColor(Color.green);
  43.         }
  44.         else if (myForeColor.equalsIgnoreCase("Blue")) {
  45.             g.setColor(Color.blue);
  46.         }
  47.         else if (myForeColor.equalsIgnoreCase("Yellow")) {
  48.             g.setColor(Color.yellow);
  49.         }
  50.         if (myFill.equalsIgnoreCase("Yes")) {
  51.             g.fillRect(myXStart, myYStart,myWidth,myHeight);
  52.         }
  53.         else {
  54.             g.drawRect(myXStart, myYStart,myWidth,myHeight);
  55.         }
  56.       }
  57.  
  58.       /* getter setter methods begin */
  59.       public int getMyXStart() {
  60.           return myXStart;
  61.       }
  62.  
  63.       public void setMyXStart(int myXStart) {
  64.           this.myXStart = myXStart;
  65.           repaint();
  66.       }
  67.  
  68.       public void setMyYStart(int myYStart) {
  69.           this.myYStart = myYStart;
  70.           repaint();
  71.       }
  72.  
  73.       public int getMyYStart() {
  74.           return myYStart;
  75.       }
  76.  
  77.       public void setMyFill(String myFill) {
  78.           this.myFill = myFill;
  79.           repaint();
  80.       }
  81.  
  82.       public String getMyFill() {
  83.           return myFill;
  84.       }
  85.  
  86.       public void setMyWidth(int myWidth) {
  87.           this.myWidth = myWidth;
  88.           repaint();
  89.       }
  90.  
  91.       public int getMyWidth() {
  92.           return myWidth;
  93.       }
  94.  
  95.       public void setMyHeight(int myHeight) {
  96.           this.myHeight = myHeight;
  97.           repaint();
  98.       }
  99.  
  100.       public int getMyHeight() {
  101.           return myHeight;
  102.       }
  103.  
  104.       public void setMyForeColor(String myForeColor) {
  105.           this.myForeColor = myForeColor;
  106.           repaint();
  107.       }
  108.  
  109.       public String getMyForeColor() {
  110.           return myForeColor;
  111.       }
  112.  
  113.       public void setMyBackColor(String myBackColor) {
  114.           this.myBackColor = myBackColor;
  115.           repaint();
  116.       }
  117.  
  118.       public String getMyBackColor() {
  119.           return myBackColor;
  120.       }
  121.       /* getter setter methods end */
  122.  
  123. }