Tuesday, August 24, 2010

[Java] need help please

 

Hi,
can anyone tell me how will I put a loaded image in a JPanel?

This is my code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Itsura extends JFrame
{
// Variables declaration
//JFrame f = new JFrame("Character Recognition");

private JPanel contentPane;
private JPanel lalagynPanel;
private JPanel inputImagePanel;
private JPanel wordsPanel;
private JPanel echosPanel;
private JPanel inImagePanel;
private JPanel genImagePanel;

private JLabel imageInLabel;
private JLabel jLabel3;
private JLabel echosLabel;

// Menu variables declarations
private JMenuBar mb = new JMenuBar(); // Menu bar
private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar
private JMenu mnuFile1 = new JMenu("Help");
private JMenuItem mnuItemSelect = new JMenuItem("Load Image");
private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item

// File chooser declarations
static JFileChooser chooser = new JFileChooser();
static Component parent;

// Other variable declarations
static int flag = 0; // Initialize value

// End of variables declaration

public Itsura(String title)
{
super();
this.setJMenuBar(mb); // Create a Menu Bar

// Add Menu items to form
mb.add(mnuFile); // Add FILE
mb.add(mnuFile1); // Add HELP

// Add Menu sub items to menu
mnuFile.add(mnuItemSelect); // Create Select File Line
mnuFile.add(mnuItemQuit); // Create Quit line

// Construct the GUI
initializeComponent();

// Allows the app to be closed
mnuItemQuit.addActionListener(new ListenMenuQuit());

//Allows the app to select a file
mnuItemSelect.addActionListener(new ListenMenuSelect());

this.setVisible(true);
}

public class ListenMenuSelect implements ActionListener{
public void actionPerformed(ActionEvent e){
BufferedImage testimage;
String str = "";

try {
testimage = ImageIO.read(new File(str = chooseFile()));
System.out.println("File Name: " + str);
System.out.println("Flag Val: " + flag);
if(flag == 1)
{
// Draw image1 = new Draw(testimage, inImagePanel);
// Draw.showImage(image1,str, inImagePanel);

}

} catch (Exception e1) {
e1.printStackTrace();
}
}

}



public static String chooseFile(){
// Open a dialog box
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION){
flag =1;
return chooser.getSelectedFile().getAbsolutePath();
}
else
{
//returns null if nothing is chosen
return null;
}
}
public class ListenMenuQuit implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}

public class ListenCloseWdw extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}

// Constructor for the GUI
private void initializeComponent()
{
contentPane = (JPanel)this.getContentPane();

lalagynPanel = new JPanel();
inputImagePanel = new JPanel();
echosPanel = new JPanel();
inImagePanel = new JPanel();
genImagePanel = new JPanel();
wordsPanel = new JPanel();

imageInLabel = new JLabel();
jLabel3 = new JLabel();
echosLabel = new JLabel();

// PINAKA-LABAS NA PANEL
contentPane.setLayout(new GridLayout(1, 1, 5, 10));
contentPane.add(lalagynPanel, 0);

// LALAGYAN NG SUBPANELS
lalagynPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
lalagynPanel.add(wordsPanel, 0);
lalagynPanel.add(inputImagePanel, 1);
lalagynPanel.add(echosPanel, 2);
lalagynPanel.setBorder(BorderFactory.createRaisedBevelBorder());

// INPUT IMAGE LABEL
imageInLabel.setText("INPUT IMAGE");
imageInLabel.setMaximumSize(new Dimension(256, 30));
imageInLabel.setMinimumSize(new Dimension(256, 30));
imageInLabel.setPreferredSize(new Dimension(256, 30));

// LALAGYAN NG IMAGE OUTER PANEL
inputImagePanel.setLayout(null);
inputImagePanel.setBorder(BorderFactory.createEtchedBorder());
inputImagePanel.setPreferredSize(new Dimension(200, 350));
addComponent(inputImagePanel, imageInLabel, 75,2,100,35);
addComponent(inputImagePanel, inImagePanel, 7,40,183,300);

// WORDS LABEL
jLabel3.setText("STATUS:");
wordsPanel.setLayout(null);
wordsPanel.setBorder(BorderFactory.createEtchedBorder());
wordsPanel.setPreferredSize(new Dimension(150, 350));
addComponent(wordsPanel, jLabel3, 8,-2,77,44);


echosLabel.setText("GENERATED IMAGE");
echosPanel.setLayout(null);
echosPanel.setBorder(BorderFactory.createEtchedBorder());
echosPanel.setPreferredSize(new Dimension(200, 350));
addComponent(echosPanel, echosLabel, 55,2,180,41);
addComponent(echosPanel, genImagePanel, 7,40,183,300);

// inImagePanel
inImagePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
inImagePanel.setBorder(BorderFactory.createLoweredBevelBorder());
inImagePanel.setBackground(new Color(255, 255, 255));


// genImagePanel
genImagePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
genImagePanel.setBorder(BorderFactory.createLoweredBevelBorder());
genImagePanel.setBackground(new Color(255, 255, 255));

// Itsura
this.setTitle("Character Recognition");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(600, 425));
}


private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}

public static void main(String[] args)
{
Itsura gui = new Itsura("Itsura");
}

}

What I would like to do is to show the loaded image in the second column, but I can't figure out what to do.

What should I do? Any help will be appreciated. Thanks!

__._,_.___
Recent Activity:
Visit http://aiaiai.com or http://jgame.org for more groups to join.
Java Official Group is created for the following topics: Java 2 Enterprise Edition - J2EE, Java 2 Standard Edition - J2SE, Java 2 Micro Edition - J2ME, XML, XSL, XSD, XPATH, Web Services, Jini, JXTA for all type of Java Geeks.
Whoever posts spam / ads / job related message will be BANNED IMMEDIATELY
.

__,_._,___

No comments:

Post a Comment