GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Apr-2006, 01:18
sgware sgware is offline
New Member
 
Join Date: Apr 2006
Posts: 7
sgware is on a distinguished road

New to Java: Putting a simple image file on the screen


Sorry to ask such a basic question, but I am new to the (unexpectedly complicated) world of Java graphics. I am familiar with basic Java concepts, but that's about it.

All I want to do is add an image to the screen in a JApplet. I need to be able to change the location of this image after it has been put on the screen, and I would like to retain the background transparency that makes PNG files so useful.

Here's the URL of the image:
Code:
http://l4eclan.com/minions/person.png

I looked at several different tutorials and scrapped together this bit of code. I hope it doesn't make me look like a total moron . It complies and runs. The resize and background to blue commands work... but no image is displayed. Can someone please tell me what I am doing wrong? Maybe I am making this too complicated. All I want to do is put an image on the screen. Maybe there is an easier way?

JAVA Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.net.URL;

import javax.swing.*;

/**
 * Description
 *
 * @author Stephen Ware
 * @version 0.01
 */


public class BlockMover extends JApplet
{
	/********************** Instance Variables **********************/

	Container content = getContentPane();
	private int xCoord;
	private int yCoord;
	private static final String personFile = "http://l4eclan.com/minions/person.png";

	/************************* Constructors *************************/

	// Primary Constructor

	/*************************** Methods ****************************/

	// Applet Initializes
	public void init()
	{
		// Set window size and background color
		setSize(500,250);
		content.setBackground(Color.blue);
	}

	// MAIN function
	public static void main(String[] args)
	{
		Image personImg = Toolkit.getDefaultToolkit().getImage(BlockMover.personFile);
		Picture person = new Picture(personImg);
		JFrame f = new JFrame("Map");
		f.addWindowListener(new WindowAdapter() {
		    public void windowClosing(WindowEvent e) {System.exit(0);}
		});
		f.getContentPane().add(person);
		f.setSize(new Dimension(500, 250));
    	f.setVisible(true);
	}
}




/**
 * This is a JPanel that contains a picture.
 *
 * @author Stephen Ware
 * @version #
 */


class Picture extends JPanel
{
	/********************** Instance Variables **********************/

	private Image img;

	/************************* Constructors *************************/

	// Primary Constructor

	public Picture(Image anImage)
	{
		img = anImage;
	}

	/*************************** Methods ****************************/

	// Paint the image
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(img, 50, 50, 50, 50, this);
	}
}
Last edited by cable_guy_67 : 01-Apr-2006 at 07:14. Reason: Please enclose java code in [java] ... [/java] tags
  #2  
Old 02-Apr-2006, 10:59
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: New to Java: Putting a simple image file on the screen


Applet's do not have a 'main' function, per say...

see if the info. at this link may help:
http://www.devx.com/java/article/8030

(as related to the image part, at least)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 02-Apr-2006, 11:58
sgware sgware is offline
New Member
 
Join Date: Apr 2006
Posts: 7
sgware is on a distinguished road

Re: New to Java: Putting a simple image file on the screen


That's an excellent link. Thank you!

The only possible objection that I have is that the example link shows how to use an Applet and not a JApplet. What are the differences between the two? I may not need a JApplet after all.

Can I still use text input boxes and such if I use an Applet and not a JApplet?
  #4  
Old 02-Apr-2006, 21:14
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: New to Java: Putting a simple image file on the screen


I'd say that it would be better to stick with JApplet -- rather than plain-old Applet (I didn't realize the Applet in the other link, sorry).

Basically, JApplet inherits from Applet, but there are differences and I don't know EVERY difference between the two. JAppet is a special swing container that uses a JRootPane [accessed via the getContentPane() call] and is typically more popular because it is generally much easier to use -- especially if you want to use the swing components and without having to use AWT (see below). Applets are strictly AWT.

However, here are a couple of links about converting AWT to Swing for either an applet or application:
http://www.ictp.trieste.it/~manuals/...rting/how.html

then a link to a little more applet-specific conversion info:
http://www.ictp.trieste.it/~manuals/...ps.html#applet

Much more info can be found at the site of these two links!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 02-Apr-2006, 23:33
sgware sgware is offline
New Member
 
Join Date: Apr 2006
Posts: 7
sgware is on a distinguished road

Re: New to Java: Putting a simple image file on the screen


Thank you! You've been super helpful.
  #6  
Old 04-Apr-2006, 23:24
sgware sgware is offline
New Member
 
Join Date: Apr 2006
Posts: 7
sgware is on a distinguished road

Re: New to Java: Putting a simple image file on the screen


Grr... I'm sorry to post again asking for help, but I just can't seem to figure out how to work with JApplets. I think it has something to do with a paint() vs a paintComponent() message? I don't know.

My request is this (sorry if it is a lot to ask): Could someone write me a super basic JApplet that displays a picture? Nothing fancy... just something that when you run it, has a picture in the top left corner. Maybe if I have some example source code to look at, I will be able to figure out what I am doing wrong.
  #7  
Old 05-Apr-2006, 21:55
sgware sgware is offline
New Member
 
Join Date: Apr 2006
Posts: 7
sgware is on a distinguished road

Re: New to Java: Putting a simple image file on the screen


Nevermind! Problem solved... sort of.
  #8  
Old 05-Apr-2006, 22:11
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: New to Java: Putting a simple image file on the screen


How is that JApplet doing?

If you still need a small sample, I'll try to get one for you.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #9  
Old 06-Apr-2006, 18:49
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: New to Java: Putting a simple image file on the screen


Try this... I think it's about as "super basic" as it can be, and places the image in the top-left corner.

JAVA Code:
import javax.swing.*;

public class Picture extends JApplet {
  private JLabel label =
  	new JLabel(new ImageIcon("C:\\reaper.jpg")); // needs YOUR image.

  public void init()
  {
    label.setHorizontalAlignment( SwingConstants.LEFT );
    label.setVerticalAlignment( SwingConstants.TOP );
    getContentPane().add(label);
  }
}
... and here's the output...

__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 06-Apr-2006 at 19:41.
  #10  
Old 07-Apr-2006, 01:58
sgware sgware is offline
New Member
 
Join Date: Apr 2006
Posts: 7
sgware is on a distinguished road

Re: New to Java: Putting a simple image file on the screen


Thank you! That's perfect. This is all starting to make so much more sense.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Scalability in Java and C++ agx Miscellaneous Programming Forum 7 04-Feb-2006 15:35
Checking source codes of image, audio and video files onauc C Programming Language 5 26-Feb-2005 21:47
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:27.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.