Lost Password?

  #1 (permalink)  
Old 09-23-2007, 10:04 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,345
Last Blog:
PHP Function Overloadi...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM 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. }
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
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:08 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads