Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-23-2007, 10:04 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,234
Last Blog:
Passwords
Credits: 872
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default Java: Using Paint

One of the most popular things Java is used for is to make online games. While making 3d games is near impossible, two dimensional games are very do-able.

Although designing an entire game is beyond the scope of this tutorial, I will introduce to you the basic use of the graphics library.

We are going to implement this into a applet to lets start with the basic framework.

Java Code:
  1. package tutorials;
  2.  
  3. import javax.swing.JApplet;
  4.  
  5. public class GraphicsTut extends JApplet {
  6.  
  7.     public void init() {
  8.         System.out.println("The applet has initialized");
  9.     }
  10.  
  11. }

Next we are going to import the graphics class located in the java.awt package.

Java Code:
  1. package tutorials;
  2.  
  3. import java.awt.Graphics;
  4. import javax.swing.JApplet;
  5.  
  6. public class GraphicsTut extends JApplet {
  7.  
  8.     public void init() {
  9.         System.out.println("The applet has initialized");
  10.     }
  11.    
  12. }

Then we are going to override the paint method.

Java Code:
  1. package tutorials;
  2.  
  3. import java.awt.Graphics;
  4. import javax.swing.JApplet;
  5.  
  6. public class GraphicsTut extends JApplet {
  7.  
  8.     public void init() {
  9.         System.out.println("The applet has initialized");
  10.     }
  11.    
  12.     public void paint(Graphics g) {
  13.        
  14.     }
  15. }

Once we are at this point, we can call many methods on the Graphics object "g." I'm going to create a simple square. To do that we are going to use the

drawRect() method which requires four parameters. The x location of the upper left corner, the y location of the upper left corner, the width, and the

height. We end up with a class that looks like this:

Java Code:
  1. package tutorials;
  2.  
  3. import java.awt.Graphics;
  4.  
  5. import javax.swing.JApplet;
  6.  
  7. public class GraphicsTut extends JApplet {
  8.  
  9.     public void init() {
  10.         System.out.println("The applet has initialized");
  11.     }
  12.    
  13.     public void paint(Graphics g){
  14.         g.drawRect(10, 10, 100, 100);
  15.     }
  16.    
  17. }
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.

Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
questions about java games stack Java Help 3 07-02-2007 05:35 PM
MS-SQL deadlock and hang the Java application reachpradeep Database & Database Programming 1 03-11-2007 04:20 AM
Java Facts techni68 Java Help 0 01-17-2007 01:41 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM
Java Help Files xXHalfSliceXx Java Help 3 11-28-2006 11:30 PM


All times are GMT -5. The time now is 03:35 AM.

Contest Stats

Xav ........ 1276.19
MeTh0Dz|Reb0rn ........ 1048.58
morefood2001 ........ 879.43
John ........ 872.39
marwex89 ........ 869.98
WingedPanther ........ 761.06
Brandon W ........ 684.87
chili5 ........ 294.12
Steve.L ........ 216.18
dargueta ........ 192.86

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 81%

Ads