473,568 Members | 2,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Newbie question - Drawing objects in applet

1 New Member
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
1 2370
Nepomuk
3,112 Recognized Expert Specialist
...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

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

Similar topics

15
12986
by: Remon Huijts | last post by:
Hi there, For a very specific service, I have created a PHP script that uses the GD library functions to create a PNG image from a few lines of XML data describing a simple diagram/drawing. It's cool and works, writing the XML data in a text editor is straightforward for me because I designed the format. I have no idea however how I can...
2
3790
by: timtt | last post by:
does anyone guideline to speed up drawing in awt applet?? i have thousands of g.drawstring() + d.drawline() actually, does Swing applet draw much faster than awt applet??
0
1615
by: Hermund ?rdalen | last post by:
Hi! We're developing some children's pages for one of our customers. Already we've found some nice crossword and puzzle programs/applets. What we really need now is a drawing program. It must be possible to specify a start image and it must be possible to store the drawing once you're through. We tried aDraw but we couldn't get past a...
0
2751
by: Tomi Holger Engdahl | last post by:
I am looking for a solution to add on-line drawing tool to a phpBB discussion board. The idea would be that the users can draw their own simple drawings with the tool and attach them easily as images in the discussion. The idea is to easily add small drawings to the postings. The drawign tool should be easy to use and learn. It should...
2
12305
by: Champika Nirosh | last post by:
Hi, I want to create drawing board application that can draw Line, rectagle, circle and free hand drawing. Each drawing need to be transparent, moveable (draggable), have bring to front and send to back feature etc. How can I do this .. where should I start?
5
2084
by: barbaros | last post by:
Hello everybody, I need to put some dynamic drawings on my web page. More precisely, I need to draw a number of geometric figures (circles, rectangles) which evolve into a graphics windows according to some law (a little bit like the solar system). I need also to have several fields aside the window, where the visitor can change values for...
1
1378
by: lonertic | last post by:
Hi, I'm new to the forum, I'm hoping someone can help me with this because days of googling and lots of coffee hasn't so far..maybe I'm not using the right keywords to search.. I don't know.. I'm writing a .EXE integration program with VB, the program contains an IE Browser ActiveX control which navigates to a html page onload (which...
5
2018
by: cozsmin | last post by:
Hy all, i have aproblem, i made a simple applet that works fine at a first glance, so i just wanted to add some drawing on it like : class Ap extends java.applet.Applet { java.awt.Graphics g; public void start() {
0
1687
by: Madmartigan | last post by:
Hi I'm a newbie to C# and have been instructed to create a Hangman game in SharpDevelop. I don't want the answer to the full code, just some help along the way. I have included my code thus far and at this stage would like to know how I can get the RandomWordManager, which I found on another site, to display a newly generated word as the...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.