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!
/*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;
//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();
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();
/** * @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.
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.
/*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(); }
//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();
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();
/*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(); }
//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();
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();
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);
@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(); } }); } }
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 ?