5 Replies - 1094 Views - Last Post: 22 March 2011 - 09:12 AM Rate Topic: -----

#1 neid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 11

How to get Images in an applet?

Posted 18 March 2011 - 07:26 AM

Ok, as a complete newbie to Java, i found a code like this to paint images into an applet, but it is not working, I pasted the image in the same folder, and this is the simplest code I found yet to do this... so the question is where is my mistake?
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class getImages extends Applet{
	Image bkg;
	public void init (){
		bkg = getImage(getCodeBase(), "bkg.jpg");
	}
	
	public void paint(Graphics g){
		g.drawImage(bkg, 0, 0, this);
	}
}



thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: How to get Images in an applet?

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How to get Images in an applet?

Posted 18 March 2011 - 02:13 PM

When you say something "doesn't work," please provide details. What's not working, or how is it not working? If you're getting error messages, provide them exactly as they appear on your system, cut and paste preferred. When you say the "same folder" which folder do you mean? In which folder is your source code, the resulting *.class file, and the image?

You might check the tutorials here for advice on using graphics in an applet, though I don't have a specific one to point you to. I'll try to find an example that might be helpful and let you know.

<edit> Here's one: You might review Chapter 13 of version 6 (beta) of Eck's free Java book, "Introduction to Programming Using Java, Sixth Edition," at the link below.

Eck's book

This post has been edited by GregBrannon: 18 March 2011 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 neid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 11

Re: How to get Images in an applet?

Posted 18 March 2011 - 03:01 PM

ok, the problem is that when I run the applet I can not see the image, the applet did not paint it, but I do not know the reason, so I thought that maybe was because the code did not get the path of the image so I put both files (getImages.java and bkg.jpg) in the same folder, hoping it to work but it did not. Maybe with these details you can help me more. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How to get Images in an applet?

Posted 18 March 2011 - 03:50 PM

Are you getting any error messages at the command line, or if you're running from an IDE, in the Console window? And what's the rest of your code look like? How are you running what you've posted?

This post has been edited by GregBrannon: 18 March 2011 - 03:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,926
  • Joined: 27-December 08

Re: How to get Images in an applet?

Posted 18 March 2011 - 04:47 PM

If the File was not being found, you would have gotten a NullPointerException.

A couple things. First, always invoke the super.paintComponent() method immediately when overriding paintComponent(). It handles key superclass functionality for painting. Second, don't use AWT Components. They are outdated. Instead, use the more modern Swing Components and JComponents like JApplet.
Was This Post Helpful? 0
  • +
  • -

#6 neid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 11

Re: How to get Images in an applet?

Posted 22 March 2011 - 09:12 AM

View Postmacosxnerd101, on 18 March 2011 - 04:47 PM, said:

If the File was not being found, you would have gotten a NullPointerException.

A couple things. First, always invoke the super.paintComponent() method immediately when overriding paintComponent(). It handles key superclass functionality for painting. Second, don't use AWT Components. They are outdated. Instead, use the more modern Swing Components and JComponents like JApplet.


Ok, this is my 2 month learning java by myself, and I found this code, I just want to load an image as a background in the applet and that the applet take the image size, it has any other purpose than learning, so until now, the code goes like this, using the most of swing components following macosxnerd101 advice:

import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JPanel;

public class imageLoades extends JApplet{
	ImageIcon imagen = new ImageIcon("bkg.jpg"); 	
	
	public void init() {
		int imgX = imagen.getIconWidth();
		int imgY = imagen.getIconHeight();
		setSize(imgX, imgY);
	}
	public void paintComponent(Graphics g){
		JPanel pan = new JPanel();
		super.paintComponent(g);
		imagen.paintIcon(pan, g, 0, 0);
	}
}



The questions are:
1. When I run the applet, it do not upload or paints the image, so, where is the mistake in the code?.
2. I found this code using java.awt.Graphics but I could not found the swing replacement for it, which one should I replace Graphics for?

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1