Menu
support@authoritypapers.com
+1(805) 568 7317

in this lab you will create a gui application that allows a user to move and resize 5188429

In this lab, you will create a GUI application that allows a user to move and resize a rectangle on the screen. The rectangle is initially red. If the user drags the top or left side of the rectangle, the rectangle should turn green and move with the mouse cursor. Movement should stop when the mouse is released, and the rectangle should go back to its original red colour. If the user drags the bottom or right side of the rectangle, the rectangle should turn blue and be resized using the edge being dragged. You need not handle a case where the bottom edge is dragged through the top edge, or the right edge is dragged through the left edge. When the mouse is released, resizing should stop and the mouse should go back to its original red colour. I have provided some code to get you started.

Rectangle

import java.awt.*; import java.awt.event.*; public class MyRectangle { //You need some kind of tolerance, in pixels //…a +/- of the mouse position to determine if it's close enough to a given edge of the rectangle // Rectangle properties private int x; //the x for position private int y; //the y for position private int width; //the rectangle width private int height; //the rectangle height private Color color = Color.RED; // Previous coordinates, dimensions, and mouse positions //You will need to store the previous X and Y positions of the rectangle, as well as the height and width //You will also need the previous mouse position //This way, you can compute changes in these values and apply the necessary changes to the rectangle private boolean resizing = false; public MyRectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public void processMousePressed(MouseEvent e) { //fill this in… what shall we do when the mouse is initially pressed down? //you will also need to add this method call to your handler for mouse down events } public void processMouseReleased(MouseEvent e) { //fill this in as well… what shall we do when the mouse is released? //you will also need to add this method call to your handler for mouse released events } public void processMouseDragged(MouseEvent e) { //fill this in too… what shall we do as the mouse is being dragged around? //you will also need to add this method call to your handler for mouse dragged events } public void draw(Graphics g) { //this method receives the Graphics object of the JFrame and draws on it //this method simply draws the rectangle by setting its color and calling drawRect g.setColor(color); g.drawRect(x, y, width, height); //you should NOT need to add anything here… //in your “process” calls above, you will be modifying color, x, y, width, and height depending on the mouse position //in your handlers, you will be calling Main's repaint and the above “process” calls } //you may need other methods to handle stuff }

main

import javax.swing.*;

import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private int rectangleWidth; private int rectangleHeight; MyRectangle1 rectangle = new MyRectangle1(50, 50, 100, 100); public Main() { super(“Resizable rectangle”); setSize(400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add listeners here //mouse dragged, mouse released, etc. //you should be calling some methods in the rectangle object from here //see the “process” calls in rectangle setVisible(true); } public static void main(String[] args) { new Main(); } @Override public void paint(Graphics g) { super.paint(g); //this makes the rectangle draw itself… see the MyRectangle class for this method rectangle.draw(g); } }

"Order a similar paper and get 15% discount on your first order with us
Use the following coupon
"GET15"

Order Now