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

    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;  
          
          
        @Override  
        public void init()  
      {  
          FlowLayout layout = new FlowLayout();  
          con.setLayout(layout);  
          con.setBackground(Color.GRAY);  
          header.setFont(font1);  
          con.add(header);  
          con.add(header1);  
          con.add(risk1);  
          con.add(hRisk);  
          con.add(risk2);  
          con.add(mRisk);  
          con.add(risk3);  
          con.add(lRisk);  
          con.add(risk4);  
          con.add(nRisk);        
          con.add(header2);  
            
      }  
          
        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());  
            hRisk.setBackground(Color.RED);  
            mRisk.setBackground(Color.GREEN);  
            lRisk.setBackground(Color.YELLOW);  
            nRisk.setBackground(Color.BLUE);  
              
            hRisk1 = hRiskText;  
            mRisk1 = mRiskText;  
            lRisk1 = lRiskText;  
            nRisk1 = nRiskText;  
            }  
              
              
        }  
          
        public void keyPressed(KeyEvent k)   
        {  
         if(k.getKeyCode() == KeyEvent.VK_ENTER)   
         {  
             keyPressed = "Enter";  
         }  
        }  
      
          
        @Override  
        public void paint(Graphics g)   
        {  
            JLabel invalid =  new JLabel("Please enter number greater than zero");  
            float Total;  
                      
            setLegend();  
                      
            if (hRisk1 < 0)  
            {  
                con.add(invalid);  
            }  
            else  
            {  
                con.remove(invalid);  
            }  
              
            if (mRisk1 < 0)  
            {  
                con.add(invalid);  
            }  
            else  
            {  
                con.remove(invalid);  
            }  
              
            if (lRisk1 < 0)  
            {  
                con.add(invalid);  
            }  
            else  
            {  
                con.remove(invalid);  
            }  
              
            if (nRisk1 < 0)  
            {  
                con.add(invalid);  
            }  
            else  
            {  
                con.remove(invalid);  
            }  
              
            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.setColor(Color.RED);  
       g.fillArc(x, y, w, h, centerAngle, degrees);  
         
      
       centerAngle = degrees;  
       degrees = (int)(mRiskPer * 360 / 100);  
         
       g.setColor(Color.GREEN);  
       g.fillArc(x, y, w, h, centerAngle, degrees);  
         
       // Draw the Pie for Hum  
       centerAngle = centerAngle + degrees;  
       degrees = (int)(lRiskPer * 360 / 100);  
         
       g.setColor(Color.YELLOW);  
       g.fillArc(x, y, w, h, centerAngle, degrees);  
         
       centerAngle = centerAngle + degrees;  
       degrees = (int)(nRiskPer * 360 / 100);  
         
       g.setColor(Color.BLUE);  
       g.fillArc(x, y, w, h, centerAngle, degrees);  
        }  
      
        @Override  
        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. 
       
      
    /*<html> 
        <body> 
     
    <applet code=InvestmentAnalysis.InvestmentAnalysis.class width=400 height=400> 
    </applet> 
     
        </body> 
         
    </html>*/

    Thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    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
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Applet will not draw correctly

    your code get empty string on this line...

    Java Code:
    if("Enter".equals(keyPressed))
    That's like your string keyPressed is null. So the process is not enter into the above condition
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

Similar Threads

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