Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to create a java program that has 5 gui buttons: ( Open, 100%, Zoom in, Z

ID: 3565849 • Letter: I

Question

I need to create a java program that has 5 gui buttons: ( Open, 100%, Zoom in, Zoom out and Exit. ) I'm supposed to bring up an image and manipulate it with the buttons. I have figured out how to bring up an image successfully. I have a different program with gui buttons, but they don't really do much. The open button will ask for an image file and open it, but not within the panel with the buttons. I'm not sure how to zoom in and out or return to 100% and I don't understand how to get the image to change while still maintaining the button options.

Explanation / Answer

/**

* @(#)ZoomLevel.java

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.URL;

import java.io.*;

import javax.imageio.*;

//create a Panel ZoomLevel

public class ZoomLevel extends JPanel implements MouseListener, ActionListener {

   

     public ImageViewer imagePanel;

     public JScrollPane srollPane;

     public JPanel imageContainer;

     public JLabel zoomedInfo;

     public JButton zoomInButton;

     public JButton zoomOutButton;

     public JButton originalButton;

     public Cursor zoomCursor;   

     public double zoomPercentage = 10.00;

   

     static public ImageViewer ip = new ImageViewer();;

   

     public String pathName = "Y:/TrainningPeriod/image2/slide8.jpg";

     public Image img;

     public JFileChooser jfc;

     protected JButton b,b1, b2, b3, b4;

     public static JFrame frame = new JFrame();

     public ZoomLevel()

     {

          this.setLayout(new BorderLayout());

        

          try {

              ImageIcon image=new ImageIcon(pathName);

              URL url = new URL(pathName);

              img = image.getImage();

          } catch (IOException e) {

          }           

        

          ip.setImage(img, zoomPercentage);

          b = new JButton("Open");

          b.setActionCommand("open");

          b1 = new JButton("Zoom In");

          b1.setActionCommand("zoomin");

          b2 = new JButton("Original");

          b2.setActionCommand("original");

          b3 = new JButton("Zoom Out");

          b3.setActionCommand("zoomout");

          b4 = new JButton("Exit");

          b4.setActionCommand("exit");

          JPanel buttonPanel = new JPanel(new FlowLayout());

          buttonPanel.add(b);

          buttonPanel.add(b1);

          buttonPanel.add(b2);

          buttonPanel.add(b3);

          buttonPanel.add(b4);

          b.addActionListener(this);

          b1.addActionListener(this);

          b2.addActionListener(this);

          b3.addActionListener(this);

          b4.addActionListener(this);

        

          imageContainer = new JPanel(new FlowLayout(FlowLayout.CENTER));

        

          imageContainer.setBackground(Color.BLACK);

        

          imageContainer.add(ip);

        

          srollPane = new JScrollPane(imageContainer);

          srollPane.setAutoscrolls(true);

        

          this.add(srollPane, BorderLayout.CENTER);

          this.add(buttonPanel, BorderLayout.PAGE_END);

     }

   

   

     public static void main(String[] args)throws Exception

     {

        

          //add the 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();

              }

          });   

     }

   

     //createAndShowGUI method

     public static void createAndShowGUI() {

          //Create and set up the window.

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

          frame.setTitle("ImageZoomer");

            

          //Display the window

          frame.pack();

          frame.setVisible(true);

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     }  

   

     //Action Listener method to the buttons

     public void actionPerformed(ActionEvent ae)

     {

          if("open".equals(ae.getActionCommand()))

          {

              //JFileChooser class, using setter

              //methods, the values are set to access

              //the files

              jfc=new JFileChooser();

              jfc.setAcceptAllFileFilterUsed(true);

              //to set access to both files and

              //directories

              jfc.setFileSelectionMode(jfc.FILES_AND_DIRECTORIES);

              jfc.setAcceptAllFileFilterUsed(true);

              int rVal = jfc.showOpenDialog(null);

              if (rVal == JFileChooser.APPROVE_OPTION)

              {

                 

                   try {

                        pathName=jfc.getSelectedFile().toString();

                        URL url = new URL(pathName);

                        img = ImageIO.read(url);

                   } catch (IOException e) {

                   }       

                 

                   ip.setImage(img, zoomPercentage);

                   imageContainer.add(ip);

              }

          }

        

          if ("zoomin".equals(ae.getActionCommand())) {

              ip.zoomIn();

              imageContainer.doLayout();      

              srollPane.doLayout();

              repaint();

          }

          if ("original".equals(ae.getActionCommand())) {

              ip.originalSize();

              imageContainer.doLayout();      

              srollPane.doLayout();

              repaint();

          }

          if ("zoomout".equals(ae.getActionCommand())) {

              ip.zoomOut();

              imageContainer.doLayout();      

              srollPane.doLayout();

              repaint();

          }  

          if ("exit".equals(ae.getActionCommand()))

          {

              closeFrame();

          }

     }

     public void closeFrame()

     {

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     }

-----------------------------------------------------------------------------------

/**

* @(#)ImageViewer.java

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ImageViewer extends JPanel

{

     public double zoom = 1.0;

     public double zoomPercent;

     public Image my_image;

       //Constructor

     public ImageViewer()

     {

        

     }

     public ImageViewer(Image image, double zoomPercentage)

     {

          my_image = image;

          zoomPercentage = zoomPercentage / 100;

     }

   

     //setImage

     public void setImage(Image image, double zoomPercentage)

     {

          my_image = image;

          zoomPercentage = zoomPercentage / 100;   

     }

   

     //overridding the paint method of Graphics class

     public void paintComponent(Graphics g)

     {

          Graphics2D g2D = (Graphics2D)g;

        

          //set the background color to white

          g2D.setColor(Color.WHITE);

          //fill the rect

          g2D.fillRect(0, 0, getWidth(), getHeight());

        

          //scale the graphics to get the zoom effect

          g2D.scale(zoom, zoom);

        

          //draw the image

          g2D.drawImage(my_image, 0, 0, this);

     }

   

     //overridding the getPreferredSize()

     public Dimension getPreferredSize()

     {

          int width=(int)(my_image.getWidth(this) + (my_image.getWidth(this) * (zoom - 1)));

          int height = (int)(my_image.getHeight(this) + (my_image.getHeight(this) * (zoom -1 )));

     return new Dimension(width,height);

     }

   

     //set the Zoom Percentage

     public void setZoomPercentage(int zoomPercentage)

     {

          zoomPercent = ((double)zoomPercentage) / 100;  

     }  

     //sets the original of image to 100%

     public void originalSize()

     {

          zoom = 1;

     }

   

     //zoomIn method

     public void zoomIn()

     {

          zoom += zoomPercent;

     }          

   

     //zoomOut method

     public void zoomOut()

     {

          zoom -= zoomPercent;

          if(zoom < zoomPercent)

          {

              if(zoomPercent > 1.0)

              {

                   zoom = 1.0;

              }

              else

              {

                   zoomIn();

              }

          }

     }

     //getZoomedTo method

     public double getZoomedTo()

     {

          return zoom * 100;

     }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote