Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is the solution to the Target problem from Assignment 2 of the Stanford CS106A Introduction to Programming Methodology Class
/*
* File: Target.java
* Name:
* Section Leader:
* -----------------
* This file is the starter file for the Target problem.
*/
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class Target extends GraphicsProgram {
public void run() {
putOuterCircle();
putMiddleCircle();
putInnerCircle();
}
private void putOuterCircle() {
int radius = 72;
int x = getWidth()/2 - radius/2;
int y = getHeight()/2 - radius/2;
GOval OuterCircle = new GOval (x, y, radius, radius);
OuterCircle.setColor(Color.RED);
OuterCircle.setFilled(true);
OuterCircle.setFillColor(Color.RED);
add(OuterCircle);
}
private void putMiddleCircle() {
double radius = 72*64/100;
double x = getWidth()/2 - radius/2;
double y = getHeight()/2 - radius/2;
GOval MiddleCircle = new GOval (x, y, radius, radius);
MiddleCircle.setColor(Color.WHITE);
MiddleCircle.setFilled(true);
MiddleCircle.setFillColor(Color.WHITE);
add(MiddleCircle);
}
private void putInnerCircle() {
double radius = 72*3/10;
double x = getWidth()/2 - radius/2;
double y = getHeight()/2 - radius/2;
GOval InnerCircle = new GOval (x, y, radius, radius);
InnerCircle.setColor(Color.RED);
InnerCircle.setFilled(true);
InnerCircle.setFillColor(Color.RED);
add(InnerCircle);
}
}
@filmibuff

This comment has been minimized.

Copy link

@filmibuff filmibuff commented Apr 25, 2013

Here's my code. By setting a max_circles constant, this program can be possibly extended to draw more circles with just a minor change on adding a calculation to determine each circle's radius which could be radius-=(radius/MAX_CIRCLES).

import acm.graphics.;
import acm.program.
;
import java.awt.*;

public class Target2 extends GraphicsProgram {
private static final int MAX_CIRCLES=3;
private static final int PIXELS_PER_INCH=72;
public void run() {
/* Set Initial Radius */
double radius=1;
drawTarget(radius);

}
private void drawTarget(double radius){
    radius*=PIXELS_PER_INCH;

    for(int i=0; i<MAX_CIRCLES; i++){

        double x=(getWidth()/2)-radius;
        double y=(getHeight()/2)-radius;
        GOval circle=new GOval(x, y, 2*radius, 2*radius);
        circle.setFilled(true);
        if (i%2==0){
        circle.setColor(Color.RED);
        }
        else{
            circle.setColor(Color.WHITE);   
        }
        add(circle);
        radius-=0.35*PIXELS_PER_INCH;



    }
}
@Abdul7869

This comment has been minimized.

Copy link

@Abdul7869 Abdul7869 commented Oct 22, 2016

I am taking cs101 at staford. Need Need help with coding "image expressions excercises" -week 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment