Title says it all.

I'm loading my image with the statement below

//logo = getImage(getDocumentBase(),"http://fromdustelune.com/FD_Logo.png");
logo = getImage(getDocumentBase(),"FD_Logo.png");

*The commented one won't work until I sign this applet (or so I believe that's how it's done*

Then I call the paint method:

public void paint (Graphics g){
	 g.drawImage(logo, 0, 0, logo.getWidth(this), logo.getHeight(this), this);
	}

When you call the paint method, is there a way to only paint that object in the location you want it? My main problem is I have about 4 labels and 2 JComboBoxes that I need to show in my applet below this image. I'll paste all of my code below but this seems to be my main problem.

Also, one other quick question. Once I get my applet working as intended, once I make the JAR and sign it, the applet will be able to function with URLs correct?


All of my program:

import java.util.*;
import javax.swing.JApplet;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.Desktop;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.*;

public class Ranks extends JApplet{
	
	//Declare varibles
	private String address, worldRank, realmRank;
	private JLabel worldRankLabel, realmRankLabel,tierNormalLabel, tierHeroicLabel;
	private JComboBox tierNormal, tierHeroic;
	private Image logo;
	
public void init(){
		Container contents = getContentPane();
		contents.setLayout(new FlowLayout());
		
		String[] bossesNormal = {"Morchok", "Yor'sahj", "Zon'ozz", "Hagara", "Ultraxion", "Blackhorn", "Spine", "Madness"};
		String[] bossesHeroic = {"Morchok", "Yor'sahj"};
		
		realmRank = new String("number1");
		worldRank = new String("number2");
		
		tierNormal = new JComboBox(bossesNormal);
		tierHeroic = new JComboBox(bossesHeroic);
		
		//Set logo to guild image
		//logo = getImage(getDocumentBase(),"http://fromdustelune.com/FD_Logo.png");
		logo = getImage(getDocumentBase(),"FD_Logo.png"); 		
				
		realmRankLabel = new JLabel("Realm Rank:      " + realmRank);
		worldRankLabel = new JLabel("World Rank:     " + worldRank);
		tierNormalLabel = new JLabel("Normal kills");
		tierHeroicLabel = new JLabel("Heroic kills");
	
		
		
      contents.add(realmRankLabel);
		contents.add(worldRankLabel);
		contents.add(tierNormalLabel);
		contents.add(tierNormal);
		contents.add(tierHeroicLabel);
		contents.add(tierHeroic);
		
		
		
		getContentPane().setBackground(Color.white);
		setSize(185, 200);
		//run();
	}
	/*
public void run(){
	
	try{
	  URL url = new URL("http://www.wowprogress.com/guild/us/elune/From+Dust/json_rank");
 	  BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

     String inputLine;
	  String data = null;
		// Set data = to the only line in the input stream from the URL
		data = (inputLine = in.readLine());
		
		worldRank = data.substring(28,31);
	}
	
	catch(IOException io){}
	
	
}
*/
/*
	public void paint (Graphics g){
	 g.drawImage(logo, 0, 0, logo.getWidth(this), logo.getHeight(this), this);
	}
*/

}

Any help would be much appreciated! Thanks in advance!

Recommended Answers

All 7 Replies

Can you put the image into the jar file and save loading it from the server?
Look at using ImageIO and the Class class's getResource() method

a way to only paint that object in the location you want it

Not sure what you are asking here. By object do you mean the image?
The draw method has x,y parameters to specify location.

I need to show in my applet below this image

This is also confusing. The applet is shown in a html page in a browser. How would the applet control an image outside of itself in the browser?

Well when you call the paint method, it seems to paint over the whole applet and not just the specified height and width of the image I've loaded in. The reason I say this is because I can resize the applet while testing and none of the labels that are supposed to appear on the applet appear - even if the label were covering up some labels, it wouldn't cover up all of them.

Basically, what I was doing before I went this route: made a JLabel and created a new imageIcon inside the label to display the logo. Below that JLabel were a few other labels which displayed the data I wanted to display.

After testing my applet with some simple HTML code, the applet wouldn't run at all this way. It ran perfectly fine in an IDE test, but on a basic html site, it wouldn't load. After testing I found that the image was the problem, removed the image and the applet displayed everything as planned. I found that other people were loading in images through this method onto applets, but I can't seem to master it. Hopefully this answers your questions.

Well when you call the paint method, it seems to paint over the whole applet

It will paint over the component it is in. If that is the whole applet then the whole applet can be painted over. Create a JPanel, override its paintComponent method and then it should not paint outside of the boundary of the JPanel.

the applet wouldn't run at all this way.

Not sure what is "this way".

the image was the problem

What problem? Did you try using ImageIO and getResource() to load the image?


Can you make a small simple program (SSCCE) that compiles and executes to show the problem?

This is the way I was talking about earlier. I figured if the image was located in the same directory as the .class and html file, it should bring up the image without any issue like so (by the way it should compile and execute for you):

import java.util.*;
import javax.swing.JApplet;
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.*;


public class LogoImage extends JApplet{
	
	//Declare varibles
		private JLabel logo;

	
	
public void init(){
		Container contents = getContentPane();
		contents.setLayout(new FlowLayout());
		
		logo = new JLabel(new ImageIcon("FD_Logo.png"));
		
		contents.add(logo);
		
		
		
		getContentPane().setBackground(Color.white);		
	}


}

Along with the HTML:

<html>
<head>
<p> this works </P
</head>

<body>
<p>This also works</p>

<applet code = "LogoImage.class" width = 500 height = 600>
</applet>

</body>

</html>

This is clearly the wrong way to fetch an image with an applet (since the applet won't ever load on the page.) But you're saying ImageIO and getResource() will do it for me? I don't think I've ever used those.

ImageIO and getResource() will do it

You can load images with them.

Could you show me a quick example on calling the getResource() method with ImageIO? I'm not exactly sure how I'd set it up = /

Do a Search on the forum or with Google.
There are lots of code samples out there

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.