13 Replies - 358 Views - Last Post: 29 July 2014 - 03:02 PM Rate Topic: -----

#1 quinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-July 14

Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 11:51 AM

Working on an assignment for a class: Create a DESKTOP application that allows you to open an image file and enables you to zoom in on or out from an image. I believe all I need is too create an open button to open the actual image file, instead of the html link, this code is an example that the teacher provided.

I'm new to Java so do you have any suggestions on how to do that.

My current code:


import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import java.net.URL;
 import java.io.*;
 import javax.imageio.*;

public class ZoomImage_V2 extends JPanel implements MouseListener, ActionListener {

private ImagePanel m_imagePanel;
private JScrollPane m_srollPane;
private JPanel m_imageContainer;
private JLabel m_zoomedInfo;
private JButton m_zoomInButton;
private JButton m_zoomOutButton;
private JButton m_originalButton;
private Cursor m_zoomCursor;   
private double m_zoomPercentage = 10.00;

private ImagePanel ip;

private String pathName = "http://files.myopera.com/kckbestfriendforever/albums/810068/iam-blue-wallpaper.jpg";
private Image img;

protected JButton b1, b2, b3;

public ZoomImage_V2() {

   this.setLayout(new BorderLayout());

   try {
       URL url = new URL(pathName);
       img = ImageIO.read(url);
   } catch (IOException e) {
   }      

   ip = new ImagePanel();
   ip.setImage(img, m_zoomPercentage);

   b1 = new JButton("Zoom In");
   b1.setActionCommand("zoomin");
   b2 = new JButton("Original");
   b2.setActionCommand("original");
   b3 = new JButton("Zoom Out");
   b3.setActionCommand("zoomout");

   JPanel buttonPanel = new JPanel(new FlowLayout());
   buttonPanel.add(b1);
   buttonPanel.add(b2);
   buttonPanel.add(b3);

   b1.addActionListener(this);
   b2.addActionListener(this);
   b3.addActionListener(this);

   m_imageContainer = new JPanel(new FlowLayout(FlowLayout.CENTER));
   m_imageContainer.setBackground(Color.BLACK);
   m_imageContainer.add(ip);

    m_srollPane = new JScrollPane(m_imageContainer);
    m_srollPane.setAutoscrolls(true);

   this.add(m_srollPane, BorderLayout.CENTER);
   this.add(buttonPanel, BorderLayout.PAGE_END);
}


public static void main(String[] args) {

   // TODO, add your application code
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });      

}

/**
 * Create the GUI and show it. For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Zoom In - Zoom Out - Original Image.");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(new ZoomImage_V2(), BorderLayout.CENTER);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}  

/**
 * Action Listener method taking care of
 * actions on the buttons
 */
public void actionPerformed(ActionEvent ae)
{
   if ("zoomin".equals(ae.getActionCommand())) {
       ip.zoomIn();
        m_imageContainer.doLayout();      
        m_srollPane.doLayout();
       //repaint();
   }
   if ("original".equals(ae.getActionCommand())) {
       ip.originalSize();
        m_imageContainer.doLayout();      
        m_srollPane.doLayout();
       //repaint();
   }
   if ("zoomout".equals(ae.getActionCommand())) {
       ip.zoomOut();
        m_imageContainer.doLayout();      
        m_srollPane.doLayout();
       //repaint();
   }              
}

/**
 * This method handles mouse clicks
 */
public void mouseClicked(MouseEvent e)
{

}

public void mouseEntered(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{

}

public void mousePressed(MouseEvent e)
{

}

public void mouseReleased(MouseEvent e)
{

}       

}




I guess I'm having trouble figuring out how to add an open file button so I can search for the file and then open it into my program.

Thank you for any help

Is This A Good Question/Topic? 0
  • +

Replies To: Java Applet: Open, Zoom In & Out Of Image

#2 ben255  Icon User is online

  • D.I.C Head

Reputation: 20
  • Posts: 249
  • Joined: 09-September 13

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 12:28 PM

JFileChooser to search files and for zooming and rotating and all that use matrix transformations
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2717
  • View blog
  • Posts: 11,428
  • Joined: 20-September 08

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 12:35 PM

Add a standard JMenu and as mentioned, use JFileChooser
Was This Post Helpful? 0
  • +
  • -

#4 quinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-July 14

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 12:38 PM

View Postben255, on 29 July 2014 - 12:28 PM, said:

JFileChooser to search files and for zooming and rotating and all that use matrix transformations



I'm a newbie to this, could you explain to me how to set up the JFileChooser?

I would appreciate it
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2717
  • View blog
  • Posts: 11,428
  • Joined: 20-September 08

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 12:40 PM

http://docs.oracle.c...ilechooser.html
Was This Post Helpful? 0
  • +
  • -

#6 quinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-July 14

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 12:43 PM

View Postg00se, on 29 July 2014 - 12:35 PM, said:

Add a standard JMenu and as mentioned, use JFileChooser


Something like this? where would I put it with the file?


    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Choose a file");
    this.getContentPane().add(fileChooser);
    fileChooser.setVisible(true);
  }

  public static void main(String[] args) {
    JFrame frame = new Zoom();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.pack();
    frame.setVisible(true);
  }
}




Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2717
  • View blog
  • Posts: 11,428
  • Joined: 20-September 08

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 12:46 PM

From a menu

http://docs.oracle.c...nents/menu.html
Was This Post Helpful? 0
  • +
  • -

#8 quinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-July 14

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 12:46 PM

Very basic question but how would I start the file? and what do I want to put after the JFileChooser?
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2717
  • View blog
  • Posts: 11,428
  • Joined: 20-September 08

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 12:48 PM

Read the docs i just posted - they tell you how
Was This Post Helpful? 0
  • +
  • -

#10 quinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-July 14

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 12:49 PM

View Postg00se, on 29 July 2014 - 12:46 PM, said:



Start of my program, where would i go from here?
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class Zoom3 extends JFrame {
  public Zoom () {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Choose a file");
    this.getContentPane().add(fileChooser);
    fileChooser.setVisible(true);
  }

  public static void main(String[] args) {
    JFrame frame = new Zoom ();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.pack();
    frame.setVisible(true);
  }
}



Was This Post Helpful? 0
  • +
  • -

#11 quinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-July 14

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 01:04 PM

View Postg00se, on 29 July 2014 - 12:48 PM, said:

Read the docs i just posted - they tell you how


I have but I'm very confused
Was This Post Helpful? 0
  • +
  • -

#12 ben255  Icon User is online

  • D.I.C Head

Reputation: 20
  • Posts: 249
  • Joined: 09-September 13

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 01:19 PM

add a actionlistener to the jfilechooser, doubleclick a file and its chosen. and do a
File file = JFileChooser.getFileSource();
Was This Post Helpful? 0
  • +
  • -

#13 quinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-July 14

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 01:44 PM

View Postben255, on 29 July 2014 - 01:19 PM, said:

add a actionlistener to the jfilechooser, doubleclick a file and its chosen. and do a
File file = JFileChooser.getFileSource();


Do you have an example of how to do that? Here is what I currently have


import javax.swing.JFileChooser;
import java.io.File;
public class Zoom3

public static void main(String[] args) {
       JFileChooser chooser = new JFileChooser();

       chooser.ShowOpenDialog(null);

        }



}






Was This Post Helpful? 0
  • +
  • -

#14 ben255  Icon User is online

  • D.I.C Head

Reputation: 20
  • Posts: 249
  • Joined: 09-September 13

Re: Java Applet: Open, Zoom In & Out Of Image

Posted 29 July 2014 - 03:02 PM

public class Foo extends JFrame{
	JFileChooser choose;
	File file;
	Foo(){
		choose = new JFileChooser();
		choose.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				file = choose.getSelectedFile();
				System.out.println(file.getAbsolutePath());
			}
		});
		this.setSize(500, 500);
		this.setVisible(true);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.add(choose);
	}
	public static void main(String[] args){
		new Foo();
	}
}


also, youtube is a great source of information for stuff like this

This post has been edited by ben255: 29 July 2014 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1