By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

Newbie question - Drawing objects in applet

P: 1
Hi,
I'm pretty new to java, and I have a small problem involving drawing a rectangle on a java applet.Firstly this is not a plea for someone to help me with this peice of work, I just need pointing in the right direction.

Ok the problem.

I am creating a program that ask the user to input a height value, the program will then do a calculation and create a golden ratio width. The type of both the height and the width are double.

This is where the problem starts. The next thing I need to do is make the program draw the rectangle using the user inputted height and the calculated width.

I have a feeling that the problem has occured because I haven't converted the type double into a int type.
Here is my code

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.applet.Applet;
  5. import java.awt.Graphics;
  6.  
  7. /**
  8.  * Class Test - write a description of the class here
  9.  * 
  10.  * @author (your name) 
  11.  * @version (a version number)
  12.  */
  13. public class Test extends JApplet
  14. {
  15.     // instance variables - replace the example below with your own
  16.    double sum;
  17.    int height;
  18.    int width;
  19.  
  20.      /**
  21.      * Called by the browser or applet viewer to inform this JApplet that it
  22.      * has been loaded into the system. It is always called before the first 
  23.      * time that the start method is called.
  24.      */
  25.     public void init()
  26.     {
  27.         String firstNumber;
  28.         double number1, sum1;
  29.  
  30.         firstNumber = JOptionPane.showInputDialog("Please enter the height" );
  31.  
  32.  
  33.         number1 = Double.parseDouble( firstNumber );
  34.  
  35.  
  36.         sum1 = (Math.sqrt(5) + 1) / 2;
  37.         sum = number1 * sum1;
  38.  
  39.         int height = (int)number1;
  40.         int width = (int)sum;
  41.  
  42.  
  43.  
  44.  
  45.         // this is a workaround for a security conflict with some browsers
  46.         // including some versions of Netscape & Internet Explorer which do 
  47.         // not allow access to the AWT system event queue which JApplets do 
  48.         // on startup to check access. May not be necessary with your browser. 
  49.         JRootPane rootPane = this.getRootPane();    
  50.         rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
  51.  
  52.         // provide any initialisation necessary for your JApplet
  53.     }
  54.  
  55.     /**
  56.      * Called by the browser or applet viewer to inform this JApplet that it 
  57.      * should start its execution. It is called after the init method and 
  58.      * each time the JApplet is revisited in a Web page. 
  59.      */
  60.     public void start()
  61.     {
  62.         // provide any code requred to run each time 
  63.         // web page is visited
  64.     }
  65.  
  66.     /** 
  67.      * Called by the browser or applet viewer to inform this JApplet that
  68.      * it should stop its execution. It is called when the Web page that
  69.      * contains this JApplet has been replaced by another page, and also
  70.      * just before the JApplet is to be destroyed. 
  71.      */
  72.     public void stop()
  73.     {
  74.         // provide any code that needs to be run when page
  75.         // is replaced by another page or before JApplet is destroyed 
  76.     }
  77.  
  78.     /**
  79.      * Paint method for applet.
  80.      * 
  81.      * @param  g   the Graphics object for this applet
  82.      */
  83.     public void paint(Graphics g)
  84.     {
  85.         // simple text displayed on applet
  86.         g.drawRect( 15, 10, 270, 20 );
  87.         g.drawString("The sum is " + sum, 25, 25 );
  88.         g.drawRect( 100, 100, height, width);
  89.  
  90.  
  91.     }
  92.  
  93.     /**
  94.      * Called by the browser or applet viewer to inform this JApplet that it
  95.      * is being reclaimed and that it should destroy any resources that it
  96.      * has allocated. The stop method will always be called before destroy. 
  97.      */
  98.     public void destroy()
  99.     {
  100.         // provide code to be run when JApplet is about to be destroyed.
  101.     }
  102.  
  103.  
  104.     /**
  105.      * Returns information about this applet. 
  106.      * An applet should override this method to return a String containing 
  107.      * information about the author, version, and copyright of the JApplet.
  108.      *
  109.      * @return a String representation of information about this JApplet
  110.      */
  111.     public String getAppletInfo()
  112.     {
  113.         // provide information about the applet
  114.         return "Title:   \nAuthor:   \nA simple applet example description. ";
  115.     }
  116.  
  117.  
  118.     /**
  119.      * Returns parameter information about this JApplet. 
  120.      * Returns information about the parameters than are understood by this JApplet.
  121.      * An applet should override this method to return an array of Strings 
  122.      * describing these parameters. 
  123.      * Each element of the array should be a set of three Strings containing 
  124.      * the name, the type, and a description.
  125.      *
  126.      * @return a String[] representation of parameter information about this JApplet
  127.      */
  128.     public String[][] getParameterInfo()
  129.     {
  130.         // provide parameter information about the applet
  131.         String paramInfo[][] = {
  132.                  {"firstParameter",    "1-10",    "description of first parameter"},
  133.                  {"status", "boolean", "description of second parameter"},
  134.                  {"images",   "url",     "description of third parameter"}
  135.         };
  136.         return paramInfo;
  137.     }
  138. }
  139.  
  140.  
  141.  
Thanks for any suggestions.
Feb 17 '08 #1
Share this Question
Share on Google+
1 Reply


Nepomuk
Expert 2.5K+
P: 3,112
...This is where the problem starts. The next thing I need to do is make the program draw the rectangle using the user inputted height and the calculated width.

I have a feeling that the problem has occured because I haven't converted the type double into a int type...
First of all, welcome to TSDN!
About your problem: What DOES happen? And if you think, the problem has happened, because of the missing conversion, have you tried converting it?

Greetings,
Nepomuk
Feb 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.