Inventory5 Adding a Image

Need help with adding a logo to my GUI

Page 1 of 1

2 Replies - 1112 Views - Last Post: 17 January 2009 - 11:09 PM Rate Topic: -----

#1 eQu1pD   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-January 09

Inventory5 Adding a Image

Posted 17 January 2009 - 09:57 PM

I need help with trying to figure why my logo will not display. I have tried every type of code examples and now it will not even compile. Here is my last compile error:
Inventory5.java:328: variable label might not have been initialized
}
^

Here is my code, I labled the section in my GUI procedure where I put the logo and attached the logo file

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.ImageIcon;


public class Inventory5 {

	public static void main(String[] args) {
	   
		System.out.println("Classic Sci-Fi Movies List");
		System.out.println();

		Movie dvd = null;
		Inventory inventory = new Inventory();

	 	dvd = new Movie(1, "Forbidden Planet", 5, 10.99, 1956);
		inventory.addMovie(dvd);
		System.out.println(dvd);
		  
		dvd = new Movie(22, "2001 - A Spcae Odyssey", 10, 10.99);
		inventory.addMovie(dvd);
		System.out.println(dvd);  
		  
		dvd = new Movie(44, "Metropolis", 3, 27.99);	
		inventory.addMovie(dvd);
		System.out.println(dvd);
		  
		dvd = new Movie(5, "Planets of the Apes", 3, 9.99, 1968 );
		inventory.addMovie(dvd);
		System.out.println(dvd);

		inventory.printValue();
		new InventoryGUI(inventory);
	}

}

/*********************************************************
DVD class constructor and foramts output

***********************************************************/

class DVD {
	private int itemNum;
	private String title;
	private int stockQuty;
	private double unitPrice;
   

	DVD(int itemNum, String title, int stockQuty, double unitPrice) {
		this.itemNum = itemNum;
		this.title = title;
		this.stockQuty = stockQuty;
		this.unitPrice = unitPrice;
	}

	public int getItemNo() { return itemNum; }
	public String getTitle() { return title; }
	public int getInStock() { return stockQuty; }
	public double getUnitPrice() { return unitPrice; }

	public double value() {
		return stockQuty * unitPrice;
	}

	
	public String toString() {
	   
		return String.format("Item Number:%3d   DVD Name:%-20s \t   Units in Stock:%3d   Price:$%.2f \t  Value:$%.2f \n",
							 itemNum, title, stockQuty, unitPrice, value());
	}

} // end DVD



/*******************************************************
Inventory class creates array and total inventory
value.
*******************************************************/
class Inventory {

	private final int INVENTORY_SIZE = 30;
	private Movie[] items;
	private int numItems;

	Inventory() {
		items = new Movie[INVENTORY_SIZE];
		numItems = 0;
	}

	public int getNumItems() {
		return numItems;
	}

	public DVD getDVD(int n) {
		return items[n];
	}

	public void addMovie(Movie item) {
		items[numItems] = item;
		++numItems;
	}

	public double value() {
		double sumOfInventory = 0.0;

		for (int i = 0; i < numItems; i++)sumOfInventory += items[i].value();
		return sumOfInventory;
	}

	public void printValue() {
	

		if (numItems <= 0) {
			System.out.println("Inventory is empty.\n");
		} 
		
	
		System.out.printf("\nTotal value in inventory is $%,.2f\n\n", value());
		
	}
	
}



/***************************************************************
class Movie extends DVD class to include release year for Movie
and calculate resocking fee.
***************************************************************/

class Movie extends DVD {

	private int movieYear;

	public Movie(int MovieID, String itemName, int quantityOnHand, double itemPrice, int year) {
		
		super(MovieID, itemName, quantityOnHand, itemPrice);

		this.movieYear = movieYear;
	}
   
	 public Movie(int MovieID, String itemName, int quantityOnHand, double itemPrice) {

		super(MovieID, itemName, quantityOnHand, itemPrice);
		this.movieYear = 0;
	}
	
	
	
	public void setYear(int year) {
		movieYear = year;
	}

	public int getMovieYear() {
		return movieYear;
	}

   
	public double value() {
		return super.value() + restockingFee();
	}

	public double restockingFee() {
		return super.value() * 0.05f;
	}

}


/****************************************************
GUI method

*****************************************************/

class InventoryGUI extends JFrame	{

	private Inventory theInventory;

	private int index = 0;

	private final JLabel itemNumberLabel = new JLabel(" Item Number:");
	private JTextField itemNumberText;

	private final JLabel prodnameLabel = new JLabel(" Product Name:");
	private JTextField prodnameText;

	private final JLabel prodpriceLabel = new JLabel(" Price:");
	private JTextField prodpriceText;

	private final JLabel numinstockLabel = new JLabel(" Number in Stock:");
	private JTextField numinstockText;

	private final JLabel valueLabel = new JLabel(" Value:");
	private JTextField valueText;

	private final JLabel restockingFeeLabel = new JLabel(" RestockingFee:");
	private JTextField restockingFeeText;

	private final JLabel totalValueLabel = new JLabel(" Inventory TotalValue:");
	private JTextField totalValueText;

	private JPanel centerPanel;
	private JPanel buttonPanel;
	
	final JLabel label; // logo
	

	InventoryGUI(Inventory inventory) {
		super("Classic Sci-Fi Movie Inventory");
		final Dimension dim = new Dimension(140, 20);
		final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
		JPanel jp;

		theInventory = inventory;

		centerPanel = new JPanel();
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
		
		
		jp = new JPanel(flo);
		itemNumberLabel.setPreferredSize(dim);
		jp.add(itemNumberLabel);
		itemNumberText = new JTextField(3);
		itemNumberText.setEditable(false);
		jp.add(itemNumberText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		prodnameLabel.setPreferredSize(dim);
		jp.add(prodnameLabel);
		prodnameText = new JTextField(17);
		prodnameText.setEditable(false);
		jp.add(prodnameText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		prodpriceLabel.setPreferredSize(dim);
		jp.add(prodpriceLabel);
		prodpriceText = new JTextField(17);
		prodpriceText.setEditable(false);
		jp.add(prodpriceText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		numinstockLabel.setPreferredSize(dim);
		jp.add(numinstockLabel);
		numinstockText = new JTextField(5);
		numinstockText.setEditable(false);
		jp.add(numinstockText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		restockingFeeLabel.setPreferredSize(dim);
		jp.add(restockingFeeLabel);
		restockingFeeText = new JTextField(17);
		restockingFeeText.setEditable(false);
		jp.add(restockingFeeText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		valueLabel.setPreferredSize(dim);
		jp.add(valueLabel);
		valueText = new JTextField(17);
		valueText.setEditable(false);
		jp.add(valueText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		totalValueLabel.setPreferredSize(dim);
		jp.add(totalValueLabel);
		totalValueText = new JTextField(17);
		totalValueText.setEditable(false);
		jp.add(totalValueText);
		centerPanel.add(jp);
		
		
		
		buttonPanel = new JPanel();
		JButton nextButton = new JButton("Next");
		nextButton.addActionListener(new NextButtonHandler());
		buttonPanel.add(nextButton);
		centerPanel.add(buttonPanel);
		
		
		buttonPanel = new JPanel();
		JButton prevButton = new JButton("Prev");
		prevButton.addActionListener(new PrevButtonHandler());
		buttonPanel.add(prevButton);
		centerPanel.add(buttonPanel);

/**********************
code for adding logo
***********************/

		JLabel jLabel = new JLabel();
		jLabel.setIcon(new ImageIcon(InventoryGUI.class.getResource("Buafflo.jpg")));		
		
		setContentPane(centerPanel);
		
		repaintGUI();
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(400, 350);
		setResizable(false);
		setLocationRelativeTo(null);
		setVisible(true);
		}
		
		public void repaintGUI() {
			Movie temp = (Movie) theInventory.getDVD(index);
		
			if (temp != null) {
				itemNumberText.setText("" + temp.getItemNo());
				prodnameText.setText(temp.getTitle());
				prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice()));
				restockingFeeText.setText(String.format("$%.2f", temp.restockingFee()));
				numinstockText.setText("" + temp.getInStock());
				valueText.setText(String.format("$%.2f", temp.value()));
			}
			totalValueText.setText(String.format("$%.2f", theInventory.value()));
		}
		
		class NextButtonHandler implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				int numItems = theInventory.getNumItems();
					index = (++index) % numItems;
					repaintGUI();
				}
		}		
				
		class PrevButtonHandler implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				int numItems = theInventory.getNumItems();
					if (index == 0)
						index = 4;
					index = (--index) % numItems;
					repaintGUI();
				}

			}
	
}



Attached ImageAttached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Inventory5 Adding a Image

#2 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Inventory5 Adding a Image

Posted 17 January 2009 - 10:20 PM

I dont see where u add your image to your centerPanel. Why dont you just assign your image simply to an ImageIcon and then just add this to your panel
private ImageIcon logo= new ImageIcon("mainPageLogo.jpg");


It also depends how you would want to display your image. If you want it set as a background image, you would want to override the panels paint component.
Was This Post Helpful? 0
  • +
  • -

#3 eQu1pD   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-January 09

Re: Inventory5 Adding a Image

Posted 17 January 2009 - 11:09 PM

View Postnick2price, on 17 Jan, 2009 - 09:20 PM, said:

I dont see where u add your image to your centerPanel. Why dont you just assign your image simply to an ImageIcon and then just add this to your panel
private ImageIcon logo= new ImageIcon("mainPageLogo.jpg");


It also depends how you would want to display your image. If you want it set as a background image, you would want to override the panels paint component.



Thanks nick2prics,


I have added the code and now my program complies and runs, but the image is not showing on the GUI. have attached my inventoryGUI class:

/****************************************************
GUI method

*****************************************************/

class InventoryGUI extends JFrame	{

	private Inventory theInventory;

	private int index = 0;

	private final JLabel itemNumberLabel = new JLabel(" Item Number:");
	private JTextField itemNumberText;

	private final JLabel prodnameLabel = new JLabel(" Product Name:");
	private JTextField prodnameText;

	private final JLabel prodpriceLabel = new JLabel(" Price:");
	private JTextField prodpriceText;

	private final JLabel numinstockLabel = new JLabel(" Number in Stock:");
	private JTextField numinstockText;

	private final JLabel valueLabel = new JLabel(" Value:");
	private JTextField valueText;

	private final JLabel restockingFeeLabel = new JLabel(" RestockingFee:");
	private JTextField restockingFeeText;

	private final JLabel totalValueLabel = new JLabel(" Inventory TotalValue:");
	private JTextField totalValueText;

	private JPanel centerPanel;
	private JPanel buttonPanel;
	
	private ImageIcon logo= new ImageIcon("Buafflo.jpg");	
	
	private JLabel labelLogo;

	InventoryGUI(Inventory inventory) {
		super("Classic Sci-Fi Movie Inventory");
		final Dimension dim = new Dimension(140, 20);
		final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
		JPanel jp;

		theInventory = inventory;

		centerPanel = new JPanel();
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
		
		

	
		// logo code
		labelLogo = new JLabel();
		labelLogo.setIcon(logo);
		add(labelLogo);
		
		
		
		jp = new JPanel(flo);
		itemNumberLabel.setPreferredSize(dim);
		jp.add(itemNumberLabel);
		itemNumberText = new JTextField(3);
		itemNumberText.setEditable(false);
		jp.add(itemNumberText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		prodnameLabel.setPreferredSize(dim);
		jp.add(prodnameLabel);
		prodnameText = new JTextField(17);
		prodnameText.setEditable(false);
		jp.add(prodnameText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		prodpriceLabel.setPreferredSize(dim);
		jp.add(prodpriceLabel);
		prodpriceText = new JTextField(17);
		prodpriceText.setEditable(false);
		jp.add(prodpriceText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		numinstockLabel.setPreferredSize(dim);
		jp.add(numinstockLabel);
		numinstockText = new JTextField(5);
		numinstockText.setEditable(false);
		jp.add(numinstockText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		restockingFeeLabel.setPreferredSize(dim);
		jp.add(restockingFeeLabel);
		restockingFeeText = new JTextField(17);
		restockingFeeText.setEditable(false);
		jp.add(restockingFeeText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		valueLabel.setPreferredSize(dim);
		jp.add(valueLabel);
		valueText = new JTextField(17);
		valueText.setEditable(false);
		jp.add(valueText);
		centerPanel.add(jp);
		
		jp = new JPanel(flo);
		totalValueLabel.setPreferredSize(dim);
		jp.add(totalValueLabel);
		totalValueText = new JTextField(17);
		totalValueText.setEditable(false);
		jp.add(totalValueText);
		centerPanel.add(jp);
		
		
		
		buttonPanel = new JPanel();
		JButton nextButton = new JButton("Next");
		nextButton.addActionListener(new NextButtonHandler());
		buttonPanel.add(nextButton);
		centerPanel.add(buttonPanel);
		
		
		buttonPanel = new JPanel();
		JButton prevButton = new JButton("Prev");
		prevButton.addActionListener(new PrevButtonHandler());
		buttonPanel.add(prevButton);
		centerPanel.add(buttonPanel);

		
		
		setContentPane(centerPanel);
		
		repaintGUI();
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(400, 350);
		setResizable(false);
		setLocationRelativeTo(null);
		setVisible(true);
		}
		
		public void repaintGUI() {
			Movie temp = (Movie) theInventory.getDVD(index);
		
			if (temp != null) {
				itemNumberText.setText("" + temp.getItemNo());
				prodnameText.setText(temp.getTitle());
				prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice()));
				restockingFeeText.setText(String.format("$%.2f", temp.restockingFee()));
				numinstockText.setText("" + temp.getInStock());
				valueText.setText(String.format("$%.2f", temp.value()));
			}
			totalValueText.setText(String.format("$%.2f", theInventory.value()));
		}
		
		class NextButtonHandler implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				int numItems = theInventory.getNumItems();
					index = (++index) % numItems;
					repaintGUI();
				}
		}		
				
		class PrevButtonHandler implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				int numItems = theInventory.getNumItems();
					if (index == 0)
						index = 4;
					index = (--index) % numItems;
					repaintGUI();
				}

			}
	

}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1