Results 1 to 3 of 3
  1. #1
    pls3399 is offline Member
    Join Date
    Jan 2013
    Rep Power

    Default Applet will not draw correctly

    Hello all,

    I have the following program that, when the applet loads, all I get is a white screen with 4 textboxes. I have even tried commenting everything except for the background line out but it still wont draw the background. Could you please point me in the direction of the error?

    Java Code:
    package investmentanalysis;  
    import java.awt.Color;  
    import java.awt.Container;  
    import java.awt.FlowLayout;  
    import java.awt.Font;  
    import java.awt.Graphics;  
    import java.awt.event.ActionEvent;  
    import java.awt.event.ActionListener;  
    import java.awt.event.KeyEvent;  
    import javax.swing.JApplet;  
    import javax.swing.JLabel;  
    import javax.swing.JTextField;  
    public class InvestmentAnalysis extends JApplet implements ActionListener  
    {/*Create a header for the applet. You may use the same one displayed above -  
         * Investment Analysis. This header must use a different font and a different point size from the rest of your text components. Place appropriate  
         * directions in the applet window to inform the user what he/she must do to use the applet. Make sure each JTextField object for data entry has a 
         * descriptive label immediately to its left.*/  
        JLabel header = new JLabel("Investment Analysis");   
        JLabel header1 = new JLabel("Enter dollar amounts for your investment portfolio:");  
        JLabel risk1 = new JLabel("High Risk");  
        JLabel risk2 =  new JLabel("Medium Risk");  
        JLabel risk3 =  new JLabel("Low Risk");  
        JLabel risk4 =  new JLabel("No Risk");  
        JTextField hRisk = new JTextField(10);  
        JTextField mRisk = new JTextField(10);  
        JTextField lRisk = new JTextField(10);  
        JTextField nRisk = new JTextField(10);  
        JLabel header2 = new JLabel("Press ENTER from the last box");     
        Container con = getContentPane();  
        Font font1 = new Font("Arial", Font.ITALIC, 22);  
        float hRisk1;  
        float mRisk1;  
        float lRisk1;  
        float nRisk1;  
        String keyPressed;  
        public void init()  
          FlowLayout layout = new FlowLayout();  
        public void setLegend() /*Add a legend to the chart that identifies which color represents each of the four risk categories.  
         * One way to do this is to have the color of the descriptive label and the color of the dollar amount match the color  
         * of the pie slice it represents. This is the method used above. You may also have a separate legend in the applet window  
         * that has four rectangles. Each rectangle would be filled with a color from the pie chart. Add a label for each rectangle  
         * indicating whether the color it displays is for no risk, low risk, medium risk, or high risk. Your legend must make use of different  
         * colors as in these examples.*/  
            if("Enter".equals(keyPressed)) /*Do not display the legend or any part of the pie chart before the user hits 
             * the enter key from the fourth JTextField object the first time.*/  
            float hRiskText = Float.parseFloat(hRisk.getText());  
            float mRiskText = Float.parseFloat(mRisk.getText());  
            float lRiskText = Float.parseFloat(lRisk.getText());  
            float nRiskText = Float.parseFloat(nRisk.getText());  
            hRisk1 = hRiskText;  
            mRisk1 = mRiskText;  
            lRisk1 = lRiskText;  
            nRisk1 = nRiskText;  
        public void keyPressed(KeyEvent k)   
         if(k.getKeyCode() == KeyEvent.VK_ENTER)   
             keyPressed = "Enter";  
        public void paint(Graphics g)   
            JLabel invalid =  new JLabel("Please enter number greater than zero");  
            float Total;  
            if (hRisk1 < 0)  
            if (mRisk1 < 0)  
            if (lRisk1 < 0)  
            if (nRisk1 < 0)  
            Total = hRisk1 + mRisk1 + lRisk1 + nRisk1;  
            float hRiskPer;  
            float mRiskPer;  
            float lRiskPer;  
            float nRiskPer;  
            int degrees;  
            int centerAngle;  
            int x = 150, y = 50, w = 100, h = 100;  
       hRiskPer = (hRisk1 * 100.0f) / Total;  
       mRiskPer = (mRisk1 * 100.0f) / Total;  
       lRiskPer = (lRisk1 * 100.0f) / Total;  
       nRiskPer = (nRisk1 * 100.0f) / Total;  
       /* Each "slice" of the pie chart must be a different color. Make sure that all the  
        * pieces of the pie add up to a full pie (100% or 360 degrees of the circle). There should 
        * be no gaps between the pieces of the pie. You can determine the size of each piece of the pie by dividing the respective  
        * amount invested by the total amount invested. For example, $100 is invested in the high risk (red) 
        * category, $200 in the medium risk (green) category, $300 in the low risk (yellow) category, and $400 
        * in the no risk (blue) category for the chart above. The high risk piece of the pie can be calculated as 100  
        * divided by (100 + 200 + 300 + 400) or 100 / 1000 or 10%. Therefore the high risk (red) piece of the pie is 10% of the total pie.*/  
       centerAngle = 0;  
       degrees = (int)(hRiskPer * 360 / 100);  
       g.fillArc(x, y, w, h, centerAngle, degrees);  
       centerAngle = degrees;  
       degrees = (int)(mRiskPer * 360 / 100);  
       g.fillArc(x, y, w, h, centerAngle, degrees);  
       // Draw the Pie for Hum  
       centerAngle = centerAngle + degrees;  
       degrees = (int)(lRiskPer * 360 / 100);  
       g.fillArc(x, y, w, h, centerAngle, degrees);  
       centerAngle = centerAngle + degrees;  
       degrees = (int)(nRiskPer * 360 / 100);  
       g.fillArc(x, y, w, h, centerAngle, degrees);  
        public void actionPerformed(ActionEvent e) {  
            throw new UnsupportedOperationException("Not supported yet.");  
    /*Include the html for the applet tag as a comment in your source code. This html will be  
     * graded as part of the assignment. It also allows me make sure the Swing components and 
     * the pie chart don't overlap. 
    <applet code=InvestmentAnalysis.InvestmentAnalysis.class width=400 height=400> 

    Thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Applet will not draw correctly

    I get is a white screen with 4 textboxes.
    What do you expect to see?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Rep Power

    Default Re: Applet will not draw correctly

    your code get empty string on this line...

    Java Code:
    That's like your string keyPressed is null. So the process is not enter into the above condition
    Android developer at Trinay Technology Solutions,,5705750475

Similar Threads

  1. how to drage my card images draw on applet.
    By in forum Java Applets
    Replies: 35
    Last Post: 05-08-2012, 12:06 PM
  2. Applet not working correctly?
    By Beavotropper2 in forum Java Applets
    Replies: 2
    Last Post: 04-18-2011, 06:32 AM
  3. Replies: 7
    Last Post: 04-10-2010, 07:07 PM
  4. applet won't draw a cross
    By michcio in forum New To Java
    Replies: 1
    Last Post: 06-02-2009, 07:03 PM
  5. how to draw x-y graph in Jpanel.--not in APPLET.
    By in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 05:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts