[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

01/04/2009 at 04:05AM PST, ID: 24023462
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

Java Applet to display shapes

Asked by HRMorton in Java Programming Language

Hi,
I have a Java Applet that display's 2 buttons, if 1st is pressed an additional box is displayed for input of a value. From this a circle is drawn.  If the other button is pressed 2 additional boxes are shown for input and from this a rectangle is drawn.

I have 2 problems with it, if I press the button nothing happens until I re-size the applet then the buttons are shown. Almost like it's waiting for something to happen before showing them.

If I draw the circle, then press the second button I get errors :-
Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For input string: ""
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Integer.parseInt(Integer.java:468)
        at java.lang.Integer.valueOf(Integer.java:553)
        at Assignment2.pkgshapes.paint(pkgshapes.java:160)
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
        at sun.awt.RepaintArea.paint(RepaintArea.java:224)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:301)
        at java.awt.Component.dispatchEventImpl(Component.java:4486)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4240)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

I've tried re-setting the string that will hold the input but nothing seems to be working.
Any help with these problems would be greatly appreciated.

Thanks
Heather
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
package Assignment2;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
public class pkgshapes extends java.applet.Applet implements ActionListener
{
    // Define Labels, Text Fields and Buttons to be used in Applet
    Label displayLabel, enterLabel, radiusLabel, lengthLabel, heightLabel ;
    Label areaCircleLabel, circumCircleLabel;
    TextField displayBox, enterBox, radiusBox, lengthBox, heightBox ;
    TextField areaCircleBox, circumCircleBox;
 
    Button drawCircle = new Button("Draw A Circle");
    Button drawRectangle = new Button("Draw A Rectangle");
    Button calculate = new Button("Calculate");
 
    // Define String's used to determine which button pressed
    String isCircle="N", isRectangle="N", isCal = "N", calCircle = "N", calRectangle = "N";
 
    String inputString;
 
    // Define variables to hold radius, length and height values
    // Define variables to hold caluclated values
    int radius, length, height, areaRectangle, premRectangle;
    double areaCircle, circumCircle;
    final double pi = 3.14;
 
/************************************************************************/
/**  Initialisation Function                                           **/
/************************************************************************/
    public void init() {
 
        // Add circle button with action listener
        add(drawCircle);
	drawCircle.addActionListener(this);
 
        // Add Rectangle button with action listener
        add(drawRectangle);
	drawRectangle.addActionListener(this);
 
        // Add Label for Radius Text Box
        enterLabel = new Label ( "Enter Radius" ) ;
        enterBox = new TextField (12 ) ;
 
        radiusLabel = new Label ( "Enter Radius of Circle" ) ;
        radiusLabel.setVisible(false);
        add ( radiusLabel ) ;
        
        radiusBox = new TextField ( 12 ) ;
        // Hide radius box
        radiusBox.setVisible(false);
        add ( radiusBox ) ;
        
        lengthLabel = new Label ( "Enter Length of Rectangle" ) ;
        lengthLabel.setVisible(false);
        add ( lengthLabel ) ;
 
        lengthBox = new TextField ( 12 ) ;
        lengthBox.setVisible(false);
        add ( lengthBox ) ;
 
        heightLabel = new Label ( "Enter Height of Rectangle" ) ;
        heightLabel.setVisible(false);
        add ( heightLabel ) ;
 
        heightBox = new TextField ( 12 ) ;
        heightBox.setVisible(false);
        add ( heightBox ) ;
 
        calculate.setVisible(false);
        add(calculate);
        calculate.addActionListener(this);
    }
    
/************************************************************************/
/**  Paint Function                                                    **/
/************************************************************************/
    public void paint(Graphics gscreen)
	{
                inputString = "";
                // Circle Button Pressed, display box to accept radius
                if( isCircle== "Y" ){
                    isCircle = "N";
                    calCircle = "Y";
 
                    // Display box to accept radius
                    radiusBox.setText ( "" ) ;
                    radiusLabel.setVisible(true);
                    radiusBox.setVisible(true);
                    radiusBox.requestFocus();
 
                    // Hide Length and height boxes 
                    lengthLabel.setVisible(false);
                    lengthBox.setVisible(false);
 
                    heightLabel.setVisible(false);
                    heightBox.setVisible(false);
 
                    // Display Calculate Button
                    calculate.setVisible(true);
                    
                    // Re-set value of Calculate Circle
                    calRectangle = "N";
 
                }
		else if (isRectangle == "Y"){
                    
                    lengthBox.setText ( "" ) ;
                    lengthLabel.setVisible(true);
                    lengthBox.setVisible(true);
 
                    heightBox.setText ( "" ) ;
                    heightLabel.setVisible(true);
                    heightBox.setVisible(true);
 
                    radiusLabel.setVisible(false);
                    radiusBox.setVisible(false);
 
                    lengthBox.requestFocus();
 
                    isRectangle = "N";
                    calRectangle = "Y";
                    // Re-set value of Calculate Circle
                    calCircle = "N";
                    calculate.setVisible(true);
                    }
                // Calculate Button Pressed
                else if (isCal == "Y" ){
 
                    // Calculate Dimensions and Draw a Circle
                    if (calCircle == "Y" ){
 
                        inputString = radiusBox.getText() ;
                        radius = Integer.valueOf( inputString ).intValue();
 
                        // Calculate the area of the Circle 3.14 * radius * radius
                        areaCircle = radius * radius * pi;
                        circumCircle = (radius * 2) * pi;
 
                        gscreen.drawString("Area of the Circle is " + areaCircle,80, radius + 130);
                        gscreen.drawString("Circumference of the Circle is " + circumCircle,80,radius + 150);
 
                        // Draw Circle with parameter given
        		gscreen.setColor(Color.red);
                        gscreen.drawOval(85,105,radius,radius);
                    }
                    // Calculate Dimensions and Draw a Rectangle
                    if (calRectangle == "Y" ){
                        inputString = lengthBox.getText() ;
                        length = Integer.valueOf( inputString ).intValue();
 
                        inputString = heightBox.getText() ;
                        height = Integer.valueOf( inputString ).intValue();
 
                        // Calculate the area of the Rectangle length * breadth
                        areaRectangle = length * height;
                        premRectangle = 2 * ( length * height );
 
                        gscreen.drawString("Area of the Rectangle is " + areaRectangle,80, height + 120);
                        gscreen.drawString("Perimeter of the Rectangle is " + premRectangle,80,height + 130);
 
                        // Draw Rectangle with length and Height given
        		gscreen.setColor(Color.red);
                        gscreen.fillRect(85, 105, length,height);                    
                        }
                    }
                } 
/************************************************************************/
/**  Actioned Performed Function                                       **/
/************************************************************************/
    public void actionPerformed ( ActionEvent event )
    {
        // Deal with the button click event Find out which Button generated the event
        String arg = event.getActionCommand() ;
        if ( arg.equals( "Clear" ) ){
            // Re-set Boxes when clear button selected
            }
        // Clear not selected, Check if Pound or Euro selected
        else
        {
            try
            {
                //  Draw Circle button pressed
		if (event.getSource()==drawCircle){
                    isCircle = "Y";
                    isRectangle = "N";
                    repaint();
                    }
                //  Draw Rectangle button pressed
                if (event.getSource()==drawRectangle){
                    isCircle = "N";
                    isRectangle = "Y";
   		    repaint();
                    }
                //  Caluclate button pressed
		if (event.getSource() == calculate){
                    isCal = "Y";
   		    repaint();
                }
            }
            /**********************************************************************/
            /**  Value entered by user was not an integer                        **/
            /**********************************************************************/
            catch ( NumberFormatException entry ){
                showStatus ( "Error: Invalid Input - please enter a valid number" ) ;
                }
       }                 
    }
    
}
 
 
[+][-]01/04/09 06:13 AM, ID: 23289781

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/05/09 12:57 AM, ID: 23293836

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/05/09 01:08 AM, ID: 23293888

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Java Programming Language
Sign Up Now!
Solution Provided By: bluebelldiscovery
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20091111-EE-VQP-91 - Hierarchy / EE_QW_3_20080625