GPWiki.org
GPWiki.org
It is currently Sat May 25, 2013 8:10 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sat Apr 14, 2012 1:59 am 
Novice

Joined: Wed Apr 11, 2012 2:11 am
Posts: 6
Hello everyone!

Well i am beginner in Java in which is the only language i have any experience on.

Anyways getting to the point i want to know how to use one of my images as the background of my Frame.

Code:
package TestingPackage;

import javax.swing.JFrame;

public class Frame extends JFrame{
   public static void main(String[] args){
      JFrame Frame = new JFrame();
      Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Frame.setSize(1280, 720);
      Frame.setVisible(true);
      Frame.setTitle("Game Frame/Window");
   }
}


That is my current code that i have for my J Frame and i want to know how i can add an image to it so that every time i open the frame the Background is the picture i want.

Thank you
DaSnipeKid-


Top
 Profile  
 
PostPosted: Sat Apr 14, 2012 7:04 am 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
The simplest approach is to draw the background every time before you draw anything else.

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Sat Apr 14, 2012 3:49 pm 
Novice

Joined: Wed Apr 11, 2012 2:11 am
Posts: 6
Alberth wrote:
The simplest approach is to draw the background every time before you draw anything else.

And how can i draw the background. Do you know any sites that might explain it? or can you do so?


Top
 Profile  
 
PostPosted: Sat Apr 14, 2012 5:19 pm 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
I do program in Java, but not GUIs (nor games), so my knowledge is limited here :(


A background is often just an image, so you need to draw an image.
Given that you use swing, perhaps this may explain how to do it:

http://stackoverflow.com/questions/7761 ... with-swing


However, I would recommend that you read a tutorial about swing so you get an idea of what it does and how it works.

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Mon Apr 23, 2012 3:41 am 
U could try storing the picture you want to use inside the project and then call it using the Draw Method, another approach is to maybe us it as an Icon.

Here is a snippet of code that i did

import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

public class Gui extends JFrame {

private JComboBox box; // create a JComboBox variable
private JLabel picture;

private static String[] fileName = { "b.png", "b.png" }; // static so it

private Icon[] pics = { new ImageIcon(getClass().getResource(fileName[0])),
new ImageIcon(getClass().getResource(fileName[1])) };// stores the


public Gui ()
{
super("the title ");
setLayout(new FlowLayout());

box = new JComboBox(fileName); // puts all the options in a list

box.addItemListener(
new ItemListener(){ // Anonymous inner class
public void itemStateChanged(ItemEvent event)
{
if (event.getStateChange() == ItemEvent.SELECTED)
picture.setIcon(pics[box.getSelectedIndex()]);
}
}
);

add(box);
picture = new JLabel(pics[0]);
add(picture);


Top
  
 
PostPosted: Mon Apr 23, 2012 3:43 am 
Rookie

Joined: Sun Apr 22, 2012 3:55 am
Posts: 4
that last post was by me using the JCombo box


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group