Help with correcting errors in Java program. The program is drawing a 2d irregul
ID: 3826507 • Letter: H
Question
Help with correcting errors in Java program. The program is drawing a 2d irregular polygon. I need helping fixing these classes AribitraryPolygon and BoxPanel errors to make the program run. The program below keeps giving me these errors:
----jGRASP exec: javac -g IrregularPolyPanel.java
IrregularPolyPanel.java:16: error: cannot find symbol
private ArbitraryPolygon shape = new ArbitraryPolygon();
^
symbol: class ArbitraryPolygon
location: class IrregularPolyPanel
IrregularPolyPanel.java:28: error: cannot find symbol
private BoxesPanel drawPanel;
^
symbol: class BoxesPanel
location: class IrregularPolyPanel
IrregularPolyPanel.java:16: error: cannot find symbol
private ArbitraryPolygon shape = new ArbitraryPolygon();
^
symbol: class ArbitraryPolygon
location: class IrregularPolyPanel
IrregularPolyPanel.java:160: error: cannot find symbol
drawPanel = new BoxesPanel(npoints, xpoints, ypoints, 100);
^
symbol: class BoxesPanel
location: class IrregularPolyPanel.ButtonListener
4 errors
-------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.text.DecimalFormat;
import java.awt.Polygon;
import java.awt.Graphics;
public class IrregularPolyPanel extends JPanel
{
private JLabel Intro, numPointsLabel, area, perimeter;
private ArbitraryPolygon shape = new ArbitraryPolygon();
private int npoints;
private int[] xpoints;
private int[] ypoints;
private String[] numbers;
private JLabel Xpoints;
private JTextField[] XPoints;
private JLabel Ypoints;
private JTextField[] YPoints;
private JTextField numPoints;
private JButton enter;
private JPanel pane;
private BoxesPanel drawPanel;
private double thetaInc;
private double theta;
public IrregularPolyPanel()
{
setLayout(new BorderLayout());
numPointsLabel = new JLabel ("Number of Points:");
area = new JLabel("Area:");
perimeter = new JLabel ("Perimeter:");
enter = new JButton("Enter All Parameters");
enter.addActionListener(new ButtonListener());
numPoints = new JTextField(5);
numPoints.addActionListener(new ButtonListener());
pane = new JPanel (new GridLayout(0,1,1,1));
pane.setBorder(new TitledBorder("Arbitrary Polygon Parameters"));
pane.add(numPointsLabel);
pane.add(numPoints);
pane.add(area);
pane.add(perimeter);
pane.add(enter);
add(pane, BorderLayout.WEST);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String NUM_POINTS;
int result;
if(event.getSource() == enter)
{
NUM_POINTS = numPoints.getText();
if(NUM_POINTS.equals(""))
npoints = 0;
else
npoints = Integer.parseInt(NUM_POINTS);
if(npoints < 3)
JOptionPane.showMessageDialog(null, "Invalid entry: Number of points must be more than 2.", "Error", JOptionPane.ERROR_MESSAGE);
else
{
xpoints = new int[npoints];
ypoints = new int[npoints];
numbers = new String[npoints];
XPoints = new JTextField[npoints];
YPoints = new JTextField[npoints];
JPanel numPanel = new JPanel(new GridLayout((npoints+1), 2));
Xpoints = new JLabel(" Xpoints");
Ypoints = new JLabel(" Ypoints");
numPanel.add(Xpoints);
numPanel.add(Ypoints);
for(int i = 0; i < npoints; i++)
{
XPoints[i] = new JTextField(5);
YPoints[i] = new JTextField(5);
numPanel.add(XPoints[i]);
numPanel.add(YPoints[i]);
}
result = JOptionPane.showConfirmDialog(null, numPanel, "Enter Points", JOptionPane.YES_NO_OPTION);
boolean check = false;
int counter = 0;
while((result != JOptionPane.NO_OPTION) && (counter < npoints))
{
System.out.println(counter);
if(((XPoints[counter].getText()).equals("")) || ((YPoints[counter].getText()).equals("")))
xpoints[counter] = ypoints[counter] = 0;
else
{
xpoints[counter] = Integer.parseInt(XPoints[counter].getText());
ypoints[counter] = Integer.parseInt(YPoints[counter].getText());
}
if((xpoints[counter] < 1) || (ypoints[counter] < 1))
{
check = false;
JOptionPane.showMessageDialog(null, "Invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
result = JOptionPane.showConfirmDialog(null, numPanel, "Enter Points", JOptionPane.YES_NO_OPTION);
}
else
{
xpoints[counter] = Integer.parseInt(XPoints[counter].getText());
ypoints[counter] = Integer.parseInt(YPoints[counter].getText());
counter++;
check = true;
}
}
if(check == true)
{
shape.setNumPoints(npoints);
shape.addPoints(xpoints, ypoints, npoints);
thetaInc = 2 * Math.PI / npoints;
theta = (npoints % 2 == 0) ? thetaInc : -Math.PI/2;
for(int i = 0; i < npoints; i++)
{
int x1 = xpoints[i] - 100;
int y1 = ypoints[i] - 100;
xpoints[i] = 100+ (int) ((x1 * Math.cos(theta)) - (y1 * Math.sin(theta))) + 200;
ypoints[i] = 100+ (int) ((x1 * Math.sin(theta)) + (y1 * Math.cos(theta))) + 300;
theta += thetaInc;
}
double Perimeter = shape.getPerimeterLength();
double Area = shape.getArea();
DecimalFormat df = new DecimalFormat("#.##");
area.setText("Area: " + df.format(Area));
perimeter.setText("Perimeter: " + df.format(Perimeter));
//-----------------------------------------------------------------
// Draw the Arbitrary Polygon
//-----------------------------------------------------------------
drawPanel = new BoxesPanel(npoints, xpoints, ypoints, 100);
add(drawPanel, BorderLayout.CENTER);
}
}
}
}
}
}
Explanation / Answer
ArbitraryPolygon and BoxesPanel are the classes which are missing from the given code.
As these classes does not seem to be built in classes, these classes should be defined in some other file that you are missing from your project. Try to find the missing files which has the defination of these classes.
Put the files in the same project as this one, it will solve the error.
Otherwise the code is fine.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.