7 Replies - 9794 Views - Last Post: 04 December 2008 - 05:41 PM Rate Topic: -----

#1 Mark064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 08

Images in an Array

Post icon  Posted 04 December 2008 - 06:33 AM

Ok I'm having some troubles displaying images in an array then putting it in a GUI, according to my textbook it should be possible it just doesn't work for me anyway I do it.

 	ImageIcon[] image = {
			new ImageIcon("image/us.gif"),
			new ImageIcon("image/ca.gif"),
			new ImageIcon("image/india.gif"),
			new ImageIcon("image/uk.gif"),
			new ImageIcon("image/china.gif"),
			new ImageIcon("image/norway.gif")
		};
		
		add(new ImageViewer(image[2]));


ImageViewer is it's own class that I pulled out the textbook so there shouldn't be any problems with it as I've tried other Java methods of implementing it in the exact same way but get the same errors.

Is This A Good Question/Topic? 0
  • +

Replies To: Images in an Array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Images in an Array

Posted 04 December 2008 - 07:33 AM

Specific errors would be helpful.
Was This Post Helpful? 0
  • +
  • -

#3 Mark064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 08

Re: Images in an Array

Posted 04 December 2008 - 07:39 AM

Cannot find symbol
Was This Post Helpful? 0
  • +
  • -

#4 windra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-November 08

Re: Images in an Array

Posted 04 December 2008 - 07:44 AM

Cannot find symbol is usually a declaration error, are you trying to access a variable that has not been declared globally?
Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Images in an Array

Posted 04 December 2008 - 07:45 AM

cannot find symbol what??? and can you post the whole class please
Was This Post Helpful? 0
  • +
  • -

#6 Mark064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 08

Re: Images in an Array

Posted 04 December 2008 - 09:30 AM

Sorry about that here we go:

import javax.swing.*;
 import java.awt.*;

public class ImageText extends JFrame {
	
	public ImageText(){
		
		/*Image i = new ImageIcon("C:/us.gif").getImage();
	Image m = new ImageIcon("C:/ca.gif").getImage();
	Image a = new ImageIcon("C:/india.gif").getImage();
	Image g = new ImageIcon("C:/uk.gif").getImage();
	Image e = new ImageIcon("C:/china.gif").getImage();
	Image s = new ImageIcon("C:/norway.gif").getImage();*/
	  
	  ImageIcon[] image = {
			new ImageIcon("image/us.gif"),
			new ImageIcon("image/ca.gif"),
			new ImageIcon("image/india.gif"),
			new ImageIcon("image/uk.gif"),
			new ImageIcon("image/china.gif"),
			new ImageIcon("image/norway.gif")
		};
		
		add( ImageViewer(image[1]));
		
	}
	
	

	
  public static void main(String[] args) {
	  
	  
		
	ImageText frame = new ImageText();
		frame.setTitle("Six Flags");
		frame.setSize(400, 320);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		
   
  }
}


And just cause the the ImageViewer class.

import java.awt.*;
import javax.swing.*;

public class ImageViewer extends JPanel {
	
	private java.awt.Image image;
	private boolean stretched = true;
	private int xCoordinate;
	private int yCoordinate;
	
	public ImageViewer(Image image) {
		this.image = image;
	}
	
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		
		if(image != null)
			if (isStretched())
				g.drawImage(image, xCoordinate, yCoordinate,
					getSize().width, getSize().height, this);
			else
				g.drawImage(image, xCoordinate, yCoordinate, this);
	}
	
	public java.awt.Image getImage(){
		return image;
	}
	
	public void setImage (java.awt.Image image){
		this.image = image;
		repaint();
	}
	
	public boolean isStretched(){
		return stretched;
	}
	
	public void isStretched(boolean stretched){
		this.stretched = stretched;
		repaint();
	}
	
	public int getXCoordinate(){
		return xCoordinate;
	}
	
	public void setXCoordinate(int xCoordinate){
		this.yCoordinate = yCoordinate;
		repaint();
	}
	
	public int getYCoordinate(){
		return yCoordinate;
	}
	
	public void setYCoordinate(int yCoordinate){
		this.yCoordinate = yCoordinate;
		repaint();
	}
	
}


The output I get is exactly "cannot find symbol method ImageViewer (javax.swing.ImageIcon)". Of course if I declare the image normally I get no problem.

Doing the same thing in another program except using

cdirections.add(dirImg[1], BorderLayout.NORTH);


Produces a simulair error of "cannot find symbol method add (javax.swing.ImageIcon)"

This post has been edited by Mark064: 04 December 2008 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2715
  • View blog
  • Posts: 11,422
  • Joined: 20-September 08

Re: Images in an Array

Posted 04 December 2008 - 05:28 PM

Try instead

cdirections.add(new JLabel(dirImg[1]), BorderLayout.NORTH);



if that object in the array is an ImageIcon
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Images in an Array

Posted 04 December 2008 - 05:41 PM

You probably mean that

add( new ImageViewer(image[1]));

instead of

add(ImageViewer(image[1]));

And ImageViewer tries to put an ImageIcon into an Image... no ImageIcon does not extends Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1