Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 5th, 2004, 10:07 AM
Registered User
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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();
    }
}
 
Old February 5th, 2004, 08:39 PM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

//hello confused girl, try this:
// I have made some changes to your code
// I have marked it with the comments "this is new"
/*
 * ImageMaker.java
 *
 * Created on 6. februar 2004, 01:09
 */

/**
 *
 * @author Rune P.F. Fimland
 */
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;
    // this is new
    JDesktopPane desktop;

    public BufferedImage mBufferedImage;

    public ImageMaker(String fileName)
    {

        Container appWindow = getContentPane();
        setTitle(kBanner);
        //this is new
        desktop = new JDesktopPane();

        appWindow.setLayout(new BorderLayout(5,5));

        //this is new
        appWindow.add( desktop );
        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();
    }
}



 
Old February 5th, 2004, 09:59 PM
Registered User
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for that! :) It was helpful!
When i go to load an image though, the image appears covering the menu and toolbar? Anyway of placing it in the pane below so that it does not cover anything?

I was playing around with some code and i created the class below.
The class allows a user to select an image which is placed in a jlabel which is then placed in a scrollpane. What i want to do is load the image as a bufferedimage and then place this in the scrollpane. This is so that i can go on to manipulate the image later. I have tried my best and researched as much as i can but i simply just cannot get it to happen. I have placed the code below, could someone please assist me and show me how to do it. Would it be better to use your method or the method in the class i create?
Thanks again for your help!

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.*;
import javax.swing.plaf.basic.*;


public class ImageMaker extends JFrame implements ActionListener
{
    String kBanner = "ImageMaker v1.0";

    JPanel canvas = new JPanel();
    ImageIcon icon;

    JLabel lab;
    JScrollPane state_scroll;
    JButton openButton;
    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();

        openButton = new JButton("Open");
        openButton.addActionListener(this);
        openButton.setToolTipText("Click to load an image.");
        toolBar.add(openButton);



        state_scroll = new JScrollPane(canvas, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        state_scroll.setPreferredSize(new Dimension(100,100));

        appWindow.add(BorderLayout.CENTER, state_scroll);
        appWindow.add(BorderLayout.NORTH, toolBar);
    }


    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();



        //int width = getWidth();
        //int height = getHeight();
        //g2.drawImage(image, 0, 0, width, height, this);



        lab=new JLabel(new ImageIcon(image));
        canvas.add(lab);

        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);
            }
    }


    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();
    }
}
 
Old February 10th, 2004, 01:24 AM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this class works fine at my system, but I get many images side by side, but they are not covering the menubar

 
Old October 1st, 2004, 03:02 AM
Registered User
 
Join Date: Oct 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i got one problem in scaling an imge heare is my code

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.imageio.ImageIO;
import java.io.*;

public class aplet extends JFrame implements ItemListener,MouseListener
{
      JPanel panel1 = new JPanel();
      JComboBox box1= new JComboBox();
      JLabel label1;
      JLabel label2;
     static ImageIcon img;
      static ImageIcon img1;
    // static Image image = Toolkit.getDefaultToolkit().getImage("E:/Winter.gif");



    public aplet()
    {
        try{
            jbinit();
           }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
    public void jbinit()
    {
        this.getContentPane().setLayout(null);
        this.setSize(800,800);
        //img=new ImageIcon("F:/Water lilies.jpg");
        //img1=new ImageIcon("F:/Water lilies.jpg");
        label1 = new JLabel(img);
         label2 = new JLabel(img1);
        panel1.setLayout(null);
        panel1.setBounds(new Rectangle(0,0,800,800));

        panel1.add(label2);
        panel1.add(label1);
        label2.setBounds(300,300,100,100);
        label1.setBounds(0,0,700,700);
        box1.setBounds(new Rectangle(650,650,80,20));
        box1.addItem("Strech");
        box1.addItem("Center");
        panel1.add(box1);
        this.getContentPane().add(panel1);
        this.setVisible(true);
        label2.setVisible(false);
        box1.addItemListener(this);
        label1.addMouseListener(this);
        label2.addMouseListener(this);
    }

    public void Paint (Graphics g)
    {

        g=panel1.getGraphics();
        Graphics2D g2d=(Graphics2D) g;
        g2d.scale(0.2,0.2);


    }

    public void itemStateChanged(ItemEvent e)
    {
        if (box1.getSelectedIndex()==0)
        {
            label2.setVisible(false);
            label1.setVisible(true);

        }
        else if(box1.getSelectedIndex()==1)
           {
            label1.setVisible(false);
            label2.setVisible(true);
        }
    }





public void mousePressed(MouseEvent mpe)
{

}

public void mouseReleased(MouseEvent mre)
{

}

public void mouseEntered(MouseEvent mee)
{

}

public void mouseExited(MouseEvent meE)
{

}
public void mouseClicked(MouseEvent mce)
{
   new mouse();
}




    public static void main(String args[])
    {
        aplet a=new aplet();

    }




}



import javax.swing.*;
import javax.swing.JButton;
import java.awt.Rectangle;

public class mouse extends JFrame
{
    JPanel panel =new JPanel();
    //ImageIcon ico=new ImageIcon(aplet.img);
    JLabel label=new JLabel(aplet.img);
    JLabel label3=new JLabel(aplet.img1);
public mouse()
{
    try
    {
        jbinit();
    }
    catch(Exception e)
    {

    }

}
public void jbinit()
{
    this.setSize(300,300);
    panel.setBounds(new Rectangle(0,0,400,400));
    this.getContentPane().add(panel);
    label.setBounds(0,0,300,300);

    panel.add(label);

    this.setVisible(true);


}

}







Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading Image to Image Control using JavaScript ctranjith General .NET 1 May 28th, 2013 05:43 AM
IE7 keeps loading an image toshi HTML Code Clinic 1 May 13th, 2008 10:02 AM
program loading image? 3d_immortal C# 2005 1 May 12th, 2008 08:58 PM
image loading abaso.jadhav01 ASP.NET 2.0 Basics 0 October 28th, 2006 01:24 AM
Problems in Loading Image ctranjith General .NET 0 December 18th, 2004 06:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.