Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

GUI application to move and resize a rectangle using java

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.

Original configuration, and while mouse button is not held down:
GUI application to move and resize rectangle usiing java

During a drag of top or left side:
GUI application to move and resize rectangle and change color to green usiing java

During a drag of bottom or right side:
GUI application to move rectangle usiing java

Answer:

Coordinate Class


public class Coordinate {
	private int x;
	private int y;

	public Coordinate(int x, int y){
		this.x = x;
		this.y = y;
	}

	public int getX(){
		return x;
	}

	public int getY(){
		return y;
	}

	// Method distance calculates the distance from the current object to point
	public double distance(Coordinate point)
	   {
	     double dist;
	     dist = Math.sqrt((double) ((point.getX() - x)*(point.getX() - x) +
	                      (point.getY() - y)*(point.getY() - y)));
	     return dist;
	   }

	public Coordinate add(Coordinate anotherPosition){
		int newXValue, newYValue;
		newXValue = this. x + anotherPosition.x;
		newYValue = this. y + anotherPosition.y;
		return new Coordinate(newXValue, newYValue);
	}

	public Coordinate difference(Coordinate anotherPosition){
		int newXValue, newYValue;
		newXValue = this. x - anotherPosition.x;
		newYValue = this. y - anotherPosition.y;
		return new Coordinate(newXValue, newYValue);
	}
}


Main Class


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main  extends JFrame{
	 /* This is the object that will keep information about a Rectangle and support all services needed*/
	MyRectangle rectangle = new MyRectangle();
	public Main(){
		super("Resizable rectangle");
		setSize(400, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addMouseListener( new MouseAdapter(){
			public void mousePressed(MouseEvent e ){
				Coordinate currentMousePosition;
				/* This will extract the position where the event occurred*/
				int x, y;
				x = e.getX();
				y = e.getY();
				currentMousePosition = new Coordinate(x, y);
				/* This will send a message to rectangle to handle the event*/
				rectangle. processMousePressedEvent(currentMousePosition);
				repaint();
			}
			/* when the left mouse button is released*/
			public void mouseReleased(MouseEvent e ){ 
				Coordinate currentMousePosition;
				int x, y;
				x = e.getX();
				y = e.getY();
				currentMousePosition = new Coordinate(x, y);
				rectangle. processMouseReleasedEvent(currentMousePosition);
				repaint();
			}
		}
		);

		addMouseMotionListener( new MouseMotionAdapter(){
			public void mouseDragged( MouseEvent e ){ 
				int x, y;
				Coordinate currentMousePosition;
				x = e.getX();
				y = e.getY();
				currentMousePosition = new Coordinate(x, y);
				rectangle. processMouseDraggedEvent(currentMousePosition);
				repaint();
			}
		}
		); 
		
		setVisible(true); 
	}
	
	/* called to display the Rectangle*/
	public void paint(Graphics g){ 
		super.paint(g);
		/*this makes the rectangle draw itself... see the MyRectangle class for this method*/
		rectangle. draw(g); 
		
	}
	public static void main(String args[]){
		new Main();
	}
} 


MyRectangle Class


import java.awt.Color;
import java.awt.Graphics;
public class MyRectangle {
	Coordinate topLeftCorner; 
	Coordinate lastMousePosition;
	int width, height;
	Color RectangleColor;
	int topLeftX, topLeftY;
	boolean RectangleIsSelectedForMoving = false; 
	boolean bottomSelected = false;
	boolean rightSelected = false;
	
	/* This is the initial definition of the rectangle*/
	public MyRectangle(){
		topLeftCorner = new Coordinate(50, 50); 
		width = 100;
		height = 100;
		RectangleColor = Color.RED;
	}
	
	/* called when the user drags the mouse*/
	public void processMouseDraggedEvent(Coordinate currentMousePosition){
		if (RectangleIsSelectedForMoving){
			/* Find new position of Rectangle*/
			updateRectanglePosition(currentMousePosition); 
		} else if (bottomSelected){
			updateRectangleHeight(currentMousePosition); 
		} else if (rightSelected){
			updateRectangleWidth (currentMousePosition);
		}
	}
	
	/* This method will be called when the user releases the left mouse button*/
    public void processMouseReleasedEvent(Coordinate currentMousePosition){
		//change the color back to red */
    	RectangleColor = Color.RED;		
		RectangleIsSelectedForMoving = false;             
		bottomSelected = false;
		rightSelected = false;
    }
	/* will be called when the user presses the left mouse button*/
	public void processMousePressedEvent(Coordinate currentMousePosition){
		lastMousePosition = currentMousePosition;
		if (RectangleIsSelected(lastMousePosition)){ 
			RectangleIsSelectedForMoving = true;     
			RectangleColor = Color.GREEN;           
		}
		else if (onBottom(lastMousePosition)){ 
			bottomSelected = true;            
			                                      
			RectangleColor = Color.BLUE;             
		}
		else if (onRight(lastMousePosition)){
			rightSelected = true;
			RectangleColor = Color.BLUE;
		}
	}
	
	/* This method is called when the user selects the Rectangle*/
	private void updateRectanglePosition(Coordinate newMousePosition){
		/* x value and the y value of the new topLeftCorner of the Rectangle*/                                                         
		int newXValue, newYValue; 
		newXValue = topLeftCorner.getX() + newMousePosition.getX() - lastMousePosition.getX();
		newYValue = topLeftCorner.getY() + newMousePosition.getY() - lastMousePosition.getY();
		/*new position of topLeftCorner of Rectangle*/
		topLeftCorner = new Coordinate(newXValue, newYValue); 
		lastMousePosition = newMousePosition; 
	}
	
	/*This method is used to modify the size of Rectangle*/
	private void updateRectangleHeight(Coordinate newMousePosition){
		int newYValue;
		newYValue = newMousePosition.getY();
		height = Math.abs ((int) (topLeftCorner.getY() - newYValue));
	}
	
	/*This method is used to modify the size of Rectangle*/
	private void updateRectangleWidth(Coordinate newMousePosition){
		int newXValue;
		newXValue = newMousePosition.getX();
		width = Math.abs((int) (topLeftCorner.getX() - newXValue));
	}
	private boolean  RectangleIsSelected(Coordinate point){
		double Distance;
		Distance = Math.sqrt((double) (topLeftCorner.getX() + width/2 - point.getX())*(topLeftCorner.getX() + width / 2 - point.getX())
				+ (topLeftCorner.getY() + height / 2 - point.getY()) * (topLeftCorner.getY() + height / 2 - point.getY()));
	if (Distance < 10.0 && Distance > 0.0) {
			return true;
		}
		else {
			return false;
		}
	}
	 
	 /* method is called to check if the user has pressed mouse button*/
	private boolean onBottom(Coordinate point){ 
		if ((point.getY() < (topLeftCorner.getY() + height + 5)) && (point.getY() > (topLeftCorner.getY() + height - 5 ))
			&& (point.getX() <  (topLeftCorner.getX() + width + 5)) && (point.getX() > (topLeftCorner.getX() - width - 5))){
					return true;
		} else{
			return false;
		}
	}

	private boolean onRight (Coordinate point){
		if ((point.getX() > (topLeftCorner.getX() + width - 5)) && (point.getX() < (topLeftCorner.getX() + width + 5))
				&& (point.getY() < (topLeftCorner.getY() + height + 5)) && (point.getY() > (topLeftCorner.getY() - height - 5))){
			return true;
		}
		else return false;
	}
	
	/*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*/
	public void draw(Graphics g){	
		g.setColor(RectangleColor);
		g.drawRect(topLeftCorner.getX() , topLeftCorner.getY() , width, height);
	}

}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!