我正在尝试实现一个函数来计算多边形的面积。这段代码允许你画一个多边形,但是当涉及到显示区域时,我不知道该怎么做。我尝试了几种方法,但我仍然是编程的初学者,所以我很感激任何帮助。这是代码:
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Polygon;
public class DrawPolygons extends JApplet implements ActionListener, MouseListener
{
private static final int NUMPOINTS = 500; //Up to 500 points can be chosen
private JButton finish; //Button to indicate user is done entering points
private Polygon shape; //polygon object to be drawn
private boolean isDrawn; //boolean flag for when the user is finished drawing
private int count; //how many points the user has clicked
private Color color; //color of the polygon after user finalizes points
private int[] x; //x coordinates of each point user picks
private int[] y; //y coordinates of each point user picks
private float sum;
private double area;
public void init() //set up GUI
{
setLayout(new FlowLayout());
addMouseListener(this); //adds MouseListener for mouse clicks
isDrawn = false; //isDrawn is initially false
count = 0; //count starts as 0
x = new int[NUMPOINTS]; //allows for up to 500 points to be chosen
y = new int[NUMPOINTS]; //allows for up to 500 points to be chosen
finish = new JButton("Finalize points"); //creates finish button
finish.addActionListener(this); //adds finish button to ActionListener
add(finish); //adds finish button to GUI
color = Color.BLACK; //color is intially black, and will remain
// so if user cancels the color chooser
//JOptionPane.showMessageDialog(null, "Click points that will make up the polygon. After each" +
// "point is entered, press the Finalize Points button");
shape = new Polygon(); //creates the Polygon shape
}
public void paint(Graphics g) //draws the Polylines, Polygons, and sets the color
{
super.paint(g);
g.drawPolyline(x, y, count); //draws the polyline specified by user
// mouseclick
g.setColor(color); //sets the color to the user chosen color
if(isDrawn) //if finalize button is clicked
{
g.fillPolygon(x, y, count); //finalizes polylines into
} // polygon and fills shape
}
public void actionPerformed(ActionEvent a) //decides what to do when finalize button is pressed
{
if(a.getSource() == finish) //if the finalize button is pressed
{
isDrawn = true; //isDrawn is set to true, ending the users ability
// to add more points, and fills the polygon
color = Color.red;
//JColorChooser.showDialog(this, "Choose a color", color); //color is set to users choice
repaint();
JOptionPane.showMessageDialog(null, "The area is ");
}
}
public void mouseClicked(MouseEvent e) //save coordinates of clicks
{
if(isDrawn == false && count < NUMPOINTS) //if the finalize button is not pressed
{ // the user can add additional points
x[count] = e.getX(); //adds the x point at the current mouse x coordinate
y[count] = e.getY(); //adds the y point at the current mouse y coordinate
count++; //count increases with each mouse click
repaint();
}
else if (e.isShiftDown()) {
// Clear the applet. (This only requires a repaint.)
// Also, set count to zero to start a new polygon.
count = 0;
isDrawn = false;
repaint();
}
}
private float getPolygonArea(int[] x, int[] y, int count)
{
float sum_but_no_result=0;
for(int i=0;i<(count-1);i++) // count is point number of polygon
{
sum_but_no_result+=x[i]*y[i+1] + y[i]*x[i+1];
}
sum_but_no_result+=x[count-1]*y[0] + y[count-1]*x[0];
float sum = (float)Math.abs(sum_but_no_result) / 2.0f;
return sum;
}
//Empty Implementation provided here so we can implement MouseListener (needed because we must
// provide concrete forms of all methods of an interface to implement it
public void mousePressed(MouseEvent e){};
public void mouseReleased(MouseEvent e){};
public void mouseEntered(MouseEvent e){};
public void mouseExited(MouseEvent e){};
}