Good Morning! Please write the Java code for the following programme EXACTLY acc
ID: 673505 • Letter: G
Question
Good Morning! Please write the Java code for the following programme EXACTLY according to the instructions below, figure below (the one in the middle) and the 10.13 instructions. PLEASE make sure the code COMPILES and programme RUNS before sending. Thank You!!!
The MyRectangle2D class was defined in Exercise 10.12. Write an application that enables the user to specify the location and size of the rectangles and displays whether the two rectangles intersect, as shown in the following figure. Enable the user to point the mouse inside a rectangle and drag it. As the rectangle is being dragged, the rectangle center coordinates in the text fields are updated.
Explanation / Answer
Complete Program:
File: TwoRectanglesInteresect.java
// TwoRectanglesInteresect class implementation
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class TwoRectanglesInteresect extends JApplet
{
private MyRectangle2D rectangle1;
private MyRectangle2D rectangle2;
private JTextField jtfCenterx1;
private JTextField jtfCentery1;
private JTextField jtfWidth1;
private JTextField jtfHeight1;
private JTextField jtfCenterx2;
private JTextField jtfCentery2;
private JTextField jtfWidth2;
private JTextField jtfHeight2;
private JButton jbtRedraw;
private JLabel jlblStatus;
private PaintPanel paintPanel;
public TwoRectanglesInteresect()
{
rectangle1 = new MyRectangle2D(143.0, 49.0, 70.0, 50.0);
rectangle2 = new MyRectangle2D(90.0, 50.0, 50.0, 70.0);
jtfCenterx1 = new JTextField(rectangle1.getX() + "");
jtfCentery1 = new JTextField(rectangle1.getY() + "");
jtfWidth1 = new JTextField(rectangle1.getWidth() + "");
jtfHeight1 = new JTextField(rectangle1.getHeight() + "");
jtfCenterx2 = new JTextField(rectangle2.getX() + "");
jtfCentery2 = new JTextField(rectangle2.getY() + "");
jtfWidth2 = new JTextField(rectangle2.getWidth() + "");
jtfHeight2 = new JTextField(rectangle2.getHeight() + "");
jbtRedraw = new JButton("Redraw Rectangles");
jlblStatus = new JLabel("Two rectangles intersect? "
+ (rectangle1.contains(rectangle2) ? "Yes" : "No"),
JLabel.CENTER);
paintPanel = new PaintPanel();
JPanel p1 = new JPanel(new GridLayout(4, 2));
p1.setBorder(new TitledBorder("Enter rectangle 1 info"));
p1.add(new JLabel("Center x:"));
p1.add(jtfCenterx1);
p1.add(new JLabel("Center y:"));
p1.add(jtfCentery1);
p1.add(new JLabel("Width:"));
p1.add(jtfWidth1);
p1.add(new JLabel("Height:"));
p1.add(jtfHeight1);
JPanel p2 = new JPanel(new GridLayout(4, 2));
p2.setBorder(new TitledBorder("Enter rectangle 2 info"));
p2.add(new JLabel("Center x:"));
p2.add(jtfCenterx2);
p2.add(new JLabel("Center y:"));
p2.add(jtfCentery2);
p2.add(new JLabel("Width:"));
p2.add(jtfWidth2);
p2.add(new JLabel("Height:"));
p2.add(jtfHeight2);
JPanel p3 = new JPanel(new GridLayout(1, 2));
p3.add(p1);
p3.add(p2);
JPanel p4 = new JPanel(new BorderLayout());
p4.add(p3, BorderLayout.CENTER);
p4.add(jbtRedraw, BorderLayout.SOUTH);
add(jlblStatus, BorderLayout.NORTH);
add(p4, BorderLayout.SOUTH);
add(paintPanel, BorderLayout.CENTER);
jbtRedraw.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rectangle1.setX(Double.parseDouble(jtfCenterx1.getText()));
rectangle1.setY(Double.parseDouble(jtfCentery1.getText()));
rectangle1.setWidth(Double.parseDouble(jtfWidth1.getText()));
rectangle1.setHeight(Double.parseDouble(jtfHeight1.getText()));
rectangle2.setX(Double.parseDouble(jtfCenterx2.getText()));
rectangle2.setY(Double.parseDouble(jtfCentery2.getText()));
rectangle2.setWidth(Double.parseDouble(jtfWidth2.getText()));
rectangle2.setHeight(Double.parseDouble(jtfHeight2.getText()));
jlblStatus.setText("Two rectangles intersect? "
+ (rectangle1.contains(rectangle2) ? "Yes" : "No"));
paintPanel.repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
if(rectangle1.contains(e.getX(), e.getY()))
{
jtfCenterx1.setText("" + e.getX());
jtfCentery1.setText("" + e.getY());
rectangle1.setX(e.getX());
rectangle1.setY(e.getY());
updateRectangles();
}
else if(rectangle2.contains(e.getX(), e.getY()))
{
jtfCenterx2.setText("" + e.getX());
jtfCentery2.setText("" + e.getY());
rectangle2.setX(e.getX());
rectangle2.setY(e.getY());
updateRectangles();
}
}
});
}
private void updateRectangles()
{
jlblStatus.setText("Two rectangles intersect? "
+ (rectangle1.overlaps(rectangle2) ? "Yes" : "No"));
paintPanel.repaint();
}
private class PaintPanel extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("r1",
(int) (rectangle1.getX() + rectangle1.getWidth() / 2),
(int) (rectangle1.getY() + rectangle1.getHeight() / 2));
g.drawString("r2",
(int) (rectangle2.getX() + rectangle2.getWidth() / 2),
(int) (rectangle2.getY() + rectangle2.getWidth() / 2));
g.drawRect((int) (rectangle1.getX() - 10),
(int) (rectangle1.getY() + 10),
(int) (rectangle1.getWidth()),
(int) (rectangle1.getHeight()));
g.drawRect((int) (rectangle2.getX()), (int) (rectangle2.getY()),
(int) (rectangle2.getWidth()),
(int) (rectangle2.getHeight()));
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Exercise18_29");
JApplet applet = new TwoRectanglesInteresect();
frame.add(applet);
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} // end of TwoRectanglesInteresect class
File: MyRectangle2D.java
// MyRectangle2D class implementation
public class MyRectangle2D
{
private double x, y;
private double width, height;
public MyRectangle2D()
{
x = 0;
y = 0;
width = 0;
height = 0;
}
public MyRectangle2D(double x, double y, double width, double height)
{
setX(x);
setY(y);
setWidth(width);
setHeight(height);
}
public void setX(double xx)
{
x = xx;
}
public void setY(double yy)
{
y = yy;
}
public void setWidth(double w)
{
width = w;
}
public void setHeight(double h)
{
height = h;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
public double getPerimeter()
{
return 2 * (width + height);
}
public double getArea()
{
return width * height;
}
public boolean contains(double x, double y)
{
return Math.abs(x - this.x) <= this.width / 2 && Math.abs(y - this.y) <= this.height / 2;
}
public boolean contains(MyRectangle2D r)
{
return contains(r.x - r.width / 2, r.y + r.height / 2)
&& contains(r.x - r.width / 2, r.y - r.height / 2)
&& contains(r.x + r.width / 2, r.y + r.height / 2)
&& contains(r.x + r.width / 2, r.y - r.height / 2);
}
public boolean overlaps(MyRectangle2D r)
{
return (Math.abs(r.x - this.x) <= (r.width + this.width) / 2 && Math.abs(r.y - this.y) <= (r.height + this.height) / 2);
}
} // end of MyRectangle2D class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.