Code debug

Using FigurePanel, TestFigurePanel is supposed to draw some shapes on a panel.

Question: Could you please help me to debug it?

Thank you.
import javax.swing.*;
import java.awt.*;

public class TestFigurePanel extends JFrame{
    public TestFigurePanel(){
        setLayout(new GridLayout(2,3,5,5));
        add(new FigurePanel(FigurePanel.LINE));
        add(new FigurePanel(FigurePanel.RECTANGLE));
        add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
        add(new FigurePanel(FigurePanel.OVAL));
        add(new FigurePanel(FigurePanel.RECTANGLE,true));
        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);
    }
}


import java.awt.*;
import javax.swing.*;

public 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

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:

remove this:
   super.printComponent(g);

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
Besides with Swing you should getContenPane form the root window and
add all elements to ContentPane
like here:
  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));

Open in new window

0
for_yanCommented:
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0
for_yanCommented:
You are always welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.