Adding an Image to a GUI

This is my code and I need to add an image to the top. The code works

Page 1 of 1

13 Replies - 5956 Views - Last Post: 16 June 2008 - 03:26 PM Rate Topic: -----

#1 jozilla76   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-June 08

Adding an Image to a GUI

Post icon  Posted 15 June 2008 - 11:33 AM

package inventory5;

import java.text.NumberFormat;

/*Inventory5.java
*Adding a button to the GUI that allows the user to move to the first item,
* previous item, next itme, and last item. Company logo added.
*/

/**
* @author Jodi Guppy
*/
public class Inventory5 extends javax.swing.JFrame {

public static SubProduct inventory[] = new SubProduct[6];//an array to store 6 different products
public int inventoryIndex = 0;

private double calculateEntireInventoryValue() {
double temp = 0.0;
for (int j=0; j<=5; j++) {
temp = temp + inventory[j].getProdValue();
}
return temp;
}

//Function for changing order of two products in product array
private void changePositionInArray(int i, int j) {
SubProduct p = inventory[i];//temporary variable to hold product in first position
inventory[i] = inventory[j];
inventory[j] = p;
} //end changePositionInArray method

private void sort() {
for (int i=0; i<=inventory.length - 2; i++) {//Go through all products in array
Product tempProduct = inventory[i +1];//Hold this product in a temporary variable
int position = i + 1;
for (int j=i+2; j<=inventory.length - 1; j++) {//Go throgh all remained products to find minimum of them
if (inventory[j].getProdName().compareTo(tempProduct.getProdName()) < 0) {
tempProduct = inventory[j];
position = j;
}
}//end for (int j...
if (tempProduct.getProdName().compareTo(inventory[i].getProdName()) < 0) {
//If found mimum one is less then first one, change position of them
changePositionInArray(i, position);
}
}//end for (int i...)
}//end sort method

/** Creates new form Inventory1 */
public Inventory5() {
initComponents();

//Create instances of Product
SubProduct product = new SubProduct("Office Paper", 1, 5.45, 600, "Size");
inventory[0] = product;
product = new SubProduct("Table", 2, 45.60, 8, "Length");
inventory[1] = product;
product = new SubProduct("Scanner", 3, 95.50, 3, "Brand");
inventory[2] = product;
product = new SubProduct("Printer", 4, 150.00, 3, "Type");
inventory[3] = product;
product = new SubProduct("PC Station", 5, 240.00, 4, "Speed");
inventory[4] = product;
product = new SubProduct("Library", 6, 120.00, 2, "Capacity");
inventory[5] = product;

//Sort inventory
sort();

printInventoryDetails();
}

public void printInventoryDetails() {
//NumberFormat object to format fload values properly
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);


//Print product detail information to output
jlInventory.add("Item Number: " + inventory[inventoryIndex].getProdNumber()); //product number
jlInventory.add("Item Name: " + inventory[inventoryIndex].getProdName()); //product name
jlInventory.add("Price: " + nf.format(inventory[inventoryIndex].getProdPrice())); //product price
jlInventory.add("Quantity: " + inventory[inventoryIndex].getProdQuantity()); //product quantity in stock
jlInventory.add("Additional Item: " + inventory[inventoryIndex].getItem());//product additional item
jlInventory.add("Restocking Fee: " + nf.format(inventory[inventoryIndex].getRestockingFee()));//restocking fee
jlInventory.add("Product's Value: " + nf.format(inventory[inventoryIndex].inventoryValue())); //product inventory value

jlInventory.add("Inventory Value: " + nf.format(calculateEntireInventoryValue())); //entire inventory value
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
jbNext = new javax.swing.JButton();
jbClose = new javax.swing.JButton();
jlInventory = new java.awt.List();
jbPrev = new javax.swing.JButton();
jbFirst = new javax.swing.JButton();
jbLast = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

jButton1.setText("jButton1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Inventory");
setResizable(false);
jbNext.setText("Next");
jbNext.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbNextMouseClicked(evt);
}
});

jbClose.setText("Close");
jbClose.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbCloseMouseClicked(evt);
}
});

jbPrev.setText("Prev");
jbPrev.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbPrevMouseClicked(evt);
}
});

jbFirst.setText("First");
jbFirst.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbFirstMouseClicked(evt);
}
});

jbLast.setText("Last");
jbLast.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbLastMouseClicked(evt);
}
});

jButton2.setIcon(new javax.swing.ImageIcon("C:\\inventory.gif"));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jbFirst)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbPrev)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbNext)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbLast)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbClose))
.addComponent(jlInventory, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jlInventory, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbClose)
.addComponent(jbNext)
.addComponent(jbPrev)
.addComponent(jbLast)
.addComponent(jbFirst))
.addContainerGap())
);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();
setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
}// </editor-fold>//GEN-END:initComponents

private void jbLastMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbLastMouseClicked
jlInventory.removeAll();
inventoryIndex = 4;
printInventoryDetails();
}//GEN-LAST:event_jbLastMouseClicked

private void jbFirstMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbFirstMouseClicked
jlInventory.removeAll();
inventoryIndex = 0;
printInventoryDetails();
}//GEN-LAST:event_jbFirstMouseClicked

private void jbPrevMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbPrevMouseClicked
jlInventory.removeAll();
inventoryIndex--;
if (inventoryIndex == -1)
inventoryIndex = 4;
printInventoryDetails();
}//GEN-LAST:event_jbPrevMouseClicked

private void jbNextMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbNextMouseClicked
jlInventory.removeAll();
inventoryIndex++;
if (inventoryIndex == 5)
inventoryIndex = 0;
printInventoryDetails();
}//GEN-LAST:event_jbNextMouseClicked

private void jbCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbCloseMouseClicked
System.exit(0);
}//GEN-LAST:event_jbCloseMouseClicked

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Inventory5().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jbClose;
private javax.swing.JButton jbFirst;
private javax.swing.JButton jbLast;
private javax.swing.JButton jbNext;
private javax.swing.JButton jbPrev;
private java.awt.List jlInventory;
// End of variables declaration//GEN-END:variables

}


I need to add a company logo to the GUI using java graphics classes and can not figure it out. I was thinking of using an image with a url. Please help.

Is This A Good Question/Topic? 0
  • +

Replies To: Adding an Image to a GUI

#2 nick2price   User is offline

  • D.I.C Lover
  • member icon

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

Re: Adding an Image to a GUI

Posted 15 June 2008 - 11:49 AM

Here is a way you can add an image from a url:
public class image extends JPanel
{
  private static final String NET_URL_PATH = "http://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Mooring_bollard_at_sunset%2C_Lyme_Regis.jpg/800px-Mooring_bollard_at_sunset%2C_Lyme_Regis.jpg";
  // Photo by Michael Maggs, Wikimedia Commons: http://commons.wikimedia.org/wiki/Image:Mooring_bollard_at_sunset%2C_Lyme_Regis.jpg 
  private BufferedImage image = null;
 
  public image ()
  {
	try
	{
	  // first load the image
	  image = ImageIO.read(new URL(NET_URL_PATH));
	}
	catch (IOException e)
	{
	  e.printStackTrace();
	}

  protected void paintComponent(Graphics g)
  {
	super.paintComponent(g);
	if (image != null)
	{
	  g.drawImage(image, 0, 0, this);
	}
  }
}


This at the moment is set to a background image. If you want to use an image which is saved in your project folder, set your filepath
private static final String FILE_PATH = "myImageFile.jpg"; // !! change this



and load your image like
image = ImageIO.read(new File(FILE_PATH));



If you want to just add an image as a a logo like you say, it might be best to just create a panel and add the image to this and then place this at the top of your application.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding an Image to a GUI

Posted 15 June 2008 - 04:35 PM

Sorry
don't really understand what your problem is
Was This Post Helpful? 0
  • +
  • -

#4 jozilla76   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-June 08

Re: Adding an Image to a GUI

Posted 15 June 2008 - 04:54 PM

I am getting illegal start of expression at this point.

protected void paintComponent(Graphics g)
Was This Post Helpful? 0
  • +
  • -

#5 nick2price   User is offline

  • D.I.C Lover
  • member icon

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

Re: Adding an Image to a GUI

Posted 15 June 2008 - 05:26 PM

are you inporting all the correct libraries?
Was This Post Helpful? 0
  • +
  • -

#6 jozilla76   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-June 08

Re: Adding an Image to a GUI

Posted 15 June 2008 - 05:38 PM

As far as I know. This is what I have.

package inventory5;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.text.NumberFormat;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/*Inventory5.java
*Adding a button to the GUI that allows the user to move to the first item,
* previous item, next itme, and last item. Company logo added.
*/


/**
* @author Jodi Guppy
*/
public class Inventory5 extends javax.swing.JFrame {

public class image extends JPanel
{
private static final String NET_URL_PATH = "http://www.microsoft.com/middleeast/partner/isv/images/Inventory_logo.jpg";
private BufferedImage image = null;
public image ()
{
try
{
// first load the image
image = ImageIO.read(new URL(NET_URL_PATH));
}
catch (IOException e)
{
e.printStackTrace();
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (image != null)
{
g.drawImage(image, 0, 0, this);
}
}
}


public static SubProduct inventory[] = new SubProduct[6];//an array to store 6 different products
public int inventoryIndex = 0;

private double calculateEntireInventoryValue() {
double temp = 0.0;
for (int j=0; j<=5; j++) {
temp = temp + inventory[j].getProdValue();
}
return temp;
}

//Function for changing order of two products in product array
private void changePositionInArray(int i, int j) {
SubProduct p = inventory[i];//temporary variable to hold product in first position
inventory[i] = inventory[j];
inventory[j] = p;
} //end changePositionInArray method

private void sort() {
for (int i=0; i<=inventory.length - 2; i++) {//Go through all products in array
Product tempProduct = inventory[i +1];//Hold this product in a temporary variable
int position = i + 1;
for (int j=i+2; j<=inventory.length - 1; j++) {//Go throgh all remained products to find minimum of them
if (inventory[j].getProdName().compareTo(tempProduct.getProdName()) < 0) {
tempProduct = inventory[j];
position = j;
}
}//end for (int j...
if (tempProduct.getProdName().compareTo(inventory[i].getProdName()) < 0) {
//If found mimum one is less then first one, change position of them
changePositionInArray(i, position);
}
}//end for (int i...)
}//end sort method

/** Creates new form Inventory1 */
public Inventory5() {
initComponents();

//Create instances of Product
SubProduct product = new SubProduct("Office Paper", 1, 5.45, 600, "Size");
inventory[0] = product;
product = new SubProduct("Table", 2, 45.60, 8, "Length");
inventory[1] = product;
product = new SubProduct("Scanner", 3, 95.50, 3, "Brand");
inventory[2] = product;
product = new SubProduct("Printer", 4, 150.00, 3, "Type");
inventory[3] = product;
product = new SubProduct("PC Station", 5, 240.00, 4, "Speed");
inventory[4] = product;
product = new SubProduct("Library", 6, 120.00, 2, "Capacity");
inventory[5] = product;

//Sort inventory
sort();

printInventoryDetails();
}

public void printInventoryDetails() {
//NumberFormat object to format fload values properly
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);


//Print product detail information to output
jlInventory.add("Item Number: " + inventory[inventoryIndex].getProdNumber()); //product number
jlInventory.add("Item Name: " + inventory[inventoryIndex].getProdName()); //product name
jlInventory.add("Price: " + nf.format(inventory[inventoryIndex].getProdPrice())); //product price
jlInventory.add("Quantity: " + inventory[inventoryIndex].getProdQuantity()); //product quantity in stock
jlInventory.add("Additional Item: " + inventory[inventoryIndex].getItem());//product additional item
jlInventory.add("Restocking Fee: " + nf.format(inventory[inventoryIndex].getRestockingFee()));//restocking fee
jlInventory.add("Product's Value: " + nf.format(inventory[inventoryIndex].inventoryValue())); //product inventory value

jlInventory.add("Inventory Value: " + nf.format(calculateEntireInventoryValue())); //entire inventory value
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
jbNext = new javax.swing.JButton();
jbClose = new javax.swing.JButton();
jlInventory = new java.awt.List();
jbPrev = new javax.swing.JButton();
jbFirst = new javax.swing.JButton();
jbLast = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

jButton1.setText("jButton1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Inventory");
setResizable(false);
jbNext.setText("Next");
jbNext.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbNextMouseClicked(evt);
}
});

jbClose.setText("Close");
jbClose.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbCloseMouseClicked(evt);
}
});

jbPrev.setText("Prev");
jbPrev.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbPrevMouseClicked(evt);
}
});

jbFirst.setText("First");
jbFirst.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbFirstMouseClicked(evt);
}
});

jbLast.setText("Last");
jbLast.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbLastMouseClicked(evt);
}
});

jButton2.setIcon(new javax.swing.ImageIcon("C:\\inventory.gif"));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jbFirst)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbPrev)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbNext)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbLast)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbClose))
.addComponent(jlInventory, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jlInventory, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbClose)
.addComponent(jbNext)
.addComponent(jbPrev)
.addComponent(jbLast)
.addComponent(jbFirst))
.addContainerGap())
);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();
setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
}// </editor-fold>//GEN-END:initComponents

private void jbLastMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbLastMouseClicked
jlInventory.removeAll();
inventoryIndex = 4;
printInventoryDetails();
}//GEN-LAST:event_jbLastMouseClicked

private void jbFirstMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbFirstMouseClicked
jlInventory.removeAll();
inventoryIndex = 0;
printInventoryDetails();
}//GEN-LAST:event_jbFirstMouseClicked

private void jbPrevMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbPrevMouseClicked
jlInventory.removeAll();
inventoryIndex--;
if (inventoryIndex == -1)
inventoryIndex = 4;
printInventoryDetails();
}//GEN-LAST:event_jbPrevMouseClicked

private void jbNextMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbNextMouseClicked
jlInventory.removeAll();
inventoryIndex++;
if (inventoryIndex == 5)
inventoryIndex = 0;
printInventoryDetails();
}//GEN-LAST:event_jbNextMouseClicked

private void jbCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbCloseMouseClicked
System.exit(0);
}//GEN-LAST:event_jbCloseMouseClicked

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Inventory5().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jbClose;
private javax.swing.JButton jbFirst;
private javax.swing.JButton jbLast;
private javax.swing.JButton jbNext;
private javax.swing.JButton jbPrev;
private java.awt.List jlInventory;
// End of variables declaration//GEN-END:variables

}
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding an Image to a GUI

Posted 15 June 2008 - 05:42 PM

Use that :code:
to post your code
And please tell us exactly what your problem is
Was This Post Helpful? 0
  • +
  • -

#8 jozilla76   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-June 08

Re: Adding an Image to a GUI

Posted 15 June 2008 - 05:57 PM

package inventory5;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.text.NumberFormat;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/*Inventory5.java
*Adding a button to the GUI that allows the user to move to the first item, 
* previous item, next itme, and last item. Company logo added. 
*/


/**
* @author Jodi Guppy 
*/
public class Inventory5 extends javax.swing.JFrame {

public class image extends JPanel
{
private static final String NET_URL_PATH = "http://www.microsoft.com/middleeast/partner/isv/images/Inventory_logo.jpg";
private BufferedImage image = null;
public image ()
{
try
{
// first load the image
image = ImageIO.read(new URL(NET_URL_PATH));
}
catch (IOException e)
{
e.printStackTrace();
}

[color=#FF0000]protected void paintComponent(Graphics g)[/color]{
super.paintComponent(g);
if (image != null)
{
g.drawImage(image, 0, 0, this);
}
}
} 


public static SubProduct inventory[] = new SubProduct[6];//an array to store 6 different products
public int inventoryIndex = 0;

private double calculateEntireInventoryValue() {
double temp = 0.0;
for (int j=0; j<=5; j++) {
temp = temp + inventory[j].getProdValue();
}
return temp;
}

//Function for changing order of two products in product array
private void changePositionInArray(int i, int j) {
SubProduct p = inventory[i];//temporary variable to hold product in first position
inventory[i] = inventory[j];
inventory[j] = p;
} //end changePositionInArray method

private void sort() {
for (int i=0; i<=inventory.length - 2; i++) {//Go through all products in array
Product tempProduct = inventory[i +1];//Hold this product in a temporary variable
int position = i + 1;
for (int j=i+2; j<=inventory.length - 1; j++) {//Go throgh all remained products to find minimum of them
if (inventory[j].getProdName().compareTo(tempProduct.getProdName()) < 0) {
tempProduct = inventory[j];
position = j;
}
}//end for (int j...
if (tempProduct.getProdName().compareTo(inventory[i].getProdName()) < 0) {
//If found mimum one is less then first one, change position of them
changePositionInArray(i, position);
}
}//end for (int i...)
}//end sort method 

/** Creates new form Inventory1 */
public Inventory5() { 
initComponents();

//Create instances of Product
SubProduct product = new SubProduct("Office Paper", 1, 5.45, 600, "Size");
inventory[0] = product;
product = new SubProduct("Table", 2, 45.60, 8, "Length");
inventory[1] = product;
product = new SubProduct("Scanner", 3, 95.50, 3, "Brand");
inventory[2] = product;
product = new SubProduct("Printer", 4, 150.00, 3, "Type");
inventory[3] = product;
product = new SubProduct("PC Station", 5, 240.00, 4, "Speed"); 
inventory[4] = product;
product = new SubProduct("Library", 6, 120.00, 2, "Capacity");
inventory[5] = product;

//Sort inventory
sort();

printInventoryDetails();
}

public void printInventoryDetails() {
//NumberFormat object to format fload values properly
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);


//Print product detail information to output
jlInventory.add("Item Number: " + inventory[inventoryIndex].getProdNumber()); //product number
jlInventory.add("Item Name: " + inventory[inventoryIndex].getProdName()); //product name
jlInventory.add("Price: " + nf.format(inventory[inventoryIndex].getProdPrice())); //product price
jlInventory.add("Quantity: " + inventory[inventoryIndex].getProdQuantity()); //product quantity in stock
jlInventory.add("Additional Item: " + inventory[inventoryIndex].getItem());//product additional item
jlInventory.add("Restocking Fee: " + nf.format(inventory[inventoryIndex].getRestockingFee()));//restocking fee
jlInventory.add("Product's Value: " + nf.format(inventory[inventoryIndex].inventoryValue())); //product inventory value 

jlInventory.add("Inventory Value: " + nf.format(calculateEntireInventoryValue())); //entire inventory value 
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
jbNext = new javax.swing.JButton();
jbClose = new javax.swing.JButton();
jlInventory = new java.awt.List();
jbPrev = new javax.swing.JButton();
jbFirst = new javax.swing.JButton();
jbLast = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

jButton1.setText("jButton1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Inventory");
setResizable(false);
jbNext.setText("Next");
jbNext.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbNextMouseClicked(evt);
}
});

jbClose.setText("Close");
jbClose.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbCloseMouseClicked(evt);
}
});

jbPrev.setText("Prev");
jbPrev.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbPrevMouseClicked(evt);
}
});

jbFirst.setText("First");
jbFirst.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbFirstMouseClicked(evt);
}
});

jbLast.setText("Last");
jbLast.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbLastMouseClicked(evt);
}
});

jButton2.setIcon(new javax.swing.ImageIcon("C:\\inventory.gif"));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jbFirst)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbPrev)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbNext)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbLast)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbClose))
.addComponent(jlInventory, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jlInventory, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbClose)
.addComponent(jbNext)
.addComponent(jbPrev)
.addComponent(jbLast)
.addComponent(jbFirst))
.addContainerGap())
);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();
setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
}// </editor-fold>//GEN-END:initComponents

private void jbLastMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbLastMouseClicked
jlInventory.removeAll();
inventoryIndex = 4;
printInventoryDetails(); 
}//GEN-LAST:event_jbLastMouseClicked

private void jbFirstMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbFirstMouseClicked
jlInventory.removeAll();
inventoryIndex = 0;
printInventoryDetails(); 
}//GEN-LAST:event_jbFirstMouseClicked

private void jbPrevMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbPrevMouseClicked
jlInventory.removeAll();
inventoryIndex--;
if (inventoryIndex == -1)
inventoryIndex = 4;
printInventoryDetails();
}//GEN-LAST:event_jbPrevMouseClicked

private void jbNextMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbNextMouseClicked
jlInventory.removeAll();
inventoryIndex++;
if (inventoryIndex == 5)
inventoryIndex = 0;
printInventoryDetails();
}//GEN-LAST:event_jbNextMouseClicked

private void jbCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbCloseMouseClicked
System.exit(0);
}//GEN-LAST:event_jbCloseMouseClicked

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Inventory5().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jbClose;
private javax.swing.JButton jbFirst;
private javax.swing.JButton jbLast;
private javax.swing.JButton jbNext;
private javax.swing.JButton jbPrev;
private java.awt.List jlInventory;
// End of variables declaration//GEN-END:variables

}
 


I am getting illegal start of expression. I highlighted the line.
Was This Post Helpful? 0
  • +
  • -

#9 nick2price   User is offline

  • D.I.C Lover
  • member icon

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

Re: Adding an Image to a GUI

Posted 15 June 2008 - 06:04 PM

Let me show you a simple, complete background painting example. See how you do figuring out your error for yourself.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
 
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public class Assignment2 extends JPanel
{
  private static final String FILE_PATH = "myImageFile.jpg"; // !! change this
  private static final String NET_URL_PATH = "http://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Mooring_bollard_at_sunset%2C_Lyme_Regis.jpg/800px-Mooring_bollard_at_sunset%2C_Lyme_Regis.jpg";
  // Photo by Michael Maggs, Wikimedia Commons: http://commons.wikimedia.org/wiki/Image:Mooring_bollard_at_sunset%2C_Lyme_Regis.jpg 
  private BufferedImage image = null;
 
  public Assignment2()
  {
	try
	{
	  // first load the image
	  //image = ImageIO.read(new File(FILE_PATH));
	  image = ImageIO.read(new URL(NET_URL_PATH));
	}
	catch (IOException e)
	{
	  e.printStackTrace();
	}
	
	// then set up the JPanel.  Try to avoid using the "null" layout
	JLabel titleLabel = new JLabel("My Title Goes Here");
	titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 36));
	titleLabel.setForeground(Color.lightGray);
	JPanel titlePanel = new JPanel();
	titlePanel.setOpaque(false);
	titlePanel.add(titleLabel);
	
	setPreferredSize(new Dimension(800, 590));
	setLayout(new BorderLayout());
	add(titlePanel, BorderLayout.NORTH);
  }
  
  @Override  // here's where I paint my background:
  protected void paintComponent(Graphics g)
  {
	super.paintComponent(g);
	if (image != null)
	{
	  g.drawImage(image, 0, 0, this);
	}
  }
 
  private static void createAndShowUI()
  {
	// Then I only create my JFrame when I need it.  I don't override it.
	// This way, I can use this same code in a JApplet, or a JDialog, or whatever
	JFrame frame = new JFrame("Assignment2");
	frame.getContentPane().add(new Assignment2()); // and add the JPanel to the JFrame here
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.pack();
	frame.setLocationRelativeTo(null);
	frame.setVisible(true);
  }
 
  public static void main(String[] args)
  {
	java.awt.EventQueue.invokeLater(new Runnable()
	{
	  public void run()
	  {
		createAndShowUI();
	  }
	});
  }
}


Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding an Image to a GUI

Posted 15 June 2008 - 06:51 PM

paintComponent() is surely not protected but surely public
Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding an Image to a GUI

Posted 15 June 2008 - 08:53 PM

Cut & pasted your code
Very nice image of a .... whatever the name is in English... the thing you attach a boat to at in a port
So what is your problem ?
Was This Post Helpful? 0
  • +
  • -

#12 nick2price   User is offline

  • D.I.C Lover
  • member icon

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

Re: Adding an Image to a GUI

Posted 16 June 2008 - 04:42 AM

Its not my problem, i was just showing the original poster how to load an image into their application. I thought i would give back to the community.
Was This Post Helpful? 0
  • +
  • -

#13 mensahero   User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 680
  • Joined: 26-May 08

Re: Adding an Image to a GUI

Posted 16 June 2008 - 05:13 AM

public image ()
{									 <<<<< public image open
try
{									  <<<<< try open
// first load the image
image = ImageIO.read(new URL(NET_URL_PATH));
}									 <<<<<< try close
catch (IOException e)
{								   <<<< catch open
e.printStackTrace();
}									<<<< catch close

========<<<   you forgot a closing bracket here for the public image >==========

protected void paintComponent(Graphics g){
super.paintComponent(g);



See the code. Maybe that's why your having an error.
Was This Post Helpful? 0
  • +
  • -

#14 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding an Image to a GUI

Posted 16 June 2008 - 03:26 PM

no need to overload paintComponent() if the only thing it does is calling its super
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1