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 - 1449 Views - Last Post: 17 December 2010 - 06:09 AM Rate Topic: -----

#1 noob^2  Icon 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()  
    {
    	setTitle("MiniEdit");
    	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);
    	add("North",p);
    
    	ta = new TextArea(10,20); add("Center", ta);
    	aktuellMapp = ".\\"; aktuellFil = "text.txt";
    	
    	Image[] images = loadImages();

    	
    	addWindowListener(this);    	      	
    }
 		public void actionPerformed(ActionEvent e)
 			
 		{
 			if(e.getSource()==b1) ett();
 			if(e.getSource()==b2) två();
 		//	if(e.getSource()==b3) tre();
 		}   
    	private void ett()
    	{
    		
    	
    	ta.setBackground(Color.yellow);
    		  	
    	}
    	private void två()
    	{
    		
    	
    	ta.setBackground(Color.black);
    		  	
    	}
    		
    	private void oppna()
    	{
    		d = new FileDialog(this, "öppna fil", FileDialog.LOAD);
    		d.setDirectory(aktuellMapp);
    		d.setFile(aktuellFil);
    		d.setVisible(true);
    		if (d.getFile()== null) return;
    		aktuellMapp = d.getDirectory();
    		aktuellFil = d.getFile();
    		filnamn = aktuellMapp + aktuellFil;
    		
    		try
    		{
    			
    		}
    		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();
  				f.setSize(360,200);
  				f.setResizable(true);
  				f.setVisible(true);  		
    		
    		}
    		
 } 


Is This A Good Question/Topic? 0
  • +

Replies To: Loading image in to Java

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • 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:
http://www.dreaminco...part-1-loading/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1