
February 5th, 2004, 07:48 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Trouble with loading up an image!! HELP!!
Hi,
With regards to the following code, I was wondering if anyone could help me. I am in the early stages of making a basic Image Editing program, and have managed to implement the following code. The thing is, when you load up an image, it seems to always position it in the top left corner of the container - overwiting all menus, buttons placed underneath. Why is this??
I need the image to appear in the JPanel/canvas below the toolbar and not on top of it!!
I hope someone out there can help me!
SOMEONE PLEASE HELP ME!
Thanks,
a very confused girl
xxx
Code:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import java.net.*;
import javax.swing.filechooser.*;
public class ImageMaker extends JFrame implements ActionListener
{
String kBanner = "ImageMaker v1.0";
JPanel contentPane;
JButton loadButton;
JToolBar toolBar;
JMenuBar menuBar;
JMenu fileMenu;
JMenuItem fileOpenMenuItem;
public BufferedImage mBufferedImage;
public ImageMaker(String fileName)
{
Container appWindow = getContentPane();
setTitle(kBanner);
appWindow.setLayout(new BorderLayout(5,5));
menuBar = new JMenuBar();
setJMenuBar(menuBar);
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
fileOpenMenuItem = new JMenuItem("Open");
fileMenu.add(fileOpenMenuItem);
fileOpenMenuItem.addActionListener(this);
toolBar = new JToolBar();
loadButton = new JButton("Open");
loadButton.addActionListener(this);
toolBar.add(loadButton);
contentPane = new JPanel();
appWindow.add( BorderLayout.NORTH, toolBar);
appWindow.add( BorderLayout.SOUTH, contentPane);
}
public void loadImage(String fileName)
{
Image image = Toolkit.getDefaultToolkit().getImage(fileName);
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try { mt.waitForID(0); }
catch (InterruptedException ie) { return; }
if (mt.isErrorID(0)) return;
mBufferedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = mBufferedImage.createGraphics();
g2.drawImage(image, null, null);
validate();
repaint();
setTitle(kBanner + ": " + fileName);
}
public void paint(Graphics g)
{
super.paint(g);
if (mBufferedImage == null) return;
Insets insets = getInsets();
g.drawImage(mBufferedImage, insets.left, insets.top, null);
}
public void actionPerformed(ActionEvent ae)
{
JFileChooser fd = new JFileChooser();
int returnVal=fd.showOpenDialog(ImageMaker.this);
if(returnVal==JFileChooser.APPROVE_OPTION)
{
if (fd.getSelectedFile() == null)
{ return; }
File file=fd.getSelectedFile();
String path=(String)file.getPath();
loadImage(path);
}
JMenu men = (JMenu) ae.getSource();
String label = (String) men.getText();
JButton butt = (JButton) ae.getSource();
String butt2 = (String) butt.getText();
if (label.equals("Open"))
{/*............*/}
else if (label.equals("Save"))
{/*............*/}
}//end ActionListener
public static void main(String[] args)
{
String fileName = "default";
if (args.length > 0) fileName = args[0];
ImageMaker img=new ImageMaker(fileName);
img.setSize(800,600);
img.show();
}
}
|