Working with Image

Hi,

I've got an application that reads from a text file to create a (recipe) object. Now I need to add a picture associating with the object and display it somehow...

What would be the best way in doing this?

any recommend page i should read on and any code suggestions are much appreciated!

thanks

[330 byte] By [CUIDAa] at [2007-11-26 19:14:21]
# 1
Here is a link to part of the swing tutorial, it is basically what you need to know. http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
morgalra at 2007-7-9 21:15:16 > top of Java-index,Desktop,Core GUI APIs...
# 2

You want to add it to a JLabel, a JOptionPane, a JPanel? ... one example:

JPanel jp = new JPanel();

Icon pix = new ImageIcon("pix.jpg");

Jlabel jl = new JLabel(pix);

JButton jb = new JButton("Switch Pix");

jp.add(jl);

jp.add(jb);

jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

if ( ae.getSource() == jl && someOtherCondition == true )

jl.setIcon(new ImageIcon("pix2.jpg"));

}

});

// ...

Message was edited by:

abillconsl

abillconsla at 2007-7-9 21:15:16 > top of Java-index,Desktop,Core GUI APIs...
# 3

import java.awt.*;

import java.awt.image.*;

import javax.swing.*;

import javax.swing.*;

class SampleJFrame extends JFrame

{

Image image;

Dimension d;

ImageIcon imageicon;

PixelGrabber pg;

//int pixels[];

public SampleJFrame()

{

super("This Is A JFrame For MemoryImageSource");

try

{

setSize(400,400);

Dimension d=getSize();

int w=d.width;

int h=d.height;

int pixels[]=new int[w*h];

ImageIcon imageicon=new ImageIcon("G:/java files1/images/aa.jpg");

image=imageicon.getImage();

int iw=image.getWidth(null);

int ih=image.getHeight(null);

image=image.getScaledInstance(w,h,Image.SCALE_SMOOTH);

pg=new PixelGrabber(image,0,0,w,h,pixels,0,iw);

pg.grabPixels();

image=createImage(new MemoryImageSource(w,h,pixels,0,w));

setVisible(true);

}

catch(Exception e)

{

}

}

public void paint(Graphics g)

{

g.drawImage(image,0,0,this);

}

}

public class Image8

{

public static void main(String args[])

{

SampleJFrame samplejframe=new SampleJFrame();

}

}

qUesT_foR_knOwLeDgea at 2007-7-9 21:15:16 > top of Java-index,Desktop,Core GUI APIs...
# 4

Another example

import java.awt.*;

import java.awt.image.*;

import javax.swing.*;

class SampleJFrame extends JFrame

{

Image image;

public SampleJFrame()

{

super("This Is A JFrame For MemoryImageSource");

setSize(400,400);

Dimension d=getSize();

int w=d.width;

int h=d.height;

int pixels[]=new int[w*h];

int i=0;

for(int y=0;y<h;y++)

{

for(int x=0;x<w;x++)

{

int r=(x^y)&0xff;

int g=(x*2^y*2)&0xff;

int b=(x*4^y*4)&0xff;

pixels[i++]=(255 ><< 24) | (r << 16) | (g << 8) | (b);

}

}

image=createImage(new MemoryImageSource(w,h,pixels,0,w));

setVisible(true);

}

public void paint(Graphics g)

{

g.drawImage(image,0,0,this);

}

}

public class Image7

{

public static void main(String args[])

{

SampleJFrame samplejframe=new SampleJFrame();

}

}

qUesT_foR_knOwLeDgea at 2007-7-9 21:15:16 > top of Java-index,Desktop,Core GUI APIs...
# 5

import java.awt.image.*;

import java.awt.*;

import javax.swing.*;

import java.lang.*;

class SampleJFrame extends JFrame// implements Runnable

{

Image image[]=new Image[7];

MediaTracker tracker;

int tracked=0,donecount=0;

String loaded="";

int current_img=0;

Thread t;

public SampleJFrame()

{

super("This Is A JFrame For A MediaTracker");

ImageIcon imageicon0=new ImageIcon("G:/java files1/images/aa.jpg");

image[0]=imageicon0.getImage();

ImageIcon imageicon1=new ImageIcon("G:/java files1/images/aa.jpg");

image[1]=imageicon1.getImage();

ImageIcon imageicon2=new ImageIcon("G:/java files1/images/aa.jpg");

image[2]=imageicon2.getImage();

ImageIcon imageicon3=new ImageIcon("G:/java files1/images/aa.jpg");

image[3]=imageicon3.getImage();

ImageIcon imageicon4=new ImageIcon("G:/java files1/images/aa.jpg");

image[4]=imageicon4.getImage();

ImageIcon imageicon5=new ImageIcon("G:/java files1/images/aa.jpg");

image[5]=imageicon5.getImage();

ImageIcon imageicon6=new ImageIcon("G:/java files1/images/aa.jpg");

image[6]=imageicon6.getImage();

this.setSize(500,500);

this.setVisible(true);

tracker=new MediaTracker(this);

for(int i=0;i<7;i++)

{

tracker.addImage(image[i],i);

tracked++;

}

//t=new Thread(this);

//t.start();

//}

//public void run()

//{

//t.setPriority(Thread.MIN_PRIORITY);

while(true)

{

this.repaint();

try

{

//Thread.sleep(1000);

}

catch(Exception e)

{

}

}

}

public void paint(Graphics g)

{

for(int i=0;i<tracked;i++)

{

if(tracker.checkID(i,true))

donecount++;

}

Dimension d=this.getSize();

int w=d.width;

int h=d.height;

if(donecount==tracked)

{

Image i=image[current_img];

int iw=i.getWidth(null);

int ih=i.getHeight(null);

g.drawImage(i,50,100,100,100,this);

if(current_img>=tracked)

current_img=0;

}

else

{

int x=w*donecount/tracked;

g.setColor(Color.black);

g.fillRect(0,h/3,x,16);

g.setColor(Color.white);

g.fillRect(x,h/3,w-x,16);

g.setColor(Color.black);

try

{

Thread.sleep(1000);

}

catch(Exception e)

{

}

}

}

}

public class Image6

{

public static void main(String args[])

{

SampleJFrame samplejframe=new SampleJFrame();

}

}

qUesT_foR_knOwLeDgea at 2007-7-9 21:15:16 > top of Java-index,Desktop,Core GUI APIs...
# 6

import java.awt.*;

import javax.swing.*;

import java.awt.image.*;

class SampleJFrame extends JFrame implements ImageObserver

{

Image image;

public SampleJFrame(String title)

{

super(title);

this.setSize(500,500);

ImageIcon imageicon=new ImageIcon("G:/java files1/images/aa.jpg");

SampleThread samplethread=new SampleThread(this);

image=imageicon.getImage();

image=image.getScaledInstance(497,497,Image.SCALE_SMOOTH);

}

public void paint(Graphics g)

{

g.drawImage(image,0,30,this);

}

}

class SampleThread implements Runnable , ImageObserver

{

SampleJFrame samplejframe;

public SampleThread(SampleJFrame samplejframe)

{

this.samplejframe=samplejframe;

Thread t=new Thread(this);

t.start();

}

public void run()

{

boolean imageUpdate(Image image,int flags,int x,int y,int w,int h)

{

if((flags & ALLBITS) == 0)

{

System.out.println("Still processing the image");

//this.repaint();

return false;

}

else

{

System.out.println("Image Processed");

return true;

}

}

}

}

public class Image5 extends JFrame

{

public static void main(String args[])

{

SampleJFrame jframe=new SampleJFrame("Frame For A Image");

jframe.setVisible(true);

}

}

qUesT_foR_knOwLeDgea at 2007-7-9 21:15:16 > top of Java-index,Desktop,Core GUI APIs...