since I am completely newly learning Java right now I have a serious problem which is hopefully easy to solve.
I need to create the olympic rings for a course which should show up centered on the screen.
Here's my code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import acm.graphics.*;
import acm.program.*;
public class OlympicRings extends GraphicsProgram{
public void run() {
GOval blueRing = new GOval (100, 100, 90, 90);
blueRing.setColor(Color.BLUE);
add(blueRing);
GOval yellowRing = new GOval (150, 150, 90, 90);
yellowRing.setColor(Color.YELLOW);
add(yellowRing);
GOval blackRing = new GOval (200, 100, 90, 90);
add(blackRing);
GOval greenRing = new GOval (250, 150, 90, 90);
greenRing.setColor(Color.GREEN);
add(greenRing);
GOval redRing = new GOval (300, 100, 90, 90);
redRing.setColor(Color.RED);
add(redRing);
GCompound olympics = new GCompound();
olympics.add(blueRing, 100, 100);
olympics.add(yellowRing, 150, 150);
olympics.add(blackRing, 200, 100);
olympics.add(greenRing, 250, 150);
olympics.add(redRing, 300, 100);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double x = (screenSize.getWidth() - olympics.getWidth()) / 2;
double y = (screenSize.getHeight() + olympics.getHeight()) / 2;
add(olympics, x, y);
}
}
Not pretty, I know, but it's only my 3rd code.
My problem: I don't get the GCompound centered. It always shows up on the far right bottom corner of the canvas.
I already searched through this site and plenty of others to find a solution but nothing worked.
I hope somebody can help me with this.
Kind regards, Kate