Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Drawing shapes

Posted on 2011-10-06
4
Medium Priority
?
321 Views
Last Modified: 2012-05-12
I have the following code typed from a book to test drawing shapes. But, there are some errors (red underlined) at:

Line       code
4           public class FigurePanel extend JPanel {
31         super.printComponent(g);
34         int width=getWidth();
35         int height=getHeight();
75         repaint();
86         repaint();

I don't usderstand why I am getting these errors?




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

public class FigurePanel extend 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
Comment
Question by:Mike Eghtebas
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
evedder earned 2000 total points
ID: 36928627
your class declaration is wrong you wrote extend, extends is correct

public class FigurePanel extends JPanel{

...
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36928658
yes that took care of it.

However, I am having some errors with the test class below.


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.setVisible(true);
    }

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36928674
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

was also missing, I added it shows the panel but non of the shapes.
0
 
LVL 3

Expert Comment

by:evedder
ID: 36928746
yup there's another error in FigurePanel, in this method

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

it should be

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

that will do
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question