PDA

View Full Version : How do I load an image into a GUI?


nanite51
12-05-2007, 11:40 PM
I'm building a model bank program using GUIs and I was wondering, how do I load and display an image onto a GUI and how do I position it onto where on the GUI screen I want?

nanite51
12-07-2007, 02:15 PM
Here is the code to my main GUI which is where I am trying to load the image. I want to try to get it like in a JPanel or JFrame or try to get it to load as the background image of the GUI


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import sun.audio.*;

public class MainBankGUI extends JFrame{

private JFrame self;

private BufferedImage mainImage;

private static final int xMainImage = 400;
private static final int yMainImage = 111;
private static final String mainImageFileName = "aperturesciencelogo.jpg";
private static final String audioFileName = "baked.wav";

private JButton createAccountButton;
private JButton depositMoneyButton;
private JButton withdrawMoneyButton;
private JButton transferMoneyButton;
private JButton closeAccountButton;
private JButton viewAccountButton;
private JButton exitButton;

public MainBankGUI()
{
self = this;
importAccounts();
playOpeningAudio();
createMainInterface();
importMainImage(xMainImage,yMainImage,mainImageFileName);
}

private void playOpeningAudio()
{
try
{
InputStream in = new FileInputStream(audioFileName);
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
}
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "Audio File Not Found. Try reading the right file!", "Error Reading Audio File - File Not Found", JOptionPane.ERROR_MESSAGE);
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null, "Audio File can't be read for some weird reason", "I/O Error Reading Audio File", JOptionPane.ERROR_MESSAGE);
}
}

//******IMAGE CODE STARTS HERE********

private void importMainImage(int xImage,int yImage,String fileName)
{
try
{
mainImage = ImageIO.read(new File(fileName));
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Try checking the right image file your noob!", "I/O Error Reading Image File", JOptionPane.ERROR_MESSAGE);
}

BufferedImage mainImage = new BufferedImage(xImage,yImage,BufferedImage.TYPE_INT_ARGB);

Graphics2D g2 = mainImage.createGraphics();

}

//******IMAGE CODE ENDS HERE************

private void createMainInterface()
{
Container cMain = getContentPane();
cMain.setLayout(null);
cMain.setBackground(new Color(0,0,0));

//make create account button
createAccountButton = new JButton();
createAccountButton.setText("Create Account");
createAccountButton.setBounds(15, 15, 140, 24);
cMain.add(createAccountButton);

//make deposit money button
depositMoneyButton = new JButton();
depositMoneyButton.setText("Deposit Money");
depositMoneyButton.setBounds(15, 65, 140, 24);
cMain.add(depositMoneyButton);

//make withdrawl money button
withdrawMoneyButton = new JButton();
withdrawMoneyButton.setText("Withdraw Money");
withdrawMoneyButton.setBounds(15, 115, 140, 24);
cMain.add(withdrawMoneyButton);

//make transfer money button
transferMoneyButton = new JButton();
transferMoneyButton.setText("Transfer Money");
transferMoneyButton.setBounds(15, 165, 140, 24);
cMain.add(transferMoneyButton);

//make close account button
closeAccountButton = new JButton();
closeAccountButton.setText("Close Account");
closeAccountButton.setBounds(15, 215, 140, 24);
cMain.add(closeAccountButton);

//make view account info button
viewAccountButton = new JButton();
viewAccountButton.setText("View Account Info");
viewAccountButton.setBounds(15, 265, 140, 24);
cMain.add(viewAccountButton);

//make exit button
exitButton = new JButton();
exitButton.setText("Exit");
exitButton.setBounds(15, 315, 140, 24);
cMain.add(exitButton);

createAccountButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{

CreateAccountGUI makeAccount = new CreateAccountGUI(self);
setVisible(false);
}

}
);

depositMoneyButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
DepositGUI depositAccount = new DepositGUI(self);
setVisible(false);
}

}
);

withdrawMoneyButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
WithdrawGUI withdrawAccount = new WithdrawGUI(self);
setVisible(false);
}

}
);

transferMoneyButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
TransferGUI transferMoney = new TransferGUI(self);
setVisible(false);
}

}
);

closeAccountButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
CloseAccountGUI closingAccount = new CloseAccountGUI(self);
setVisible(false);
}

}
);

viewAccountButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
ViewAccountInfoGUI viewAccounts = new ViewAccountInfoGUI(self);
setVisible(false);
}

}
);

exitButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
exitButtonActionPerformed( event );
}

}
);

setTitle("GLaDOS Banking System Version 1.03");
setSize(700,400);
setVisible(true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}

/*private void importMainImage(int x,int y,String fileName)
{
ImageIcon icon = new ImageIcon(fileName);
Image mainImage = icon.getImage();
}*/

private void importAccounts()
{
try
{
BufferedReader inputStream = new BufferedReader(new FileReader("account_index.txt"));

String line = "";

while ((line = inputStream.readLine()) != null)
{
String[] tokens = line.split(" ");

String accType = tokens[0];
//System.out.println(accType.equals("c")+accType);
String name = tokens[1];
String ssn = tokens[2];
String sAmount = tokens[3];
Double amount = Double.parseDouble(sAmount);

if(accType.equals("c"))
{
Checking temp = new Checking(name,ssn,amount);
Storage.checkingAccounts.addElement(temp);
}
else if(accType.equals("s"))
{
Savings temp = new Savings(name,ssn,amount);
Storage.savingsAccounts.addElement(temp);
}
else if(accType.equals("r"))
{
Retirement temp = new Retirement(name,ssn,amount);
Storage.retirementAccounts.addElement(temp);
}
}

inputStream.close();
}
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "Account File Not Found. Try reading the right file!", "Error Reading File - Account File Not Found", JOptionPane.ERROR_MESSAGE);
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null, "Try checking the right file your noob!", "I/O Error Reading Account File", JOptionPane.ERROR_MESSAGE);
}
}

private void exitButtonActionPerformed(ActionEvent event )
{
try
{
PrintWriter outputStream = new PrintWriter(new FileOutputStream("account_index.txt"));

for(int i=0;i<Storage.checkingAccounts.size();i++)
{
outputStream.println("c" + " " + Storage.checkingAccounts.elementAt(i).getOwnerName()
+ " " + Storage.checkingAccounts.elementAt(i).getSSN() + " "
+ Storage.checkingAccounts.elementAt(i).getAmount());
}

for(int i=0;i<Storage.savingsAccounts.size();i++)
{
outputStream.println("s" + " " + Storage.savingsAccounts.elementAt(i).getOwnerName()
+ " " + Storage.savingsAccounts.elementAt(i).getSSN() + " "
+ Storage.savingsAccounts.elementAt(i).getAmount());
}

for(int i=0;i<Storage.retirementAccounts.size();i++)
{
outputStream.println("r" + " " + Storage.retirementAccounts.elementAt(i).getOwnerName()
+ " " + Storage.retirementAccounts.elementAt(i).getSSN() + " "
+ Storage.retirementAccounts.elementAt(i).getAmount());
}

outputStream.close();
}
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "Try checking the right file your noob!. Try checking the right file!", "File Not Found", JOptionPane.ERROR_MESSAGE);
}

System.exit(0);
}

public static void main(String[] args)
{
MainBankGUI bankProgram = new MainBankGUI();
}

}