Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old February 5th, 2004, 07:48 AM
essex_girl essex_girl is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 1 essex_girl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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();
	}
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Trouble with loading up an image!! HELP!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway