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

Stick to the template which is provided on below and just change the //TODO part

ID: 3572254 • 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

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;

   private Point prevPt;

   private ImageIcon image;

   private boolean grabbed = false;

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

       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) {

      

   }

   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) {

          

       }

       // If mouse released and image was grabbed

       // 1. reset grabbed to false

       // 2. reset image to "smiley"

       // 3. repaint

       public void mouseReleased(MouseEvent e) {

          

       }

   }

   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) {

          

       }

   }

   public void paintComponent(Graphics g) {

       super.paintComponent(g);

       image.paintIcon(this, g, (int) imageCorner.getX(), (int) imageCorner.getY());

   }

}

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