Register for your free account! | Forgot your password?

Go Back   elitepvpers > Off-Topics > Tutorials
You last visited: Today at 12:00

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Beginner Java: Displaying a 2D background

Discussion on Beginner Java: Displaying a 2D background within the Tutorials forum part of the Off-Topics category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Feb 2010
Posts: 475
Received Thanks: 370
Beginner Java: Displaying a 2D background

Displaying a 2D Background

Difficulty: 2 / 10.
Assumed Knowledge: basic knowledge of the Java language.
Information: This tutorial will teach you how to create a basic API and display an image.

What is an API..?

An application programming interface (API) is an interface implemented by a software program that enables it to interact with other software. It facilitates interaction between different software programs similar to the way the user interface facilitates interaction between humans and computers.



Creating the project

We need to create a new project through your IDE, I'm currently using Eclipse, which is available free
., call the project what ever you like I'm going to call my "JavaAPI".




Setting the main class up


Now once you have created your project we will need to create a class, call it what ever you like I'm going to call it "Client", We don't want to package it so keep it blank.



Creating the API

In your main class we will need to add some important imports and create a constructor.

Code:
import javax.swing.*;
Swing is the primary Java GUI widget toolkit. It is part of Sun Microsystems' Java Foundation Classes (JFC), an API for providing a graphical user interface (GUI) for Java programs.

Code:
public static void main(String[] args) {

	}
Now we can add our main code

Code:
JFrame frame = new JFrame("Application");
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setSize(500, 500);
		frame.setResizable(false);
		
		frame.setVisible(true);
To the explaining of the code,

The 1st line (JFrame frame = new JFrame("Application") creates the frame.

The 3rd line (frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE) allows the application to exit once it has been closed.

The 5th and 6th line is pretty self-explanatory...

and The 8th line display the window to the user.


Creating the second class

Same as before we need to create a new class, I'm going to call mine "Board".




Setting the second class up


And once again same as before, we need to add some imports, create a constructor and extend the JPanel.

The Abstract Window Toolkit (AWT) is Java's original platform-independent windowing, graphics, and user-interface widget toolkit. The AWT is now part of the Java Foundation Classes (JFC), the standard API for providing a graphical user interface (GUI) for a Java program.


Code:
import java.awt.*;
import javax.swing.*;
Code:
public class Board extends JPanel {

	public Board() {

	}

}

Displaying the Image


Before any image will be displayed we need to declare the Image.

Code:
Image BGTexture;
In the constructor, we will need to add the file the image will display.

Code:
ImageIcon ii = new ImageIcon("yourImage.png");
		BGTexture = ii.getImage();

An implementation of the Icon interface that paints Icons from Images. Images that are created from a URL or filename are preloaded using MediaTracker to monitor the loaded state of the image.

We will now need to create a method that will load the image.


Code:
public void paint(Graphics g) {

	}
In-side that method we can add loads of different things to display anything graphical, whether it be an image from your computer, or generated through code.

Code:
g.drawImage(BGTexture, 0, 0, null);
Notice that the Image we declared at the start was "BGTexture", that will be used through-out this class by displaying it, the ", 0, 0" represent coordinates (X, Y).

now that is all done there is one more tihng to do, we need to go back into our main class (Client.java) and add the this class (Board.java)

Code:
frame.add(new Board());


Final Code:

Client.java

Code:
import javax.swing.*;

public class Client {

	public static void main(String[] args) {
		JFrame frame = new JFrame("Application");
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.add(new Board());
		
		frame.setSize(500, 500);
		frame.setResizable(false);
		
		frame.setVisible(true);
	}
	
}
Board.java

Code:
import java.awt.*;
import javax.swing.*;

public class Board extends JPanel {

	Image BGTexture;
	
	public Board() {
		ImageIcon ii = new ImageIcon("yourImage.png");
		BGTexture = ii.getImage();
	}
	
	public void paint(Graphics g) {
		g.drawImage(BGTexture, 0, 0, null);
	}
	
}
enjoy!
XxXchowXxX is offline  
Reply


Similar Threads Similar Threads
[JAVA Error] Could not create the java virtual machine
07/21/2013 - Technical Support - 10 Replies
Schönen Abend! Leider hat es sich aus einem unerfindlichen Grund ergeben, dass sobald ich die Minecraft.exe starten will die Errormeldung kommt. Die Tips auf Minecraft.net habe ich schon ohne Erfolg befolgt. Hoffe ihr könnt mir weiterhelfen... Mein PC:
[Java] Could not create the Java virtual machine
06/22/2011 - Minecraft - 1 Replies
hallo ihr minecraftler ^^ habe seit heute das problem das wenn ich minecraft starte original als auch cracked das diese fehlermeldung kommt: Java virtual machine Launcher Could not create the Java virtual machine
C# displaying charname problem
04/08/2011 - SRO Coding Corner - 5 Replies
Hey guys, I've got some problems at the moment, im creating a information bar , or how to call it that shows my HP and MP and name ect ect... But at the moment it only display the first letter of my name and servername, sometimes it shows everything and sometimes it shows nothing ... Here is the code for my char name string charactername = mem.ReadString(0xF9E9B8);
[Question]Displaying Packets
01/11/2009 - CO2 Private Server - 1 Replies
just asking how to put that option back in there to display packets in the console Help is appreciated and THanx



All times are GMT +2. The time now is 12:00.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.