Welcome to Dream.In.Code
Become a Java Expert!

Join 150,068 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,808 people online right now. Registration is fast and FREE... Join Now!




Adding an Image to a GUI

2 Pages V  1 2 >  
Reply to this topicStart new topic

Adding an Image to a GUI, This is my code and I need to add an image to the top. The code works

jozilla76
15 Jun, 2008 - 10:33 AM
Post #1

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 4

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.
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Adding An Image To A GUI
15 Jun, 2008 - 10:49 AM
Post #2

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Here is a way you can add an image from a url:
CODE
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
CODE
private static final String FILE_PATH = "myImageFile.jpg"; // !! change this


and load your image like
CODE
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.
User is online!Profile CardPM
+Quote Post

pbl
RE: Adding An Image To A GUI
15 Jun, 2008 - 03:35 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Sorry
don't really understand what your problem is

User is online!Profile CardPM
+Quote Post

jozilla76
RE: Adding An Image To A GUI
15 Jun, 2008 - 03:54 PM
Post #4

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 4

I am getting illegal start of expression at this point.

protected void paintComponent(Graphics g)
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Adding An Image To A GUI
15 Jun, 2008 - 04:26 PM
Post #5

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
are you inporting all the correct libraries?
User is online!Profile CardPM
+Quote Post

jozilla76
RE: Adding An Image To A GUI
15 Jun, 2008 - 04:38 PM
Post #6

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 4

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

}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Adding An Image To A GUI
15 Jun, 2008 - 04:42 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Use that code.gif
to post your code
And please tell us exactly what your problem is
User is online!Profile CardPM
+Quote Post

jozilla76
RE: Adding An Image To A GUI
15 Jun, 2008 - 04:57 PM
Post #8

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 4

CODE

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.
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Adding An Image To A GUI
15 Jun, 2008 - 05:04 PM
Post #9

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Let me show you a simple, complete background painting example. See how you do figuring out your error for yourself.
CODE
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();
      }
    });
  }
}

User is online!Profile CardPM
+Quote Post

pbl
RE: Adding An Image To A GUI
15 Jun, 2008 - 05:51 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
paintComponent() is surely not protected but surely public
User is online!Profile CardPM
+Quote Post

pbl
RE: Adding An Image To A GUI
15 Jun, 2008 - 07:53 PM
Post #11

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
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 ?
User is online!Profile CardPM
+Quote Post

nick2price
RE: Adding An Image To A GUI
16 Jun, 2008 - 03:42 AM
Post #12

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
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.
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic