APPZXOR
Hello and welcome to APPZXOR,

We would appreciate if you register so you can enjoy
the full benefits of browsing, viewing and using our forum.
Here are some features:

• Create threads;
• Reply to threads;
• View links & images;
• Leave positive or negative feedback to a member.

What are you waiting for? Go ahead and register!
It's free, quick and easy!
APPZXOR
Hello and welcome to APPZXOR,

We would appreciate if you register so you can enjoy
the full benefits of browsing, viewing and using our forum.
Here are some features:

• Create threads;
• Reply to threads;
• View links & images;
• Leave positive or negative feedback to a member.

What are you waiting for? Go ahead and register!
It's free, quick and easy!
APPZXOR
Would you like to react to this message? Create an account in a few clicks or log in to continue.


Where Applications Become AppZ. Join Us Now!
 
HomePortalSearchLatest imagesRegisterLog in
Search
 
 

Display results as :
 
Rechercher Advanced Search
Latest topics
» See you next decade.
Beginner Java: Displaying a 2D background Icon_minitimeby LarsValraz Fri Sep 09, 2022 4:43 pm

» Clean Up On Database
Beginner Java: Displaying a 2D background Icon_minitimeby Vex338 Mon Jul 06, 2020 2:59 pm

» Guess who's back!
Beginner Java: Displaying a 2D background Icon_minitimeby Vex338 Thu Feb 01, 2018 12:16 pm

» [COC] Clash of Clans
Beginner Java: Displaying a 2D background Icon_minitimeby D'ShadowZRay Thu Jan 28, 2016 12:10 am

» iam new
Beginner Java: Displaying a 2D background Icon_minitimeby D'ShadowZRay Thu Jan 28, 2016 12:06 am

» I need some help
Beginner Java: Displaying a 2D background Icon_minitimeby akumasan_01 Fri May 01, 2015 1:25 pm

» HELLO GUYS! :))
Beginner Java: Displaying a 2D background Icon_minitimeby Appz-RhastaSix Fri Sep 19, 2014 9:14 am

» Visual C# Programming Basics
Beginner Java: Displaying a 2D background Icon_minitimeby Appz-RhastaSix Wed Sep 10, 2014 9:03 am

» Old Game!
Beginner Java: Displaying a 2D background Icon_minitimeby iRegen Tue May 13, 2014 12:28 am


Top posters
[Detheroc_93]
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 
MrStar
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 
kurosakinaruto
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 
GreyPhantom
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 
ShadowSonic
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 
Appzwesley29
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 
z_f
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 
MasterGandeo
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 
Vex338
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 
wafumon
Beginner Java: Displaying a 2D background I_vote_lcapBeginner Java: Displaying a 2D background I_voting_barBeginner Java: Displaying a 2D background I_vote_rcap 

Share | 
 

 Beginner Java: Displaying a 2D background

View previous topic View next topic Go down 
AuthorMessage
Gk-[invisible]
Super Member

Gk-[invisible]

Posts : 100
Join date : 2012-01-09
Age : 31
Location : philippines and germany

Beginner Java: Displaying a 2D background Empty
PostSubject: Beginner Java: Displaying a 2D background   Beginner Java: Displaying a 2D background Icon_minitimeSun Feb 19, 2012 5:35 am

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
[You must be registered and logged in to see this link.] ., call the project what ever you like I'm going to call my "JavaAPI".

[You must be registered and logged in to see this image.]


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.

[You must be registered and logged in to see this image.]

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")Wink creates the frame.

The 3rd line (frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE)Wink 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".


[You must be registered and logged in to see this image.]

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());

[You must be registered and logged in to see this image.]

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!
Back to top Go down
[Detheroc_93]
Administrator
Administrator

[Detheroc_93]

Posts : 5628
Join date : 2011-03-12

Beginner Java: Displaying a 2D background Empty
PostSubject: Re: Beginner Java: Displaying a 2D background   Beginner Java: Displaying a 2D background Icon_minitimeSun Feb 19, 2012 7:26 am

Nice tutorial! Keep it up! Glad to have you back - haven't seen you for a while.
Back to top Go down
 

Beginner Java: Displaying a 2D background

View previous topic View next topic Back to top 

 Similar topics

-
» [Tutorial]How To Hack Site [Beginner]
» Photoshop: Make a Beginner Forum Signature - Tutorial
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
APPZXOR :: Discussions :: Tricks & Tutorials-
Free forum | ©phpBB | Free forum support | Report an abuse | Forumotion.com