Still having trouble. See comments Below is a question I asked and the answer I
ID: 3565922 • Letter: S
Question
Still having trouble. See comments
Below is a question I asked and the answer I got, which I totally appreciate.
I tried to run the program to see how it worked but I get the error on the line below. What would I need to do to fix this? I tried taking out the MouseListener but that made it worse.
public class ZoomLevel extends JPanel implements MouseListener, ActionListener {
Multiple markers at this line
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mousePressed(MouseEvent)
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mouseClicked(MouseEvent)
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mouseReleased(MouseEvent)
- The serializable class ZoomLevel does not declare a static final serialVersionUID field of type long
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mouseExited(MouseEvent)
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mouseEntered(MouseEvent)
_
___________________________Previous 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.
I got this answer: (And thanks, by the way)
/**
* @(#)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;
}
}
Explanation / Answer
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mousePressed(MouseEvent)
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mouseClicked(MouseEvent)
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mouseReleased(MouseEvent)
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mouseExited(MouseEvent)
- The type ZoomLevel must implement the inherited abstract method
MouseListener.mouseEntered(MouseEvent)
An Inteface is a collection of abstract methods. Since all methods are abstract, whenever we implement an Interface, we have to define all the methods contained in that Interface. Interface only contains the list of actions(methods) available and it is up to the class that implements it that how they want to perform that method.
Thus in this problem all unimplemented methods of the MouseListener Interface need to be defined in the ZoomLevel class. Even if the method is not supposed to perform any task, still it needs to be included in the class definition (as an empty method - method with no statements).
These methods need to be added to the ZoomLevel class with whatever action you need to perform for each method.
- The serializable class ZoomLevel does not declare a static final serialVersionUID field of type long
Serialization is what you do to an instance of an object if you want to dump it to a raw buffer, save it to disk, transport it in a binary stream (e.g., sending an object over a network socket) etc.
If you have no intention of serializing your class, you can add the annotation just above your class @SuppressWarnings("serial"). This will supress the above warning.
Or you can add this code in your class to supply the version UID:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.