Hi,

This application does everything but show the circles and rectangles even though visible is set to true.

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

public class ShapesTest {


public static void main (String []args){
  String input = JOptionPane.showInputDialog
          ("Enter 1 to draw rectangles\n" +
           "Enter 2 to draw ovals" );

int choice = Integer.parseInt( input );

Shapes panel = new Shapes( choice );

JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
application.add( panel );
application.setSize( 500, 500 );
application.setVisible( true );

}
}

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

public class Shapes extends JPanel {
private int choice;
public Shapes(int UserChoice)
{
}

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

    for (int i = 0; i < 10; i++)
    {switch(choice)
{
                case 1:g.drawRect(10 + i * 10 , 10 + i * 10,50 + i * 10,50 + i * 10);
                break;
                case 2:g.drawOval(10 + i * 10 , 10 + i * 10, 50 +i * 10,50 + i * 10);
}               break;

}
}
}

Recommended Answers

All 8 Replies

int choice = Integer.parseInt( input );
This line of your code has an error can you tell what it is?

There is one other problem in the code paintComponent is never called so you will never draw anything on the screen.

hope this helps. : )

Isn't the paint component called in the super.paintComponent line? It is the seventh line from the bottom.

As for what is wrong with the other line I am not sure.

Thanks for looking and the advice.

lynnajoe

You have a problem in your constructor. You are sending a parameter but you are not using the parameter to set the value of the private variable (private int choice).
I added this one line to your code and it ran just fine.

//inside you constructor
public Shapes(int UserChoice)
{
  choice = UserChoice;
}

just as a rule of convention you normally don't capitalize the first letter of a normal variable. To follow this convention try:

public Shapes(int userChoice)
{
  choice = userChoice;
}

I didn't have a problem with any of the other stuff just that one line.

Mattox is right.

Hi,

I did as you said and it is showing up now. The only problem is that there is only one rectangle and one oval. There should be 10 for each. I tried a few different things but I still get only one rectangle or one oval. Do you have any ideas why this is so?

lynnajoe

Your break statement below case 2, should be inside the braces.
It should look like:

for(int i = 0; i < 10; i++)
{
switch(choice)
{
case 1:g.drawRect(10 + i * 10 , 10 + i * 10,50 + i * 10,50 + i * 10);
break;
case 2:g.drawOval(10 + i * 10 , 10 + i * 10, 50 +i * 10,50 + i * 10);
break;
}//end switch 

}//End loop

I forgot I fixed that part. By having it outside the braces it causes the program to break from the for loop.

Hi Mattox,

It works perfectly. I want to thank you so much. You're instructions were easy to understand and it absolutely fixed the problem.

Thanks again,

lynnajoe

Hi again,

Just so everyone knows this code came from my textbook by Deitel and Deitel.
I do not want people under the impression that I was passing it off as my own. This is the way that I learn things. I study the codes that people write and incorporate the concepts into my own. It helps familiarize me with the language of Java and rules for syntax.

Here's the credit:
Java: How to Program, Sixth Edition by H.M. Deitel and P.J. Deitel. Published by Prentice Hall. Copyright © 2005 by Pearson Education, Inc.

Thank you,

lynnajoe

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.