473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Newbie question - Drawing objects in applet

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 2357
Nepomuk
3,112 Expert 2GB
...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
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...
2
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
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...
0
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...
2
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...
5
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...
1
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.. ...
5
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 { ...
0
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.