473,441 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

How to use images in JFrame's and JComponent's

Hey, I'm trying to make a game for a school project that uses image files. I've been working on changing some pre-written code (I believe from IBM) that we used for pong as I'm not yet an expert on swing; however, the pre-written code only allows for the insertion of solid colored objects, not images. The pre-written code was divided into Game (which was a child of JFrame) and GameObject (which was a child of JComponent). The original paint method in GameObject was

Expand|Select|Wrap|Line Numbers
  1.  public void paint(Graphics g) {
  2.   Rectangle r = getBounds();
  3.   g.setColor(c);
  4.   g.fillRect(0, 0, (int)r.getWidth(), (int)r.getHeight());
  5.  }
  6.  
I changed it to be
Expand|Select|Wrap|Line Numbers
  1. public void paint(Graphics g) {
  2.  g.drawImage(image, getX(), getY(), this);
  3. }
  4.  
Where image has already been declared and constructed as follows
Expand|Select|Wrap|Line Numbers
  1. BufferedImage image;
  2. try {
  3.         String path = "images/spaceship.png";
  4.         URL url = getClass().getResource(path);
  5.         image = ImageIO.read(url);
  6.     }
  7.     catch (IOException e)
  8.     {
  9.       System.out.println("not valid image");
  10.     }
  11.  
Unfortunately when I run the game, the GameObject does not appear at all in the JFrame, except when I move it into the top left corner of the screen.

I'm also not sure if I should use a BufferedImage or a normal Image to construct my image, and what the best way to construct it would be.

Any help would be appreciated, thanks.
May 2 '10 #1
0 1680

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Wayne | last post by:
I have a script that uses filesystemobject that reads files from a given path, in my case images. It is running on a server that is 2000 adv svr w/ all current patches. The script prior to some...
11
by: Penelope Baker | last post by:
Greetings: I cannot seem to get IE 6.0 to pre-cache my rollover images. Every time the user rolls over a link, it rereads it from the server, even though it is well into the cache by that time....
2
by: Julie | last post by:
Hi, I'm trying to change images on a website without reloading the whole page and use the following code to preload the images: var preloadFlag = false; function preloadImages() { if...
3
by: Simon | last post by:
This problem has been driving me mad for months.... Seen a few posts on forums about it but no answers... No mention on MSDN etc. XP Pro SP1, VS.NET (c#) .Net framework 1.1, IIS 5.1. In a...
4
toxicpaint
by: toxicpaint | last post by:
Hi, can anyone give me a hand. I'm currently displaying 4 random images at the top of a page. I did this using an array of 35 pictures and then writing them to page. The problem I have is that in...
3
by: Firecore | last post by:
Can someone please explain what JFrames are?
5
by: remon87 | last post by:
I need some help. I have javasript that creates the submenu but it works if I have a text with css. I need it to do the same with a roll over images. so when I click on the image the submenu...
0
by: Krasimir Chonov | last post by:
Hello, Java: 1.6.0_23 Netbeans: 6.9.1 I`m making chatting program and I wanted to add a background image to the main window. So I added a image via JPanel and after that the components are no...
2
by: Prashanthpacchu | last post by:
Hi I am developing an website. So in that i m trying to make like an image slide show. just like in www.habtoor.com. But i am using 3 buttons on leftside. 6 images are used for this 3 buttons....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.