Erzeuge einen Kreis

Sophie.H

Mitglied
Hallo! 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;
}
}
Jetzt will ich folgendes machen:

Java:
class TestCircle {

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



		/* (2) Ausgabe des Flaecheninhalts auf der Konsole
		 */
		 
System.out.println("Flaecheninhalt=" + circle.getArea);


		//! Fuegt Kreis zum Zeichnen hinzu
		CircleDrawer.add(c);
		/*** KREIS 2 *********************************************************/
		/* (3) Verschiebe Mittelpunkt des Kreises mit der shift-Methode, wobei
		 * 	dx =  40
		 * 	dy = -60
		 */
circle.x = 0+40;
circle.y = 0+-60;


		/* (4) Skaliere Kreis mit scale-Methode, wobei 
		 * 	a = 0.5
		 */

circle.radius = 20*(int)0,5;


		/* (5) Setze Farbe des Kreises mit der entsprechenden setter-Methode, 
		 * wobei
		 * 	color = "blue"
		 */		

circle.color = "red";

		/* (6) Ausgabe des Flaecheninhalts auf der Konsole
		 */
		 System.out.println("Flaecheninhalt=" + circle.getArea);	
 
		//! Fuegt Kreis zum Zeichnen hinzu
		CircleDrawer.add(c);
		//! zeichnet Kreis
		CircleDrawer.draw();
	}
}
Aber irgendwie macht er das nicht.... Die Fehlermedlung des Compilers versteh ich leider nicht. Kann mir vielleicht jemand helfen? Ich würde gerne die Lösung erarbeiten!!

Danke und Gruß
 
Zuletzt bearbeitet:
Und bitte dein Java code in Zukunft in einem
Java:
 Block schreiben.

EDIT: Vielleicht hilft dir das [URL="http://www.itblogging.de/java/olympische-flagge-grafikprogrammierung-tutorial/"]Olympische Flagge – Grafikprogrammierung Tutorial[/URL] weiter.

Grüße
 
also erstmal:

TestCircle.java:18: unclosed string literal
cirlce.color = "red;
^
TestCircle.java:44: ';' expected
circle.radius = 20*(int)0,5;
^
2 errors
 
also erstmal:

TestCircle.java:18: unclosed string literal
cirlce.color = "red;
^
TestCircle.java:44: ';' expected
circle.radius = 20*(int)0,5;
^
2 errors

Also die Fehlermeldung sieht doch eindeutig aus. Zum einen hast du einen String nicht geschlossen und zum anderen musst du statt dem Komma ein Punkt setzen. Besser siehts noch so aus:
Java:
(int) (20*0.5)
 
gut. Jetzt kommt folgende Fehlermeldung:

TestCircle.java:18: cannot find symbol
symbol : variable cirlce
location: class TestCircle
cirlce.color = "red";
^
TestCircle.java:25: cannot find symbol
symbol : variable getArea
location: class Circle
System.out.println("Flaecheninhalt=" + circle.getArea);
^
TestCircle.java:29: cannot find symbol
symbol : variable c
location: class TestCircle
CircleDrawer.add(c);
^
TestCircle.java:29: non-static method add(java.awt.PopupMenu) cannot be referenc
ed from a static context
CircleDrawer.add(c);
^
TestCircle.java:57: cannot find symbol
symbol : variable getArea
location: class Circle
System.out.println("Flaecheninhalt=" + circle.getArea);
^
TestCircle.java:60: cannot find symbol
symbol : variable c
location: class TestCircle
CircleDrawer.add(c);
^
TestCircle.java:60: non-static method add(java.awt.PopupMenu) cannot be referenc
ed from a static context
CircleDrawer.add(c);
^
.\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);
^
8 errors
 
ok. einen Fehler habe ich noch gefunden:

Java:
Circle circle = new Circle(0,0,20,"red");

muss es sein.

Jetzt noch folgende Fehler:

TestCircle.java:21: cannot find symbol
symbol : variable getArea
location: class Circle
System.out.println("Flaecheninhalt=" + circle.getArea);
^
TestCircle.java:25: cannot find symbol
symbol : variable c
location: class TestCircle
CircleDrawer.add(c);
^
TestCircle.java:25: non-static method add(java.awt.PopupMenu) cannot be referenc
ed from a static context
CircleDrawer.add(c);
^
TestCircle.java:53: cannot find symbol
symbol : variable getArea
location: class Circle
System.out.println("Flaecheninhalt=" + circle.getArea);
^
TestCircle.java:56: cannot find symbol
symbol : variable c
location: class TestCircle
CircleDrawer.add(c);
^
TestCircle.java:56: non-static method add(java.awt.PopupMenu) cannot be referenc
ed from a static context
CircleDrawer.add(c);
^
6 errors
 
Sorry wenn ich so direkt frage, aber hast Du überhaupt eine Ahnung, was Du da tust? 😳

TestCircle.java:18: cannot find symbol
symbol : variable cirlce
location: class TestCircle
cirlce.color = "red";


Tippfehler: es heißt circle (nicht cirlce)

TestCircle.java:25: cannot find symbol
symbol : variable getArea
location: class Circle
System.out.println("Flaecheninhalt=" + circle.getArea);


getArea ist eine Methode, also mittels circle.getArea() aufrufen

TestCircle.java:29: cannot find symbol
symbol : variable c
location: class TestCircle
CircleDrawer.add(c);


Du übergibst die Variable c, aber ich kann nirgends eine Deklaration dieser Variable sehen. Wo kommt die her?
 
noch mehr Fehler gefunden🙂

Jetzt bekomme ich schon das Fadenkreuz hin aber die Kreise sind nicht farbig..... Sieht jemand noch einen Fehler?
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(0,0,20,"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();
	}

}
 
Dafür bräuchten wir den Source von der Klasse CircleDrawer.

Du definierst zwar für den Circle eine Farbe, aber dies ist erstmal lediglich ein "freier" Bezeichner. Was dann tatsächlich draus gemacht wird, wird (vermutlich) in der CircleDrawer Klasse stecken...
 
ich möchte doch nur noch, dass meine Kreise farbig sind... Sitze doch schon so lange daran!!

Das ist CircleDrawer:

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);
	}
}
 
Zuletzt bearbeitet:
Hmm, also ich hab mal die 3 Klassen zusammengebastelt und ausgeführt. Was ich sehe, ist ein Fenster mit Fadenkreuz und einem roten Kreis in der Mitte und einem kleineren blauen Kreis rechts/unten versetzt.

Also scheint es ja prinzipiell soweit zu laufen...
Vielleicht musst Du einfach nochmal den aktuellen Stand aller 3 Klassen die Du verwendest posten. Eventuell hast Du in der Zwischenzeit noch irgendwas woanders geändert?
 
JUHU! Ich hatte tatsächlich eine Änderung nicht gespeichert! Jetzt funktionierst!

Vielen Dank für eure Hilfe!!!

Hey Sophie,

ich probiere schon ewig an dem CircleDrawer, aber ich bekomme die letzte Fehlermeldung einfach nicht weg:

.\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);
^
Kannst Du mir weiterhelfen?

Gruß
Sebastian
 
JUHU! Ich hatte tatsächlich eine Änderung nicht gespeichert! Jetzt funktionierst!

Vielen Dank für eure Hilfe!!!

Hey Sophie,

ich probiere schon ewig an dem CircleDrawer, aber ich bekomme die letzte Fehlermeldung einfach nicht weg:

.\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);
^
Kannst Du mir weiterhelfen?

Gruß
Sebastian
 
Wie sieht denn Dein Konstruktor von der Klasse Circle aus?

Ich vermute mal, der sieht noch so aus:

Java:
	Circle() {
		this.x = x;
		this.y = y;
		this.radius = radius;
		this.color = color;
	}

muss aber so aussehen

Java:
	Circle(int x, int y, int radius, String color) {
		this.x = x;
		this.y = y;
		this.radius = radius;
		this.color = color;
	}
 
Wie sieht denn Dein Konstruktor von der Klasse Circle aus?

Ich vermute mal, der sieht noch so aus:

Java:
	Circle() {
		this.x = x;
		this.y = y;
		this.radius = radius;
		this.color = color;
	}

muss aber so aussehen

Java:
	Circle(int x, int y, int radius, String color) {
		this.x = x;
		this.y = y;
		this.radius = radius;
		this.color = color;
	}

Danke, das war der entscheidende Hinweis! :toll:
 

Zurück
Oben