Stick to the template which is provided on below and just change the //TODO part
ID: 3572527 • Letter: S
Question
Stick to the template which is provided on below and just change the //TODO part, please.
Template
Lab13.java
package lab13;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
public class Lab13 {
public static void main (String[] args) {
JFrame frame = new SmileyFrame();
frame.add(new SmileyPanel());
frame.setVisible(true);
}
}
class SmileyFrame extends JFrame {
private static final long serialVersionUID = 7613378514394599117L;
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
public SmileyFrame () {
setTitle("Drag Smiley");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class SmileyPanel extends JPanel {
private static final long serialVersionUID = 6650015376897677969L;
private ImageIcon SMILEY = null;
private ImageIcon SCARED = null;
private final int WIDTH;
private final int HEIGHT;
private Point imageCorner; // image's top-left corner location
private Point prevPt; // mouse location for previous event
private ImageIcon image; // toggles between smiley and scared
private boolean grabbed = false; // whether the icon image is grabbed
public SmileyPanel() {
try {
SMILEY = new ImageIcon(ImageIO.read(getClass().getResource("smiley.gif")));
SCARED = new ImageIcon(ImageIO.read(getClass().getResource("scared.gif")));
}
catch(IOException e) {
System.out.println(e);
System.exit(1);
}
WIDTH = SMILEY.getIconWidth();
HEIGHT = SMILEY.getIconHeight();
image = SMILEY;
imageCorner = new Point(0, 0); // image starts at top left
ClickListener clickListener = new ClickListener();
DragListener dragListener = new DragListener();
this.addMouseListener(clickListener);
this.addMouseMotionListener(dragListener);
}
// check whether "point" is within the (rectangular) boundary of the image
// (use "imageCorner", width, and height)
boolean isClickInBound(Point point) {
// TODO
}
private class ClickListener extends MouseAdapter {
// If mouse is pressed within the boundary of the image,
// 1. set grabbed to true
// 2. change image to "scared"
// 3. save the mouse position
// 4. repaint
public void mousePressed(MouseEvent e) {
// TODO
}
// If mouse released and image was grabbed
// 1. reset grabbed to false
// 2. reset image to "smiley"
// 3. repaint
public void mouseReleased(MouseEvent e) {
// TODO
}
}
private class DragListener extends MouseMotionAdapter {
// If image was grabbed,
// 1. change "imageCorner" based on the difference between current mouse position and previous mouse position when the image was grabbed
// 2. set the current point as previous point
// 3. repaint
public void mouseDragged(MouseEvent e) {
// TODO
}
}
// Draw the window, including the updated image.
public void paintComponent(Graphics g) {
super.paintComponent(g);
image.paintIcon(this, g, (int) imageCorner.getX(), (int) imageCorner.getY());
}
}
Lab 13 November 30, 2016 Overview In this lab, you will implement the DragSmiley program similar to the one in the textbook. I have made some changes so that the frame and panel are separate from the driver. In addition, the image icons are loaded from the current directory. You will put the smiley and scared images in the same directory as your Java source file. I also refactored out a helper method isclickInBound to check whether a point falls within the boundary of the image icon. You will use this method in the listener methods that you will implement. 2 at to implement? You will implement isClickInBound method, mousePressed, mouseReleased, and mouse Dragged method, which are marked with TODO in the provided code template. Detailed instruction for what to implement is in the comments above each method. While the logic of the program is similar to the one in the textbook and you can consult the textbook for details, you should follow the instructions in the comme Specifically, the image icon will only turn to scared if the click is within the boundary of the icon. A video showing the expected behavior is also provided.Explanation / Answer
private class ClickListener extends MouseAdapter
{
// When mouse pressed, change to scared image.
public void mousePressed(MouseEvent e)
{
image = SCARED;
prevPt = e.getPoint(); // save current position
repaint();
} // end mousePressed
// When mouse released, return to smiley image.
public void mouseReleased(MouseEvent e)
{
image = SMILEY;
repaint();
} // end mouseReleased
}
private class DragListener extends MouseMotionAdapter
{
// Enable image to be dragged by mouse.
public void mouseDragged(MouseEvent e)
{
Point currentPt = e.getPoint(); // current position
// Make sure mouse was pressed within the image.
if (currentPt.getX() >= imageCorner.getX() &&
currentPt.getX() <= imageCorner.getX() + WIDTH &&
currentPt.getY() >= imageCorner.getY() &&
currentPt.getY() <= imageCorner.getY() + HEIGHT)
{
imageCorner.translate(
(int) (currentPt.getX() - prevPt.getX()),
(int) (currentPt.getY() - prevPt.getY()));
prevPt = currentPt; // save current position
repaint();
}
} // end mouseDragged
} // end class DragListener
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.