Loading image in to Java

Hi I have an assignment where I am gonna load images in to my program

Page 1 of 1

1 Replies - 1965 Views - Last Post: 17 December 2010 - 06:09 AM Rate Topic: -----

#1 noob^2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-November 09

Loading image in to Java

Posted 17 December 2010 - 05:26 AM

Hey there fellow programmers, im quite new to programming and now I have an assingment where I am to load images in to a program. The images are then gonna be linked in to buttons. button 1 for 1 picture and button 2 for 2 pictures etc. The problem is that I dont know how I should do this? :( Wich method is the best to go with? Im using frame and not Applet.

PS some words are in swedish, ask me and ill translate. Thanks for helping a newbie!

This is my code so far :
 import java.awt.*;
import java.awt.event.*;
import java.io.*;

class index extends Frame implements WindowListener, ActionListener 
	Button b1, b2, b3;
	Panel p;
	TextArea ta;
	String filnamn, aktuellMapp, aktuellFil, rad ;
	FileDialog d; // dialogruta för filhantering
	//DataOutputStream utfil;
	BufferedReader infil;
    static String imageFile ="bild.jpg";
    public void init()  
    	p = new Panel();
    	b1 = new Button("Ett"); p.add(b1); b1.addActionListener(this);
    	b2 = new Button("Två"); p.add(b2); b2.addActionListener(this);
    	b3 = new Button("Tre"); p.add(b3); b2.addActionListener(this);
    	ta = new TextArea(10,20); add("Center", ta);
    	aktuellMapp = ".\\"; aktuellFil = "text.txt";
    	Image[] images = loadImages();

 		public void actionPerformed(ActionEvent e)
 			if(e.getSource()==b1) ett();
 			if(e.getSource()==b2) två();
 		//	if(e.getSource()==b3) tre();
    	private void ett()
    	private void två()
    	private void oppna()
    		d = new FileDialog(this, "öppna fil", FileDialog.LOAD);
    		if (d.getFile()== null) return;
    		aktuellMapp = d.getDirectory();
    		aktuellFil = d.getFile();
    		filnamn = aktuellMapp + aktuellFil;
    		catch(Exception e) {System.out.println("Vid öppna: "+e);}
    	 public void windowClosing(WindowEvent e)
    		setVisible(false); dispose(); System.exit(0);	
    		public void windowOpened(WindowEvent e){}
    		public void windowClosed(WindowEvent e){}
    		public void windowIconified(WindowEvent e){}
    		public void windowDeiconified(WindowEvent e){}
    		public void windowActivated(WindowEvent e){}
    		public void windowDeactivated(WindowEvent e){}
    		public static void main(String[] args)
    			index f;
    			f = new index();

Is This A Good Question/Topic? 0
  • +

Replies To: Loading image in to Java

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Loading image in to Java

Posted 17 December 2010 - 06:09 AM

I would load the image into an ImageIcon and the assign it to your button.

Here's how:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1