User Tools

Site Tools


it-artikel:java:my-solution-on-assignment-2-problem-2

My Solution on Assignment 2 - Problem 2

This is my Solution of Assignment 2 - Problem 2 from the Computer Science Course CS106A of Prof. Mehran Sahami at the STANFORD University.

Target.java
/*
 * File: Target.java
 * Name: 
 * Section Leader: 
 * -----------------
 * This file is the starter file for the Target problem.
 * Assignment 2 , Problem 2
 */
 
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
 
import javax.swing.plaf.synth.ColorType;
 
public class Target extends GraphicsProgram {
 
	public static final int dpi = 72; // 72 dots/pixel per inch is assumed
 
	public void run() {
		GetCanvasSizeAndFillVars(); // fills CanvasX and CanvasY Class-Variables
		DrawTargetAt(CanvasX/2,CanvasY/2); // Draw (almost) in the Middle of the Canvas
	}
 
	public void DrawTargetAt(int X, int Y) {
		/*
		 * This Method draws the "logo" or "Target" itself. It defines the
		 * LOOK of the Logo and how its build up. 
		 * X and Y define the CENTER POINT of the Logo/Target Sign. 
		 */
		DrawCircleAt(X,Y,1,Color.red);
		DrawCircleAt(X,Y,0.65,Color.white);
		DrawCircleAt(X,Y,0.3,Color.red);
	}
 
	public void DrawCircleAt(int CenterX,int CenterY,double RadiusInch,Color Colour) {
		/*
		 * This Method draws a CIRCLE with its Centerpoint at X,Y with the Radius
		 * given in INCH. Circles are of a SINGLE SOLID COLOR (filled). 
		 * Actual Size on the Screen depends on the Value set by the "dpi" Constant 
		 */
 
		//Calculate Radius in PIXEL
		double RadiusPixels = RadiusInch * dpi;
		//Calculate REAL Starting Point (Upper Left Coordinates) for a GOval Object
		double X = CenterX - RadiusPixels;
		double Y = CenterY - RadiusPixels;		
 
		GOval aCircle = new GOval(X, Y, 2*RadiusPixels, 2*RadiusPixels); // Width and Height is TWICE the Radius in Pixel
		aCircle.setColor( Colour ); // set Outline as same Color
		aCircle.setFillColor( Colour ); // set Fill as same Color
		aCircle.setFilled(true); // yes make it filled
		add(aCircle);
	}	
 
	public void GetCanvasSizeAndFillVars() {
		/*
		 * Get Canvas Sizes and fill in Class Variables
		 */
		CanvasX = getWidth();
		CanvasY = getHeight();
	}
 
 
	public int CanvasX, CanvasY; // some public Class Variables
}

Axel Werner 2011-02-06 16:26

it-artikel/java/my-solution-on-assignment-2-problem-2.txt · Last modified: 2022-08-31 12:30 by 127.0.0.1