+ Reply to Thread
Results 1 to 1 of 1

Thread: Java: Using Paint

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    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.

    Code:
    package tutorials;
    
    import javax.swing.JApplet;
    
    public class GraphicsTut extends JApplet {
    
    	public void init() {
    		System.out.println("The applet has initialized");
    	}
    
    }
    Next we are going to import the graphics class located in the java.awt package.

    Code:
    package tutorials;
    
    import java.awt.Graphics;
    import javax.swing.JApplet;
    
    public class GraphicsTut extends JApplet {
    
    	public void init() {
    		System.out.println("The applet has initialized");
    	}
    	
    }
    Then we are going to override the paint method.

    Code:
    package tutorials;
    
    import java.awt.Graphics;
    import javax.swing.JApplet;
    
    public class GraphicsTut extends JApplet {
    
    	public void init() {
    		System.out.println("The applet has initialized");
    	}
    	
    	public void paint(Graphics g) {
    		
    	}
    }
    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:

    Code:
    package tutorials;
    
    import java.awt.Graphics;
    
    import javax.swing.JApplet;
    
    public class GraphicsTut extends JApplet {
    
    	public void init() {
    		System.out.println("The applet has initialized");
    	}
    	
    	public void paint(Graphics g){
    		g.drawRect(10, 10, 100, 100);
    	}
    	
    }
    Originally posted as Using Paint
    Last edited by John; 08-01-2010 at 09:47 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem with paint
    By zapdude1234 in forum Java Help
    Replies: 3
    Last Post: 08-22-2011, 05:10 PM
  2. Java Advanced Paint
    By Beeko in forum Java Help
    Replies: 1
    Last Post: 06-02-2011, 02:42 AM
  3. Replies: 1
    Last Post: 02-10-2011, 11:02 AM
  4. Java Mini-Paint Program
    By NomNom in forum Java Tutorials
    Replies: 8
    Last Post: 12-14-2010, 10:06 AM
  5. Help with paint and GUI
    By mr_skyflakes21 in forum Java Help
    Replies: 0
    Last Post: 05-11-2009, 07:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts