create account

Drawing Methods in Java Applet (JApplet) Using Eclipse IDE: Drawing a Snowman by aromatheraphy

View this thread on: hive.blogpeakd.comecency.com
· @aromatheraphy · (edited)
$31.06
Drawing Methods in Java Applet (JApplet) Using Eclipse IDE: Drawing a Snowman
#### What Will I Learn?
In this tutorial;

- You will learn what Java Applet is. 
- You will learn how to create a basic Java Applet.
- You will learn how to draw something in Java Applets with paint methods.
- You will learn the Graphichs class and its methods.
#### Requirements

-Java SE Development Kit (Not necessarily the last version) is required.
-Eclipse IDE or any similar Interated Development Enviroment (IDE) that is designed for Java programming language is required.

#### Difficulty
This program contains the basic parts of graphics class and  Java Applets;

- Basic

#### Tutorial Contents
The purpose of this tutorial is drawing a snowman in a Java Applet. The below figure contains the image of the code of the program (JApplet_Drawing_Snowman_Demo.java).

![Code.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516302510/u1omxzwrrbt7znumqqog.jpg)

##### Java Applet (JApplet)
Two kinds of Java programs exist. These are Java Applets and Java applications. This Java program is a Java applet, not a Java application. The Java programs that are needed to be emmbedded into an HTML document, transported across a network, and executed using a web browser are the Java Applets. To create Java Applet in java programming languge firstly we need to import javax.swing.JApplet package with the following command;

	import javax.swing.JApplet;

The other important thing is that the class that is going to be used as Java program needs to be extends the JApplet class because of the object oriented concept of inheritance. Therefore while defining the class of this program we used the following command;

	public class JApplet_Drawing_Snowman_Demo extends JApplet {

##### Paint Method
There are many applet methods exist. One of them is the paint method. The paint method uses the Graphics objets in its constructor.  In this program, we have one graphics object. Therefore, we used the following code to define the main method of the program.

		public void paint(Graphics Snowman_Page) {

##### The Graphics Class
The Graphics objects allows us to draw many type of shapes by using some methods such as drawOval and  fillOval,  In this program we are going to use the following Graphics methods;

To fill rectangle defined by (x,y) upper left corner, witdth and lenght;

			Snowman_Page.fillRect(x, y, width, height);

To draw a line from (x1,y1) to (x2,y2);

			Snowman_Page.drawLine(x1, y1, x2, y2);

To fill oval bouded rectange defined by (x,y) upper left corner, witdth and lenght;

			Snowman_Page.fillOval(x, y, width, height);

To draw an arc along oval bounded by the rectangle defined by x, y, width and height and the arc starts from startAngle and goes distance defined by arcAngle; ,

			Snowman_Page.drawArc(x, y, width, height, startAngle, arcAngle);


Note that (0,0) points indicates the upper left corner of the Java Applet page.

Before using these methods, you can define their color with following code;

			Snowman_Page.setColor(Color);


##### Drawing Background
In this program firstly we need to draw the background of the page. Since the dimentions of our graphic is (300,225). Therefore with following command we can paint the backgound to cyan.

			Snowman_Page.setColor(Color.CYAN);
			Snowman_Page.fillRect(0, 0, 300, 225);
##### Drawing Ground
To have dark gray ground with width 300 and height 50 and (0.175) upper left corner, use the following code.

			Snowman_Page.setColor(Color.DARK_GRAY);
			Snowman_Page.fillRect(0, 175, 300, 50);
##### Drawing Sun
We can use oval for sun. To paint the quarter portion of the sun with 80 as width and 80 as height the upper left corner of the rectengle should be (-40,-40). The we will only see the quarter portion of the oval.

			Snowman_Page.setColor(Color.ORANGE);
			Snowman_Page.fillOval(-40, -40, 80, 80);
##### Drawing Head
The head should be circle (Oval with equal height and width) and  its upper left corner should be at the half of the height in x axes and  at the top of snowman in y axes.

			Snowman_Page.setColor(Color.white);
			Snowman_Page.fillOval(middle-20, top, 40, 40);

##### Drawing Upper Torso
Upper torso should be bigger  and lower than head. Draw the upper torso with the folowing code.

			Snowman_Page.fillOval(middle-35, top+35, 70, 50);

Note that, we choose the top of the snowman as 50 and middle of the snowman as 150.
##### Drawing Lower Torso
Lower torso should be bigger  and lower than the upper torso. Draw the lower torso with the folowing code.

			Snowman_Page.fillOval(middle-50, top+80, 100, 60);

##### Drawing Eyes and Smile
To have black eyes and smile set color to black;

			Snowman_Page.setColor(Color.BLACK);

Then, paint two symmetrical eyes on the head;

			Snowman_Page.fillOval(middle-10, top+10, 5, 5);
			Snowman_Page.fillOval(middle+5, top+10, 5, 5);

Then, draw an arc in the head as smile;

			Snowman_Page.drawArc(middle-10, top+20, 20, 10, 190, 160);

##### Drawing Arms
We can use lines as arms. It should be start from upper torso;

			Snowman_Page.drawLine(middle-25,top+60,middle-50,top+40);
			Snowman_Page.drawLine(middle+25,top+60,middle+55,top+60);


##### Drawing Hat
We can add a head top of the head;

			Snowman_Page.drawLine(middle-20,top+5,middle+20,top+5);
			Snowman_Page.fillRect(middle-15, top-20, 30, 25);

##### Result of the program
When you run the program you can see the Snowman Java Applet page.
![r.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516305446/rztevqka8kuhjldz6cgo.jpg)

![rr.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516305473/eav4lv0wwb3xpkvwosba.jpg)

##### Github
You can get this program from [Github](https://github.com/aromatheraphy/JApplet_Drawing_Demo/blob/master/src/JApplet_Drawing_Snowman_Demo.java).

![git.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516305854/dhuqfau3fsfuk7meq2p5.jpg)



#### Curriculum
You can find my other java related tutorials in below links.

- [Adding JPEG or GIF image files into the GUI in Java using Eclipse IDE](https://utopian.io/utopian-io/@aromatheraphy/adding-jpeg-or-gif-image-files-into-the-gui-in-java-using-eclipse-ide)
- [Reading Text Files and Parsing It Using Iterators and Delimiter in Java with Eclipse IDE](https://utopian.io/utopian-io/@aromatheraphy/reading-text-files-and-parsing-it-using-iterators-and-delimiter-in-java-with-eclipse-ide)

##### Code of the Program

	import javax.swing.JApplet;
	import java.awt.*;
	
	public class JApplet_Drawing_Snowman_Demo extends JApplet {
		
		public void paint(Graphics Snowman_Page) {
			
			final int middle=150;
			final int top=50;
			
			
			Snowman_Page.setColor(Color.CYAN);
			Snowman_Page.fillRect(0, 0, 300, 225);
		
			Snowman_Page.setColor(Color.DARK_GRAY);
			Snowman_Page.fillRect(0, 175, 300, 50);
		
			Snowman_Page.setColor(Color.ORANGE);
			Snowman_Page.fillOval(-40, -40, 80, 80);
		
			Snowman_Page.setColor(Color.white);
			Snowman_Page.fillOval(middle-20, top, 40, 40);
			Snowman_Page.fillOval(middle-35, top+35, 70, 50);
			Snowman_Page.fillOval(middle-50, top+80, 100, 60);
		
			Snowman_Page.setColor(Color.BLACK);
			Snowman_Page.fillOval(middle-10, top+10, 5, 5);
			Snowman_Page.fillOval(middle+5, top+10, 5, 5);
		
			Snowman_Page.drawArc(middle-10, top+20, 20, 10, 190, 160);
		
			Snowman_Page.drawLine(middle-25,top+60,middle-50,top+40);
			Snowman_Page.drawLine(middle+25,top+60,middle+55,top+60);
		
			Snowman_Page.drawLine(middle-20,top+5,middle+20,top+5);
			Snowman_Page.fillRect(middle-15, top-20, 30, 25);
		}
	}



<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@aromatheraphy/drawing-methods-in-java-applet-japplet-using-eclipse-ide-drawing-a-snowman">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , ,
properties (23)
authoraromatheraphy
permlinkdrawing-methods-in-java-applet-japplet-using-eclipse-ide-drawing-a-snowman
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":32935745,"name":"che","full_name":"eclipse/che","html_url":"https://github.com/eclipse/che","fork":false,"owner":{"login":"eclipse"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","utopian-io","java","eclipse","open-source"],"users":["aromatheraphy"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1516302510/u1omxzwrrbt7znumqqog.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516305446/rztevqka8kuhjldz6cgo.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516305473/eav4lv0wwb3xpkvwosba.jpg","https://github.com/aromatheraphy/JApplet_Drawing_Demo/blob/master/src/JApplet_Drawing_Snowman_Demo.java","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516305854/dhuqfau3fsfuk7meq2p5.jpg","https://utopian.io/utopian-io/@aromatheraphy/adding-jpeg-or-gif-image-files-into-the-gui-in-java-using-eclipse-ide","https://utopian.io/utopian-io/@aromatheraphy/reading-text-files-and-parsing-it-using-iterators-and-delimiter-in-java-with-eclipse-ide"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1516302510/u1omxzwrrbt7znumqqog.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516305446/rztevqka8kuhjldz6cgo.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516305473/eav4lv0wwb3xpkvwosba.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516305854/dhuqfau3fsfuk7meq2p5.jpg"],"moderator":{"account":"deathwing","time":"2018-01-18T23:41:35.959Z","reviewed":true,"pending":false,"flagged":false}}
created2018-01-18 20:10:45
last_update2018-01-18 23:41:36
depth0
children5
last_payout2018-01-25 20:10:45
cashout_time1969-12-31 23:59:59
total_payout_value21.520 HBD
curator_payout_value9.541 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,716
author_reputation657,783,633,007
root_title"Drawing Methods in Java Applet (JApplet) Using Eclipse IDE: Drawing a Snowman"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,470,755
net_rshares4,632,744,544,282
author_curate_reward""
vote details (10)
@deathwing ·
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authordeathwing
permlinkre-aromatheraphy-drawing-methods-in-java-applet-japplet-using-eclipse-ide-drawing-a-snowman-20180118t234142471z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-18 23:41:42
last_update2018-01-18 23:41:42
depth1
children0
last_payout2018-01-25 23:41:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation269,077,595,754,009
root_title"Drawing Methods in Java Applet (JApplet) Using Eclipse IDE: Drawing a Snowman"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,501,773
net_rshares0
@piotras ·
$0.17
good example :)
👍  
properties (23)
authorpiotras
permlinkre-aromatheraphy-drawing-methods-in-java-applet-japplet-using-eclipse-ide-drawing-a-snowman-20180119t094252742z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-19 09:42:51
last_update2018-01-19 09:42:51
depth1
children1
last_payout2018-01-26 09:42:51
cashout_time1969-12-31 23:59:59
total_payout_value0.166 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length15
author_reputation434,677,846,131
root_title"Drawing Methods in Java Applet (JApplet) Using Eclipse IDE: Drawing a Snowman"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,587,888
net_rshares17,043,675,637
author_curate_reward""
vote details (1)
@aromatheraphy ·
Thanks :)
properties (22)
authoraromatheraphy
permlinkre-piotras-re-aromatheraphy-drawing-methods-in-java-applet-japplet-using-eclipse-ide-drawing-a-snowman-20180119t154959925z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-19 15:50:00
last_update2018-01-19 15:50:00
depth2
children0
last_payout2018-01-26 15:50:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9
author_reputation657,783,633,007
root_title"Drawing Methods in Java Applet (JApplet) Using Eclipse IDE: Drawing a Snowman"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,654,199
net_rshares0
@steemitboard ·
Congratulations @aromatheraphy! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvoted.png)](http://steemitboard.com/@aromatheraphy) You got a First Vote
[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/voted.png)](http://steemitboard.com/@aromatheraphy) Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click [here](https://steemit.com/@steemitboard)

If you no longer want to receive notifications, reply to this comment with the word `STOP`

> By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
👍  
properties (23)
authorsteemitboard
permlinksteemitboard-notify-aromatheraphy-20180119t015105000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2018-01-19 01:51:03
last_update2018-01-19 01:51:03
depth1
children0
last_payout2018-01-26 01:51:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length858
author_reputation38,975,615,169,260
root_title"Drawing Methods in Java Applet (JApplet) Using Eclipse IDE: Drawing a Snowman"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,520,015
net_rshares0
author_curate_reward""
vote details (1)
@utopian-io ·
### Hey @aromatheraphy I am @utopian-io. I have just upvoted you!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Suggestions
- Contribute more often to get higher and higher rewards. I wish to see you often!
- Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
#### Get Noticed!
- Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-aromatheraphy-drawing-methods-in-java-applet-japplet-using-eclipse-ide-drawing-a-snowman-20180119t190807127z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-19 19:08:06
last_update2018-01-19 19:08:06
depth1
children0
last_payout2018-01-26 19:08:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,511
author_reputation152,955,367,999,756
root_title"Drawing Methods in Java Applet (JApplet) Using Eclipse IDE: Drawing a Snowman"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,689,902
net_rshares0