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

This need to be in JAVA. Selection The canvas should have a pointer of a single

ID: 3571930 • Letter: T

Question

This need to be in JAVA.

Selection


The canvas should have a pointer of a single selected shape at any one time. The selected shape should draw differently for now just draw a little "x" or something on the selected shape. When the mouse is clicked on the canvas, figure out if the click was on a shape, and set it to be the selected shape. A click on a spot where two or more shapes overlap should select the topmost shape. It's handy if new shapes are selected, but it's not a requirement.


You will need a method like shape.getBounds() to ask each shape its rectangle boundary. Internally, the shape does not know the rectangle, so it has to turn around and call something like model.getBounds(). Determining the selection gets data from the model, but does not change the model. The idea of the selection is a feature of the canvas (view), not the model.

Explanation / Answer

Please find below the java program to draw rectangle using the methods mentioned above in the problem:


import java.awt.Component;
import java.awt.Graphics;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.Image;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class GetBounds_Shape { //A Class GetBounds declared here
public static void main(String[] args) {
Frame frame = new Frame("GetBounds_Shape ");
frame.setSize(200, 100);
frame.add(new CanvasToDisplay()); //Canvas to display
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() { //addWindowListener
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

class CanvasToDisplay extends Component { //Class CanvastoDisplay declared
public void paint(Graphics g) { //public class called paint declared here
Graphics2D g2D = (Graphics2D) g; //Graphics2D decalared
Image image = Toolkit.getDefaultToolkit().getImage("example.gif");
// The below will create an oval shape which is as large as the component
float fx = 0; // type float declared here
float fy = 0;
float fw = getSize().width - 1;
float fh = getSize().height - 1;
Shape shape = new java.awt.geom.Eclipse2D.Float(fx, fy, fw, fh); /For eclipse shape
shape.contains(4.0,5.0);   
shape.contains(4.0,5.0,4.0,5.0);
Point2D point2D = new Point2D.Float(20.F, 30.F); //Point2D declared here
shape.contains(point2D);
shape.contains(new Rectangle2D.Double(10, 50, 280, 90));
shape.getBounds(); //shape.getBounds() method
shape.getBounds2D(); //shape.getBounds2D() method   
shape.getPathIterator(AffineTransform.getTranslateInstance(point2D.getX()+20, point2D.getY()+20)); //getPathIterator() method declared   
shape.getPathIterator(AffineTransform.getTranslateInstance(point2D.getX()+20, point2D.getY()+20),4.0); //getPathIterator() method declared
shape.intersects(4.0,5.0,4.0,5.0);   
shape.intersects(new Rectangle2D.Double(10, 50, 280, 90)); //Rectangle2D shape   

// This will set the clipping area
g2D.setClip(shape);
// The below will Draw an image
int x = 0; //x = 0
int y = 0; //y = 0
g2D.drawImage(image, x, y, this); //Draw an image
}
}

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