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());
}
}
// check whether "point" is within the (rectangular) boundary of
the image
// (use "imageCorner", width, and height)
boolean isClickInBound(Point point) {
if(point.x >= imageCorner.x && point.x
<=imageCorner.x + WIDTH) &&
(point.y >= imageCorner.y && point.y <= imageCorner.y
+ HEIGHT))
return true;
return false;
}
// 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) {
grabbed = true;
image = SCARED;
prevPt = new Point((int) e.getX(), (int)e.getY());
}
// If mouse released and image was grabbed
// 1. reset grabbed to false
// 2. reset image to "smiley"
// 3. repaint
public void mouseReleased(MouseEvent e) {
grabbed = false;
image = SMILEY;
}
// 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) {
imageCorner.x = imageCorner.x + ((int) e.getX() - prevPt.x);
imageCorner.y = imageCorner.y + ((int) e.getY() - prevPt.y);
prevPt = new Point((int) e.getX(), (int)e.getY());
}
Stick to the template which is provided on below and just change the //TODO part, please....
"Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import java.awt.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.applet.*; public class PolygonDrawer extends Applet implements MouseListener{ //Declares variables private int[] X, Y; private int ptCT; private final static Color polygonColor = Color.BLUE; //Color stuff public void init() { setBackground(Color.BLACK); addMouseListener(this); X = new int [450]; Y =...
Keep the ball in bounds by adding conditional statements after the TODO comments in the paintComponent() method to check when the ball has hit the edge of the window. When the ball reaches an edge, you will need to reverse the corresponding delta direction and position the ball back in bounds. Make sure that your solution still works when you resize the window. Your bounds checking should not depend on a specific window size This is the java code that...
JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...
The assignment is to take the current program that draws a line and to add 4 buttons at the bottom of the frame that will change the line drawn to a different color (ie. No button pressed draws a black line, Button labeled Red changes any new lines drawn to red and retains any previously colored lines, etc for all the buttons). This is the code I have so far but I'm having issues with the buttons. This is also...
In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...
Lab Description: Using graphics, polygons, and arrays, draw the tree shown below. You can change the tree anyway you like to make it your own. Sample Data Sec below Files Needed:: GraphicaRunner.java Tree.java Sample Output: import java.awt.Graphics; import java.awt.color; import java.awt.Polygon; import java.awt.Font; import java.awt.Canvas; public class Tree extends Canvas public Tree() setBackground (Color.WHITE) public void paint ( Graphics window) window. setColor (Color.RED); window.setFont (new Font("TAHOMA" ,Font.BOLD,12) window.drawstring("Lab14h Tree Lab", 50, 50) tree(window); IONaA , Font.BOL.D,12) public void tree(Graphics window)...
Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...
There 2 parts to complete: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Q4 extends JFrame { private int xPos, yPos; public Q4() { JPanel drawPanel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); // paint parent's background //complete this: } }; drawPanel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { //complete this :- set the xPos, yPos } }); setContentPane(drawPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Mouse-Click Demo"); setSize(400, 250); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() {...
Please help complete the items marked TODO in the code and get the tests to pass: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestPerformance { // TODO run test and record running times for SIZE = 10, 100, 1000, 10000, ... // (choose in conjunction with REPS below up to an upper limit where the clock // running time is in the tens of seconds) // TODO (optional) refactor to DRY // TODO...
Please help complete the items marked TODO in the code and get the tests to pass: Someone already answered it, but it was incorrect! import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestPerformance { // TODO run test and record running times for SIZE = 10, 100, 1000, 10000, ... // (choose in conjunction with REPS below up to an upper limit where the clock // running time is in the tens of seconds)...