[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Code revision...

Class TestFigurePanel below, opens three panels.

A. As it is they are 2x3, 2x3, and 2x3 grids.
   I want to change them to 2x2, 3x3, and 1x1 grids.

B. 2x2 to have DashedOval and Hexagon on the first row and
                      Arc and filled Hexagon on the second row.

C. 2x3 to have Line, Oval, and blank on the first row and
                      Oval, blank, and Oval on the second row.
                      Line, Line, and Line on the third row.

D. 1x1 Grid to have an Octagon.

The class FigurePanel below doesn't have DashedOval, Hexagon, and Octagon. We need to add them to this class.

If there are too many to do, I guess leave C out because I think I can make this part by learning the other parts you possibly will do.

Thank you.
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

public class TestFigurePanel extends JFrame  {
    public TestFigurePanel(ArrayList<Integer> select){
        Container c = this.getContentPane();

        c.setLayout(new GridLayout(2,3,5,5));
        JPanel [] pps = new JPanel[6];
        for(int j=0; j<6; j++)pps[j] = new JPanel();
        if(select.contains(0))c.add(new FigurePanel(FigurePanel.LINE));
        else   c.add(pps[0]);
        if(select.contains(1)) c.add(new FigurePanel(FigurePanel.RECTANGLE));
              else   c.add(pps[1]);
         if(select.contains(2))c.add(new FigurePanel

(FigurePanel.ROUND_RECTANGLE));
         else   c.add(pps[2]);
        if(select.contains(3)) c.add(new FigurePanel(FigurePanel.OVAL));
         else   c.add(pps[3]);
        if(select.contains(4)) c.add(new FigurePanel

(FigurePanel.RECTANGLE,true));
         else   c.add(pps[4]);
        if(select.contains(5))c.add(new FigurePanel

(FigurePanel.ROUND_RECTANGLE,true));
         else   c.add(pps[5]);
      
    }
   
    public static void main(String[] args){

        ArrayList <Integer>a1 = new ArrayList<Integer>();
          ArrayList <Integer>a2 = new ArrayList<Integer>();
          ArrayList <Integer>a3 = new ArrayList<Integer>();
        a1.add(0);
        a1.add(3);
        a1.add(5);

           a2.add(1);
        a2.add(2);
        a2.add(3);


           a3.add(0);

        a3.add(2);

            

        TestFigurePanel frame1=new TestFigurePanel(a1);
        TestFigurePanel frame2=new TestFigurePanel(a2);
        TestFigurePanel frame3=new TestFigurePanel(a3);



        frame1.setSize(400,200);
        frame2.setSize(400,200);
        frame3.setSize(400,200);

        frame1.setTitle("Panel 1");
        frame2.setTitle("Panel 2");
        frame3.setTitle("Panel 3");

        frame1.setLocation(20,300);
        frame2.setLocation(440,300);
        frame3.setLocation(860,300);

       // frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame1.setVisible(true);
        frame2.setVisible(true);
        frame3.setVisible(true);

    }
}


  class FigurePanel extends JPanel {

    public static final int LINE=1;
    public static final int RECTANGLE=2;
    public static final int ROUND_RECTANGLE=3;
    public static final int OVAL=4;

    private int type=1;
    private boolean filled = false;


    public FigurePanel(){
    }


    public FigurePanel(int type){
        this.type=type;
    }


    public FigurePanel(int type, boolean filled){
        this.type=type;
        this.filled=filled;
    }


    protected void paintComponent(Graphics g){
//        super.printComponent(g);


        int width=getWidth();
        int height=getHeight();

        switch(type){
            case LINE:
                g.setColor(Color.BLACK);
                g.drawLine(10, 10, width -10, height-10);
                g.drawLine(width-10, 10, 10, height-10);
                break;
            case RECTANGLE:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;
            case ROUND_RECTANGLE:
                g.setColor(Color.RED);
                if(filled)
                    g.fillRoundRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height), 20, 20);
                else
                  g.drawRoundRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),20,20);
                break;
            case OVAL:
                g.setColor(Color.BLACK);
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
        }
    }


    public void setType(int type){
        this.type=type;
        repaint();
    }


    public int getType(){
        return type;
    }


    public void setFilled(boolean filled){
        this.filled=filled;
        repaint();
    }


    public boolean isfilled() {
        return filled;
    }


    public Dimension getPreferredSize() {
        return new Dimension(80,80);
    }
}

Open in new window

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 3
  • 2
1 Solution
 
for_yanCommented:

OK, this is it.

Below id the old code commented out

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

public class TestFigurePanel extends JFrame  {
    public TestFigurePanel(int k){
        Container c = this.getContentPane();
        if(k == 0){



        c.setLayout(new GridLayout(2,2,5,5));
        JPanel [] pps = new JPanel[4];
        for(int j=0; j<4; j++)pps[j] = new JPanel();


        c.add(new FigurePanel(FigurePanel.DASHED_OVAL));
         c.add(new FigurePanel(FigurePanel.HEXAGON));
          c.add(new FigurePanel(FigurePanel.ARC));
             c.add(new FigurePanel(FigurePanel.HEXAGON, true));
            

        }  else if (k == 1){

         c.setLayout(new GridLayout(3,3,5,5));
        JPanel [] pps = new JPanel[9];
        for(int j=0; j<9; j++)pps[j] = new JPanel();


        c.add(new FigurePanel(FigurePanel.LINE));
         c.add(new FigurePanel(FigurePanel.OVAL));
          c.add(pps[2]);
             c.add(new FigurePanel(FigurePanel.OVAL));
              c.add(pps[4]);
         c.add(new FigurePanel(FigurePanel.LINE));
         c.add(new FigurePanel(FigurePanel.LINE));
          c.add(new FigurePanel(FigurePanel.LINE));



        }   else if (k==2 ){

           c.setLayout(new GridLayout(1,1,5,5));
        JPanel [] pps = new JPanel[1];
        for(int j=0; j<1; j++)pps[j] = new JPanel();


        c.add(new FigurePanel(FigurePanel.OCTAGON));




        }

    }

    public static void main(String[] args){

        ArrayList <Integer>a1 = new ArrayList<Integer>();
          ArrayList <Integer>a2 = new ArrayList<Integer>();
          ArrayList <Integer>a3 = new ArrayList<Integer>();
        a1.add(0);
        a1.add(3);
        a1.add(5);

           a2.add(1);
        a2.add(2);
        a2.add(3);


           a3.add(0);

        a3.add(2);



        TestFigurePanel frame1=new TestFigurePanel(0);
        TestFigurePanel frame2=new TestFigurePanel(1);
        TestFigurePanel frame3=new TestFigurePanel(2);



        frame1.setSize(400,200);
        frame2.setSize(400,200);
        frame3.setSize(400,200);

        frame1.setTitle("Panel 1");
        frame2.setTitle("Panel 2");
        frame3.setTitle("Panel 3");

        frame1.setLocation(20,300);
        frame2.setLocation(440,300);
        frame3.setLocation(860,300);

       // frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame1.setVisible(true);
        frame2.setVisible(true);
        frame3.setVisible(true);

    }
}


  class FigurePanel extends JPanel {

       float fl[] = { 12.0f };
   BasicStroke basicStroke = new BasicStroke(2.0f,
   BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 12.0f,
      fl, 0.0f);

    public static final int LINE=1;
    public static final int RECTANGLE=2;
    public static final int ROUND_RECTANGLE=3;
    public static final int OVAL=4;
     public static final int DASHED_OVAL=5;
        public static final int HEXAGON=6;
           public static final int OCTAGON=7;
          public static final int ARC=8;

    private int type=1;
    private boolean filled = false;


    public FigurePanel(){
    }


    public FigurePanel(int type){
        this.type=type;
    }


    public FigurePanel(int type, boolean filled){
        this.type=type;
        this.filled=filled;
    }


    protected void paintComponent(Graphics g){
//        super.printComponent(g);


        int width=getWidth();
        int height=getHeight();

        switch(type){
            case LINE:
                g.setColor(Color.BLACK);
                g.drawLine(10, 10, width -10, height-10);
                g.drawLine(width-10, 10, 10, height-10);
                break;
            case RECTANGLE:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;
            case ROUND_RECTANGLE:
                g.setColor(Color.RED);
                if(filled)
                    g.fillRoundRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height), 20, 20);
                else
                  g.drawRoundRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),20,20);
                break;
            case OVAL:
                g.setColor(Color.BLACK);
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;

                       case DASHED_OVAL:
                g.setColor(Color.BLACK);
           Graphics2D g2d = (Graphics2D) g;
                    if(filled)
                    g2d.fill(new Ellipse2D.Double((0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height)));
    g2d.setPaint(Color.gray);
    g2d.setStroke(basicStroke);
    g2d.draw(new Ellipse2D.Double((0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height)));
                        break;
                   case HEXAGON:
                       

    Polygon p = new Polygon();
    for (int i = 0; i < 6; i++)
      p.addPoint((int) ((0.5*width) + (0.2*width) * Math.cos(i * 2 * Math.PI / 6)),
          (int) ((0.5*height)+ (0.2*width) * Math.sin(i * 2 * Math.PI / 6)));
         if(filled)g.fillPolygon(p);
    g.drawPolygon(p);
                   break;

                      case OCTAGON:


    p = new Polygon();
    for (int i = 0; i < 8; i++)
      p.addPoint((int) ((0.5*width) + (0.2*width) * Math.cos(i * 2 * Math.PI / 8)),
          (int) ((0.5*height)+ (0.2*width) * Math.sin(i * 2 * Math.PI / 8)));
         if(filled)g.fillPolygon(p);
    g.drawPolygon(p);
                   break;

                             case ARC:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillArc((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height),0,120);
                else
                  g.drawArc((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),0,120);
                break;

                      // break;


                       /*
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                           */


        }
        
    }


    public void setType(int type){
        this.type=type;
        repaint();
    }


    public int getType(){
        return type;
    }


    public void setFilled(boolean filled){
        this.filled=filled;
        repaint();
    }


    public boolean isfilled() {
        return filled;
    }


    public Dimension getPreferredSize() {
        return new Dimension(80,80);
    }
}



/*
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

public class TestFigurePanel extends JFrame *//*implements WindowListener*//* {
    public TestFigurePanel(ArrayList<Integer> select){
        Container c = this.getContentPane();

        c.setLayout(new GridLayout(2,3,5,5));
        JPanel [] pps = new JPanel[6];
        for(int j=0; j<6; j++)pps[j] = new JPanel();
        if(select.contains(0))c.add(new FigurePanel(FigurePanel.LINE));
        else   c.add(pps[0]);
        if(select.contains(1)) c.add(new FigurePanel(FigurePanel.RECTANGLE));
              else   c.add(pps[1]);
         if(select.contains(2))c.add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
         else   c.add(pps[2]);
        if(select.contains(3)) c.add(new FigurePanel(FigurePanel.OVAL));
         else   c.add(pps[3]);
        if(select.contains(4)) c.add(new FigurePanel(FigurePanel.RECTANGLE,true));
         else   c.add(pps[4]);
        if(select.contains(5))c.add(new FigurePanel(FigurePanel.ROUND_RECTANGLE,true));
         else   c.add(pps[5]);
      //  this.addWindowListener(this);
    }
      *//*
  public void windowDeiconified(WindowEvent we){}
      public void windowIconified(WindowEvent we){}
      public void windowClosing(WindowEvent we){
        //  Object w = we.getSource();
        //  ((JFrame)w).dispose();

      }
      public void windowClosed(WindowEvent we){}
      public void windowActivated(WindowEvent we){}
      public void windowDeactivated(WindowEvent we){}
      public void windowOpened(WindowEvent we){}
      *//*


    public static void main(String[] args){

        ArrayList <Integer>a1 = new ArrayList<Integer>();
          ArrayList <Integer>a2 = new ArrayList<Integer>();
          ArrayList <Integer>a3 = new ArrayList<Integer>();
        a1.add(0);
        a1.add(3);
        a1.add(5);

           a2.add(1);
        a2.add(2);
        a2.add(3);


           a3.add(0);

        a3.add(2);

            

        TestFigurePanel frame1=new TestFigurePanel(a1);
        TestFigurePanel frame2=new TestFigurePanel(a2);
        TestFigurePanel frame3=new TestFigurePanel(a3);



        frame1.setSize(400,200);
        frame2.setSize(400,200);
        frame3.setSize(400,200);

        frame1.setTitle("Panel 1");
        frame2.setTitle("Panel 2");
        frame3.setTitle("Panel 3");

        frame1.setLocation(20,300);
        frame2.setLocation(440,300);
        frame3.setLocation(860,300);

       // frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame1.setVisible(true);
        frame2.setVisible(true);
        frame3.setVisible(true);

    }
}


  class FigurePanel extends JPanel {

    public static final int LINE=1;
    public static final int RECTANGLE=2;
    public static final int ROUND_RECTANGLE=3;
    public static final int OVAL=4;

    private int type=1;
    private boolean filled = false;


    public FigurePanel(){
    }


    public FigurePanel(int type){
        this.type=type;
    }


    public FigurePanel(int type, boolean filled){
        this.type=type;
        this.filled=filled;
    }


    protected void paintComponent(Graphics g){
//        super.printComponent(g);


        int width=getWidth();
        int height=getHeight();

        switch(type){
            case LINE:
                g.setColor(Color.BLACK);
                g.drawLine(10, 10, width -10, height-10);
                g.drawLine(width-10, 10, 10, height-10);
                break;
            case RECTANGLE:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;
            case ROUND_RECTANGLE:
                g.setColor(Color.RED);
                if(filled)
                    g.fillRoundRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height), 20, 20);
                else
                  g.drawRoundRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),20,20);
                break;
            case OVAL:
                g.setColor(Color.BLACK);
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
        }
    }


    public void setType(int type){
        this.type=type;
        repaint();
    }


    public int getType(){
        return type;
    }


    public void setFilled(boolean filled){
        this.filled=filled;
        repaint();
    }


    public boolean isfilled() {
        return filled;
    }


    public Dimension getPreferredSize() {
        return new Dimension(80,80);
    }
}
*//*
import javax.swing.*;
import java.awt.*;

public class TestFigurePanel extends JFrame{
    public TestFigurePanel(){
        Container c = this.getContentPane();

        c.setLayout(new GridLayout(2,3,5,5));
        c.add(new FigurePanel(FigurePanel.LINE));
        c.add(new FigurePanel(FigurePanel.RECTANGLE));
        c.add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
        c.add(new FigurePanel(FigurePanel.OVAL));
        c.add(new FigurePanel(FigurePanel.RECTANGLE,true));
        c.add(new FigurePanel(FigurePanel.ROUND_RECTANGLE,true));
    }

    public static void main(String[] args){
        TestFigurePanel frame=new TestFigurePanel();
        frame.setSize(400,200);
        frame.setTitle("TestFigurePanel");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}



 class FigurePanel extends JPanel {

    public static final int LINE=1;
    public static final int RECTANGLE=2;
    public static final int ROUND_RECTANGLE=3;
    public static final int OVAL=4;

    private int type=1;
    private boolean filled = false;


    public FigurePanel(){
    }


    public FigurePanel(int type){
        this.type=type;
    }


    public FigurePanel(int type, boolean filled){
        this.type=type;
        this.filled=filled;
    }


    protected void paintComponent(Graphics g){
       // super.printComponent(g);


        int width=getWidth();
        int height=getHeight();

        switch(type){
            case LINE:
                g.setColor(Color.BLACK);
                g.drawLine(10, 10, width -10, height-10);
                g.drawLine(width-10, 10, 10, height-10);
                break;
            case RECTANGLE:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;
            case ROUND_RECTANGLE:
                g.setColor(Color.RED);
                if(filled)
                    g.fillRoundRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height), 20, 20);
                else
                  g.drawRoundRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),20,20);
                break;
            case OVAL:
                g.setColor(Color.BLACK);
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
        }
    }


    public void setType(int type){
        this.type=type;
        repaint();
    }


    public int getType(){
        return type;
    }


    public void setFilled(boolean filled){
        this.filled=filled;
        repaint();
    }


    public boolean isfilled() {
        return filled;
    }


    public Dimension getPreferredSize() {
        return new Dimension(80,80);
    }
}
*/

Open in new window

0
 
for_yanCommented:

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

public class TestFigurePanel extends JFrame  {
    public TestFigurePanel(int k){
        Container c = this.getContentPane();
        if(k == 0){



        c.setLayout(new GridLayout(2,2,5,5));
        JPanel [] pps = new JPanel[4];
        for(int j=0; j<4; j++)pps[j] = new JPanel();


        c.add(new FigurePanel(FigurePanel.DASHED_OVAL));
         c.add(new FigurePanel(FigurePanel.HEXAGON));
          c.add(new FigurePanel(FigurePanel.ARC));
             c.add(new FigurePanel(FigurePanel.HEXAGON, true));
            

        }  else if (k == 1){

         c.setLayout(new GridLayout(3,3,5,5));
        JPanel [] pps = new JPanel[9];
        for(int j=0; j<9; j++)pps[j] = new JPanel();


        c.add(new FigurePanel(FigurePanel.LINE));
         c.add(new FigurePanel(FigurePanel.OVAL));
          c.add(pps[2]);
             c.add(new FigurePanel(FigurePanel.OVAL));
              c.add(pps[4]);
         c.add(new FigurePanel(FigurePanel.LINE));
         c.add(new FigurePanel(FigurePanel.LINE));
          c.add(new FigurePanel(FigurePanel.LINE));
            c.add(new FigurePanel(FigurePanel.LINE));  



        }   else if (k==2 ){

           c.setLayout(new GridLayout(1,1,5,5));
        JPanel [] pps = new JPanel[1];
        for(int j=0; j<1; j++)pps[j] = new JPanel();


        c.add(new FigurePanel(FigurePanel.OCTAGON));




        }

    }

    public static void main(String[] args){

        ArrayList <Integer>a1 = new ArrayList<Integer>();
          ArrayList <Integer>a2 = new ArrayList<Integer>();
          ArrayList <Integer>a3 = new ArrayList<Integer>();
        a1.add(0);
        a1.add(3);
        a1.add(5);

           a2.add(1);
        a2.add(2);
        a2.add(3);


           a3.add(0);

        a3.add(2);



        TestFigurePanel frame1=new TestFigurePanel(0);
        TestFigurePanel frame2=new TestFigurePanel(1);
        TestFigurePanel frame3=new TestFigurePanel(2);



        frame1.setSize(400,200);
        frame2.setSize(400,200);
        frame3.setSize(400,200);

        frame1.setTitle("Panel 1");
        frame2.setTitle("Panel 2");
        frame3.setTitle("Panel 3");

        frame1.setLocation(20,300);
        frame2.setLocation(440,300);
        frame3.setLocation(860,300);

       // frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame1.setVisible(true);
        frame2.setVisible(true);
        frame3.setVisible(true);

    }
}


  class FigurePanel extends JPanel {

       float fl[] = { 12.0f };
   BasicStroke basicStroke = new BasicStroke(2.0f,
   BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 12.0f,
      fl, 0.0f);

    public static final int LINE=1;
    public static final int RECTANGLE=2;
    public static final int ROUND_RECTANGLE=3;
    public static final int OVAL=4;
     public static final int DASHED_OVAL=5;
        public static final int HEXAGON=6;
           public static final int OCTAGON=7;
          public static final int ARC=8;

    private int type=1;
    private boolean filled = false;


    public FigurePanel(){
    }


    public FigurePanel(int type){
        this.type=type;
    }


    public FigurePanel(int type, boolean filled){
        this.type=type;
        this.filled=filled;
    }


    protected void paintComponent(Graphics g){
//        super.printComponent(g);


        int width=getWidth();
        int height=getHeight();

        switch(type){
            case LINE:
                g.setColor(Color.BLACK);
                g.drawLine(10, 10, width -10, height-10);
                g.drawLine(width-10, 10, 10, height-10);
                break;
            case RECTANGLE:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;
            case ROUND_RECTANGLE:
                g.setColor(Color.RED);
                if(filled)
                    g.fillRoundRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height), 20, 20);
                else
                  g.drawRoundRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),20,20);
                break;
            case OVAL:
                g.setColor(Color.BLACK);
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;

                       case DASHED_OVAL:
                g.setColor(Color.BLACK);
           Graphics2D g2d = (Graphics2D) g;
                    if(filled)
                    g2d.fill(new Ellipse2D.Double((0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height)));
    g2d.setPaint(Color.gray);
    g2d.setStroke(basicStroke);
    g2d.draw(new Ellipse2D.Double((0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height)));
                        break;
                   case HEXAGON:
                       

    Polygon p = new Polygon();
    for (int i = 0; i < 6; i++)
      p.addPoint((int) ((0.5*width) + (0.2*width) * Math.cos(i * 2 * Math.PI / 6)),
          (int) ((0.5*height)+ (0.2*width) * Math.sin(i * 2 * Math.PI / 6)));
         if(filled)g.fillPolygon(p);
    g.drawPolygon(p);
                   break;

                      case OCTAGON:


    p = new Polygon();
    for (int i = 0; i < 8; i++)
      p.addPoint((int) ((0.5*width) + (0.2*width) * Math.cos(i * 2 * Math.PI / 8)),
          (int) ((0.5*height)+ (0.2*width) * Math.sin(i * 2 * Math.PI / 8)));
         if(filled)g.fillPolygon(p);
    g.drawPolygon(p);
                   break;

                             case ARC:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillArc((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height),0,120);
                else
                  g.drawArc((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),0,120);
                break;

                      // break;


                       /*
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                           */


        }
        
    }


    public void setType(int type){
        this.type=type;
        repaint();
    }


    public int getType(){
        return type;
    }


    public void setFilled(boolean filled){
        this.filled=filled;
        repaint();
    }


    public boolean isfilled() {
        return filled;
    }


    public Dimension getPreferredSize() {
        return new Dimension(80,80);
    }
}



/*
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

public class TestFigurePanel extends JFrame *//*implements WindowListener*//* {
    public TestFigurePanel(ArrayList<Integer> select){
        Container c = this.getContentPane();

        c.setLayout(new GridLayout(2,3,5,5));
        JPanel [] pps = new JPanel[6];
        for(int j=0; j<6; j++)pps[j] = new JPanel();
        if(select.contains(0))c.add(new FigurePanel(FigurePanel.LINE));
        else   c.add(pps[0]);
        if(select.contains(1)) c.add(new FigurePanel(FigurePanel.RECTANGLE));
              else   c.add(pps[1]);
         if(select.contains(2))c.add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
         else   c.add(pps[2]);
        if(select.contains(3)) c.add(new FigurePanel(FigurePanel.OVAL));
         else   c.add(pps[3]);
        if(select.contains(4)) c.add(new FigurePanel(FigurePanel.RECTANGLE,true));
         else   c.add(pps[4]);
        if(select.contains(5))c.add(new FigurePanel(FigurePanel.ROUND_RECTANGLE,true));
         else   c.add(pps[5]);
      //  this.addWindowListener(this);
    }
      *//*
  public void windowDeiconified(WindowEvent we){}
      public void windowIconified(WindowEvent we){}
      public void windowClosing(WindowEvent we){
        //  Object w = we.getSource();
        //  ((JFrame)w).dispose();

      }
      public void windowClosed(WindowEvent we){}
      public void windowActivated(WindowEvent we){}
      public void windowDeactivated(WindowEvent we){}
      public void windowOpened(WindowEvent we){}
      *//*


    public static void main(String[] args){

        ArrayList <Integer>a1 = new ArrayList<Integer>();
          ArrayList <Integer>a2 = new ArrayList<Integer>();
          ArrayList <Integer>a3 = new ArrayList<Integer>();
        a1.add(0);
        a1.add(3);
        a1.add(5);

           a2.add(1);
        a2.add(2);
        a2.add(3);


           a3.add(0);

        a3.add(2);

            

        TestFigurePanel frame1=new TestFigurePanel(a1);
        TestFigurePanel frame2=new TestFigurePanel(a2);
        TestFigurePanel frame3=new TestFigurePanel(a3);



        frame1.setSize(400,200);
        frame2.setSize(400,200);
        frame3.setSize(400,200);

        frame1.setTitle("Panel 1");
        frame2.setTitle("Panel 2");
        frame3.setTitle("Panel 3");

        frame1.setLocation(20,300);
        frame2.setLocation(440,300);
        frame3.setLocation(860,300);

       // frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame1.setVisible(true);
        frame2.setVisible(true);
        frame3.setVisible(true);

    }
}


  class FigurePanel extends JPanel {

    public static final int LINE=1;
    public static final int RECTANGLE=2;
    public static final int ROUND_RECTANGLE=3;
    public static final int OVAL=4;

    private int type=1;
    private boolean filled = false;


    public FigurePanel(){
    }


    public FigurePanel(int type){
        this.type=type;
    }


    public FigurePanel(int type, boolean filled){
        this.type=type;
        this.filled=filled;
    }


    protected void paintComponent(Graphics g){
//        super.printComponent(g);


        int width=getWidth();
        int height=getHeight();

        switch(type){
            case LINE:
                g.setColor(Color.BLACK);
                g.drawLine(10, 10, width -10, height-10);
                g.drawLine(width-10, 10, 10, height-10);
                break;
            case RECTANGLE:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;
            case ROUND_RECTANGLE:
                g.setColor(Color.RED);
                if(filled)
                    g.fillRoundRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height), 20, 20);
                else
                  g.drawRoundRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),20,20);
                break;
            case OVAL:
                g.setColor(Color.BLACK);
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
        }
    }


    public void setType(int type){
        this.type=type;
        repaint();
    }


    public int getType(){
        return type;
    }


    public void setFilled(boolean filled){
        this.filled=filled;
        repaint();
    }


    public boolean isfilled() {
        return filled;
    }


    public Dimension getPreferredSize() {
        return new Dimension(80,80);
    }
}
*//*
import javax.swing.*;
import java.awt.*;

public class TestFigurePanel extends JFrame{
    public TestFigurePanel(){
        Container c = this.getContentPane();

        c.setLayout(new GridLayout(2,3,5,5));
        c.add(new FigurePanel(FigurePanel.LINE));
        c.add(new FigurePanel(FigurePanel.RECTANGLE));
        c.add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
        c.add(new FigurePanel(FigurePanel.OVAL));
        c.add(new FigurePanel(FigurePanel.RECTANGLE,true));
        c.add(new FigurePanel(FigurePanel.ROUND_RECTANGLE,true));
    }

    public static void main(String[] args){
        TestFigurePanel frame=new TestFigurePanel();
        frame.setSize(400,200);
        frame.setTitle("TestFigurePanel");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}



 class FigurePanel extends JPanel {

    public static final int LINE=1;
    public static final int RECTANGLE=2;
    public static final int ROUND_RECTANGLE=3;
    public static final int OVAL=4;

    private int type=1;
    private boolean filled = false;


    public FigurePanel(){
    }


    public FigurePanel(int type){
        this.type=type;
    }


    public FigurePanel(int type, boolean filled){
        this.type=type;
        this.filled=filled;
    }


    protected void paintComponent(Graphics g){
       // super.printComponent(g);


        int width=getWidth();
        int height=getHeight();

        switch(type){
            case LINE:
                g.setColor(Color.BLACK);
                g.drawLine(10, 10, width -10, height-10);
                g.drawLine(width-10, 10, 10, height-10);
                break;
            case RECTANGLE:
                g.setColor(Color.BLUE);
                if(filled)
                    g.fillRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
                break;
            case ROUND_RECTANGLE:
                g.setColor(Color.RED);
                if(filled)
                    g.fillRoundRect((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height), 20, 20);
                else
                  g.drawRoundRect((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height),20,20);
                break;
            case OVAL:
                g.setColor(Color.BLACK);
                if(filled)
                    g.fillOval((int)(0.1*width), (int)(0.1*height),
                      (int)(0.8*width), (int)(0.8*height));
                else
                  g.drawOval((int)(0.1*width),(int)(0.1*height),
                    (int)(0.8*width),(int)(0.8*height));
        }
    }


    public void setType(int type){
        this.type=type;
        repaint();
    }


    public int getType(){
        return type;
    }


    public void setFilled(boolean filled){
        this.filled=filled;
        repaint();
    }


    public boolean isfilled() {
        return filled;
    }


    public Dimension getPreferredSize() {
        return new Dimension(80,80);
    }
}
*/

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi for_yan,

There is a followup question on mouse x, y earlier at:

http://www.experts-exchange.com/Programming/Languages/Java/Q_27386533.html

I will be checking your solution on this thread.

Thank you
0
 
for_yanCommented:
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now