Einen Kreis erzeugen

Status
Nicht offen für weitere Antworten.

babuschka

Top Contributor
Hallo,

ich habe folgende Klasse geschrieben:

Java:
class Circle {
int x, y;
int radius;
String color;
Circle() {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
void shift (int dx, int dy) {
x = x + dx;
y = y + dy;
}
void scale (double a){
this.radius = radius*(int) a;
}
double getArea(){
return Math.PI * radius * radius;
}
void setColor (String color){
this.color = color;
}
}

Außerdem habe ich folgende Klasse bearbeitet:

Java:
class TestCircle {
 
    public static void main(String[] args) {
        
        /*** KREIS 1 *********************************************************/
        /* (1) Erzeuge Kreis mit 
         *  x = 0
         *  y = 0
         *  radius = 20
         *  color = red
         */
        
        Circle c = new Circle();
        c.x = 0;
        c.y = 0;
        c.radius = 20;
        c.color = "red";

              
        
        /* (2) Ausgabe des Flaecheninhalts auf der Konsole
         */
         
        
        
        //! Fuegt Kreis zum Zeichnen hinzu
        CircleDrawer.add(c);
        
        
        /*** KREIS 2 *********************************************************/
        /* (3) Verschiebe Mittelpunkt des Kreises mit der shift-Methode, wobei
         *  dx =  40
         *  dy = -60
         */
        
        c.x = 0+40;
        c.y = 0+-60;
        
        /* (4) Skaliere Kreis mit scale-Methode, wobei 
         *  a = 0.5
         */
         c.radius = (int) (20*0.5);
 
         
        /* (5) Setze Farbe des Kreises mit der entsprechenden setter-Methode, 
         * wobei
         *  color = "blue"
         */
        
        c.color = "blue";
        
        /* (6) Ausgabe des Flaecheninhalts auf der Konsole
         */
 
        
         
        //! Fuegt Kreis zum Zeichnen hinzu
        CircleDrawer.add(c);
        //! zeichnet Kreis
        CircleDrawer.draw();
    }
 
}

Und ich habe folgende Klasse zur Verfügung:

Java:
import java.awt.Graphics;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.*;

class CircleDrawer extends JPanel {

	private static final long serialVersionUID = 1L;

	static int width = 440;
	static int height = 440;

	static ArrayList<Circle> circles = new ArrayList<Circle>();

	private static Color getColor(String c) {
		if (c.equalsIgnoreCase("black")) {
			return Color.black;
		} else if (c.equalsIgnoreCase("blue")) {
			return Color.blue;
		} else if (c.equalsIgnoreCase("red")) {
			return Color.red;
		} else if (c.equalsIgnoreCase("green")) {
			return Color.green;
		} else if (c.equalsIgnoreCase("yellow")) {
			return Color.yellow;
		} else if (c.equalsIgnoreCase("orange")) {
			return Color.orange;
		} else {
			return Color.black;
		}
	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		for (int i = 0; i < circles.size(); i++) {
			Circle c = circles.get(i);
			g.setColor(getColor(c.color));
			g.fillOval(c.x, c.y, c.radius, c.radius);
			g.setColor(Color.black);
			g.drawString(Integer.toString(i + 1), c.x, c.y);
		}
		drawCross(g);
	}

	private void drawCross(Graphics g) {
		g.setColor(Color.black);
		int x0 = width / 2;
		int y0 = height / 2;
		g.drawLine(0, y0, width, y0);
		g.drawLine(x0, 0, x0, height);

		int lTick = 2;
		int step = 20;

		int xTick0 = y0 - lTick;
		int xTick1 = y0 + lTick;
		for (int i = 20; i < width; i += step) {
			g.drawLine(i, xTick0, i, xTick1);
		}
		int yTick0 = x0 - lTick;
		int yTick1 = x0 + lTick;
		for (int i = 20; i < height; i += step) {
			g.drawLine(yTick0, i, yTick1, i);
		}

	}

	private static Circle transform(Circle circle) {
		int r = 4 * circle.radius;
		int x = width / 2 + 2 * circle.x - r / 2;
		int y = height / 2 - 2 * circle.y - r / 2;
		String c = circle.color;
		return new Circle(x, y, r, c);
	}

	static void add(Circle c) {
		circles.add(transform(c));
	}

	static void draw() {
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(width, height);
		f.add(new CircleDrawer());
		f.setVisible(true);
	}
}

Wenn ich die Klasse TestCircle kompilieren möchte, bekomme ich folgende Fehlermeldung:

.\CircleDrawer.java:74: cannot find symbol
symbol : constructor Circle(int,int,int,java.lang.String)
location: class Circle
return new Circle(x, y, r, c);
^
1 error

Kann mir jemand helfen? Ich weiß nicht mehr weiter!?

Gruß
Sebastian
 
Schau dir mal diese Zeilen von dir an. Fällt dir was auf?

[JAVA=5]Circle() {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}[/code]

Du hast gar keinen Konstruktor definiert, der vier Parameter nimmt. Folglich wird dieser Konstruktor, wenn du ihn in CircleDrawer verwendest, nicht gefunden. Ergänze mal in Zeile 5 in den runden Klammern von Circle() dein int x, int y, int radius und String color.


EDIT: Lol, gleiche Aufgabe wie die hier: http://www.java-forum.org/java-basics-anfaenger-themen/109841-erzeuge-kreis.html
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben