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

I\'m currently trying to figure out why I\'m getting an error message. When I ru

ID: 3758767 • Letter: I

Question

I'm currently trying to figure out why I'm getting an error message. When I run the code the applet viewer window pops up, but its blank and says "Start: applet not initialized". The code is suppose to work for the project descrition below

Project Description:

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 being dragged, the rectangle center coordinates in the text fields are updated.

Name your main class Exercise18_29

The image is attached.

Here is the code so far, please offer advice on how to fix this error if possible.

//import the required packages

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

//class RectangleIntersectionGUI

public class RectangleIntersectionGUI extends JApplet

{

   //declaration of components

   private JTextField rectangle1P1x = new JTextField();

   private JTextField rectangle1P1y = new JTextField();

   private JTextField rectangle1width = new JTextField();

   private JTextField rectangle1height = new JTextField();

   private JTextField rectangle2P1x = new JTextField();

   private JTextField rectangle2P1y = new JTextField();

   private JTextField rectangle2width = new JTextField();

   private JTextField rectangle2height = new JTextField();

   private JButton jbtRedraw = new JButton("Redraw Rectangles");

   //declaration of classes

   private MyRectangle2D rectangle1 = new MyRectangle2D(50, 50, 70, 50);

   private MyRectangle2D rectangle2= new MyRectangle2D(90, 100, 50, 70);

   private DrawPanel paintPanel = new DrawPanel();

   private JLabel labelStatus = new JLabel("Two rectangle intersect? "

                      +(rectangle1.overlaps(rectangle1) ? "Yes" : "No"),JLabel.CENTER);

   //constructor

   public RectangleIntersectionGUI()

   {

//create a panel

JPanel p1 = new JPanel(new GridLayout(4, 2));

//set the Border to the panel

p1.setBorder(new TitledBorder("" + "Enter rectangle 1 info"));

//add the first rectangle labels to the panel

p1.add(new JLabel("Center x:"));

p1.add(rectangle1P1x);

p1.add(new JLabel("Center y:"));

p1.add(rectangle1P1y);

p1.add(new JLabel("Width: "));

p1.add(rectangle1width);

p1.add(new JLabel("Height:"));

p1.add(rectangle1height);

//create second panel

JPanel p2 = new JPanel(new GridLayout(4, 2));

//set the border to the panel

p2.setBorder(new TitledBorder("Enter rectangle 2 info"));

//add the second rectangle labels to the panel

p2.add(new JLabel("Center x:"));

p2.add(rectangle2P1x);

p2.add(new JLabel("Center y:"));

p2.add(rectangle2P1y);

p2.add(new JLabel("Width: "));

p2.add(rectangle2width);

p2.add(new JLabel("Height: "));

p2.add(rectangle2height);

//create a third panel

JPanel p3 = new JPanel(new GridLayout(1, 2));

//add the panels p1, p2 to the third panel

p3.add(p1);

p3.add(p2);

//create fourth panel

JPanel p4 = new JPanel(new BorderLayout());

//add the panel p3, and button to the panel p4

p4.add(p3, BorderLayout.CENTER);

p4.add(jbtRedraw, BorderLayout.SOUTH);

//add the label, panel p4, and paintPanel to the applet

add(labelStatus, BorderLayout.NORTH);

add(p4, BorderLayout.SOUTH);

add(paintPanel, BorderLayout.CENTER);

//add Action Listener class to the button

jbtRedraw.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

   //set the x,y, width and height values for the rectangle1 by getting inputs given by user

   rectangle1.setX(Integer.parseInt(rectangle1P1x.getText()));

   rectangle1.setY(Integer.parseInt(rectangle1P1y.getText()));

   rectangle1.setWidth(Integer.parseInt(rectangle1width.getText()));

   rectangle1.setHeight(Integer.parseInt(rectangle1height.getText()));

   //set the x,y, width and height values for the rectangle2 by getting input values given by user

   rectangle2.setX(Integer.parseInt(rectangle2P1x.getText()));

   rectangle2.setY(Integer.parseInt(rectangle2P1y.getText()));

   rectangle2.setWidth(Integer.parseInt(rectangle2width.getText()));

   rectangle2.setHeight(Integer.parseInt(rectangle2height.getText()));

   //set the text for a label

   labelStatus.setText("Two triangles intersect?"+(rectangle1.overlaps(rectangle2)?"Yes" : "No"));

   //calling repaint method

   paintPanel.repaint();

}

}

   }

   //Draw Panel class

   class DrawPanel extends JPanel

   {

//paintComponent method

protected void paintComponent(Graphics g)

{

super.paintComponents(g);

displayRectangle(g, rectangle1);

displayRectangle(g, rectangle2);

repaint();

}

//method to draw rectangle

private void displayRectangle(Graphics g, MyRectangle2D rect)

{

g.drawRect(

(int)(rect.getX()),

(int)(rect.getY()),

(int)(rect.getWidth()),

(int)(rect.getHeight()));

}

   }

   //applet's init method

   public void init()

   {

//method call to fill initial values

fillTextFieldWithInitialValues();

   }

   //method to fill initial values

   public void fillTextFieldWithInitialValues()

   {

//set the default values for rectangle1

rectangle1P1x.setText((int)rectangle1.getX()+ "");

rectangle1P1y.setText((int)rectangle1.getY()+ "");

rectangle1width.setText((int)rectangle1.getWidth() + "");

rectangle1height.setText((int)rectangle1.getHeight() + "");

//set the default values for rectangle2

rectangle2P1x.setText((int)rectangle2.getX() + "");

rectangle2P1y.setText((int)rectangle2.getY() + "");

rectangle2width.setText((int)rectangle2.getWidth() + "");

rectangle2height.setText((int)rectangle2.getHeight() + "");

   }

   //main method

   public static void main(String[] args)

   {

//create a frame

JFrame frame = new JFrame("Rectangle" + " Intersection");

//create an object to applet class

RectangleIntersectionGUI applet = new RectangleIntersectionGUI();

//initialize the applet

applet.start();

//add the applet to the frame

frame.add(applet);

//set the setting for the frame

Dimension d=new Dimension();

d.setSize(500, 700);

frame.setSize(d);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

   }

}

  

//class MyRectangle2D

public class MyRectangle2D

{

   //declare the required variables

   private double x, y;

   private double width, height;

   //empty constructor

   public MyRectangle2D()

   {

x = y = 0;

width = height = 1;

   }

   //parameterized constructor

   public MyRectangle2D(double x, double y, double width, double height)

   {

this.x = x;

this.y = y;

this.width = width;

this.height = height;

   }

   //getter method to get the x,y, width and height of a rectangle

   public double getX()

   {

return x;

   }

   public double getY()

   {

return y;

   }

   public double getWidth()

   {

return width;

   }

   public double getHeight()

   {

return height;

   }

   //setter methods to set the values for x,y,width and height of a rectangle

   public void setX(double x)

   {

this.x = x;

   }

   public void setY(double y)

   {

this.y = y;

   }

   public void setWidth(double width)

   {

this.width = width;

   }

   public void setHeight(double height)

   {

this.height = height;

   }

   //overlap method that returns boolean value

   public boolean overlaps(MyRectangle2D r)

   {

return Math.abs(this.x - r.x) <=(this.width + r.width) / 2 &&

   Math.abs(this.y - r.y) <= (this.height + r.height) / 2;

   }

}

Explanation / Answer

There are two problems with the code:

object for class rectangle is not created in main.

html is not there for this applet. May be there is an issue with the html code for the applet where class name is not proper.

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