Guten Abend Zusammen
Ich versuche gerade Schulaufgabe im JAVA programmieren zu lösen und kommen einfach nicht mehr weiter. Die Aufgabe besteht darin aus Inputs (Eckpunkte für ein Polygon) die Fläche zu berechnen, die Anzahl Eckpunkte sowie das eingegebene Polygon in einem Frame zu zeichnen.
Nun zu meinem Problem: Ich möchte gerne ein Polygon zeichnen mit den Eckpunkten, welche ich via Scanner einlese aber ich bringe es nicht fertig, die Array Listen in die Component Klasse zu integrieren und mit den eingegebenen Eckpunkten ein Polygon zu erstellen, welches ich zeichnen möchte.
Ich programmiere noch nicht lange und höchstwahrscheinlich mache ich etwas grundlegendes falsch:-(.
hier die drei klassen:
1. Polygon:
2. Main:
3. component:
danke vielmals für eure Hilfe
Ich versuche gerade Schulaufgabe im JAVA programmieren zu lösen und kommen einfach nicht mehr weiter. Die Aufgabe besteht darin aus Inputs (Eckpunkte für ein Polygon) die Fläche zu berechnen, die Anzahl Eckpunkte sowie das eingegebene Polygon in einem Frame zu zeichnen.
Nun zu meinem Problem: Ich möchte gerne ein Polygon zeichnen mit den Eckpunkten, welche ich via Scanner einlese aber ich bringe es nicht fertig, die Array Listen in die Component Klasse zu integrieren und mit den eingegebenen Eckpunkten ein Polygon zu erstellen, welches ich zeichnen möchte.
Ich programmiere noch nicht lange und höchstwahrscheinlich mache ich etwas grundlegendes falsch:-(.
hier die drei klassen:
1. Polygon:
Java:
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
/**
* A class which represents a Polygon.
* @author Graf
* @Date 12.12.2010
* Version 1.0
*/
public class Polygon {
// Variables
int[] xPointsArray;
int[] yPointsArray;
int numberOfVertexes;
private double area;
/**
* Constructor, that creates a polygon
* @Param vertexes (array for xpoints, array for y points and int for number of vertexes)
*/
public Polygon(int[] xpoints, int[] ypoints, int npoints)
{
xPointsArray = xpoints;
yPointsArray = ypoints;
numberOfVertexes = npoints;
}
/**
* Method, that computes the area of the polygon
* @Return area
*/
public double computeArea()
{
for (int i = 0; i <= numberOfVertexes-1; i++)
{
if (i == numberOfVertexes-1)
{
area =( area + (xPointsArray[i]*yPointsArray[0])-(xPointsArray[0]*yPointsArray[i]));
}
else
{
area =( area + (xPointsArray[i]*yPointsArray[i+1])-(xPointsArray[i+1]*yPointsArray[i]));
}
}
area = area / 2;
// if the area is entered counterclockwise the result will get negative but the value is correct.
if (area < 0)
{
area = area * -1;
}
return area;
}
/**
* Method, that lists all vertexes of the polygon
* @Return Array(List) of the vertexes
*/
public ArrayList<String> getListPoints()
{
ArrayList<String> vertexes = new ArrayList<String>();
for(int i = 0; i <= numberOfVertexes-1; i++)
{
vertexes.add(i, String.valueOf(xPointsArray[i]) + "," + String.valueOf(yPointsArray[i]));
}
return vertexes;
}
/**
* Method, that draws the polygon and fills it with an entered color
* @Param color
* @Return Polygon
*/
public java.awt.Polygon fill(Graphics2D g2, Color color)
{
java.awt.Polygon myPolygon = new java.awt.Polygon(xPointsArray, yPointsArray, numberOfVertexes);
g2.setColor(color);
g2.fillPolygon(myPolygon);
g2.drawPolygon(myPolygon);
return myPolygon;
}
}
2. Main:
Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
public class PolygonTester {
public static void main (String[] args )
{
// Variables
final int xPoints_LENGTH = 499;
final int yPoints_LENGTH = 499;
int xPointsSize = 0;
int yPointsSize = 0;
double area;
int[] xPoints = new int[xPoints_LENGTH];
int[] yPoints = new int[yPoints_LENGTH];
ArrayList<String> ListPoints;
// Scanner for the x values
System.out.println("Please enter the x points of the polygon counterclockwise. Enter finish when your done(max. 500 points): ");
Scanner in1 = new Scanner(System.in);
while (in1.hasNextInt())
{
xPoints[xPointsSize] = in1.nextInt();
xPointsSize++;
}
// Scanner for the y values
System.out.println("Please enter the y points of the polygon counterclockwise. Enter finish when your done(max. 500 points): ");
Scanner in2 = new Scanner(System.in);
while (in2.hasNextInt())
{
yPoints[yPointsSize] = in2.nextInt();
yPointsSize++;
}
// creates a new polygon with the given arrays
Polygon myFirstPolygon = new Polygon(xPoints, yPoints, xPointsSize);
// computes the area of the polygon
area = myFirstPolygon.computeArea();
// gets the listpoints
ListPoints = myFirstPolygon.getListPoints();
// prints out the area and the list of the points
System.out.println("The area of the polygon is: " + area + " square.");
System.out.println("A list of the vertexes: " + ListPoints);
//defines final variables for the pixel Size of the Frame
final int FRAME_WIDTH = 700;
final int FRAME_HEIGHT = 400;
//constructs a new frame named frame
JFrame frame = new JFrame();
//sets the size of the frame using the defined variables FRAME_WIDTH and FRAME_HEIGHT
frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
//sets the title of the frame to an Polygon
frame.setTitle("Polygon");
//set the backgroundcolor of the frame
frame.setBackground(Color.RED);
//sets the default close operation EXIT_ON_CLOSE
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// adds the polygon to the frame
PolygonComponent component = new PolygonComponent();
frame.add(component);
//sets the frame visible
frame.setVisible(true);
}
}
3. component:
Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/**
* A component that includes the elements of the class Polygon to draw them into a Frame.
* @author Graf
* @version 1.0
* Date 13.12.2010
*/
//connects the PolygonComponent class to the JComponent of the PolygonTester
public class PolygonComponent extends JComponent
{
public void paintComponent(Graphics g)
{
// Recover Graphics2D
Graphics2D g2 = (Graphics2D)g;
// creates one House of class Polygon
Polygon myPolygon = new Polygon(xPointsArray, yPointsArray, numberOfVertexes);
myPolygon.fill(g2, Color.WHITE);
}
}
danke vielmals für eure Hilfe