School Assignment

Shooting arrows at a target-not sure what to do next

Page 1 of 1

1 Replies - 1772 Views - Last Post: 10 October 2008 - 01:17 PM Rate Topic: -----

#1 gallor889  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-October 08

School Assignment

Posted 10 October 2008 - 10:56 AM

import java.util.Random;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
public class Arrow {

	private static final Random rgen = new Random();
	private static int x=rgen.nextInt(220);
	private static int y=rgen.nextInt(238);
	private int score;
	private int totalScore;
	public Arrow(int max_x, int max_y){
		x=max_x;
		y=max_y;	
	}
	
	public int getX(){
		return x;
		
	}
	public int getY(){
		return y;
	}
	public void drawArrow(Graphics g){
		g.drawLine(x, y-2, x,y+3);
		g.setColor(Color.green);
		g.drawLine(x-2, y, x+3, y);
		g.setColor(Color.green);
		
	}
	public void calcScore(){
		if (x<10 && x>210 && y<10 && y>210)
			score=0;
		else if (x>=10 && x<20 && x>200 && x<=210 
				&& y>=10 && y<20 && y>200 && y<=210)
			score=1;
		else if (x>=20 && x<30 && x>190 && x<=200 
				&&y>=20 && y<30 && y>190 && y<=200)
			score=2;
		else if (x>=30 && x<40 && x>180 && x<=190 
				&& y>=30 && y<40 &&y>180 && y<=190)
			score=3;
		else if (x>=40 && x<50 && x>170 && x<=180 
				&& y>=40 && y<50 && y>170 && y<=180)
			score=4;
		else if (x>=50 && x<60 && x>160 && x<=170 
				&& y>=50 && y<60 && y>160 && y<=170)
			score=5;
		else if (x>=60 && x<70 && x>150 && x<=160
				&& y>=60 && y<70 && y>150 && y<=160)
			score=6;
		else if (x>=70 && x<80 && x>140 && x<=150
				&& y>=70 && y<80 && y>140 && y<=150)
			score=7;
		else if (x>=80 && x<90 && x>130 && x<=140
				&& y>=80 && y<90 && y>130 && y<=140)
			score=8;
		else if (x>=90 && x<100 && x>120 && x<=130
				&& y>=90 && y<100 && y>120 && y<=130)
			score=9;
		else if (x>=100 && x<=120 && y>=100 && y<=120)
			score=10;
		totalScore += score;
	}
	public void displayScore(){
		System.out.printf("Your total score for this round is %d\n" , totalScore);
	}
	
}
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Color;

public class Circle {
	private int cx, cy, radius;
	public Circle (int x,int y, int r, Color c){
		cx=110;//center of circle 
		cy=110;//center of circle
		radius=r;
	}
	public void drawCircle(Graphics g){
		for (radius=110; radius>=10; radius-=10){
			g.fillOval(cx-radius, cy-radius, 2*radius, 2*radius);
			switch (radius/10){
			case 1:
			case 2:
			case 3:	
			g.setColor(Color.yellow);
			break;
			case 5:
			case 4:
			g.setColor(Color.red);
			break;
			case 7:
			case 6:
			g.setColor(Color.blue);
			break;
			case 9:
			case 8:
			g.setColor(Color.black);
			break;
			case 11:
			case 10:	
			g.setColor(Color.white);
			break;
		}   
		}
		for (radius=110; radius>=10; radius-=10){
			g.drawOval(cx-radius,cy-radius, 2*radius, 2*radius);	
			switch (radius/10){
				case 1:
				case 2:
				case 3:
				case 4:	
				case 5:
				case 6:
				case 9:
				case 10:
				case 11:	
					g.setColor(Color.black);
					break;
				case 7:
				case 8:
					g.setColor(Color.white);
					break;
		}
		}
	}

}
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;


public class Target extends JPanel {
	Arrow a = new Arrow(220,224);
	Circle c = new Circle(10, 10, 3, Color.cyan);
	private int arrows=0;
	public Target(int n){
		arrows=n;
	}
	public void setArrows(int a){
		arrows=a;
	}
	public int getArrows(){
		return arrows;
	}
	public void paintComponent(Graphics g){
		super.paintComponents(g);
		c.drawCircle(g);
		while (arrows>0)
	 		 a.drawArrow(g);
	 		 arrows--;
	}
}
import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JOptionPane;
public class ShootAtTarget {
	public static void main(String[] args){
		
	int arw;
		arw=
		Integer.parseInt(
			JOptionPane.showInputDialog
			("How many arrows do you want to shoot?"));
		
		JFrame application = new JFrame();
		
		application.add(new Target(arw));
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		application.setSize(220,238);
		application.setVisible(true);
		
	}

}



I'm am having a few problems with this program. First off, let me describe it. I am supposed to create a target by drawing 10 concentric circles(I have done that successfully). Those 10 rings have different point values 10-1 from outside to inside(I don't think my code for that works). Then I am supposed to "shoot" arrows at the target whose points are randomly generated(I can randomly generate the points, but drawing the arrows, I think the code is right but I am implementing it incorrectly). The user chooses the number of arrows to be shot(I believe I have done this correctly and store the number they entered correctly). What I am really having trouble with is getting the point system to work (where an arrow lands on the circle determines the points), and also when I try to import a.drawArrow(g), I am getting an error message of not applicable argument. Thank you for any help you can offer.

Is This A Good Question/Topic? 0
  • +

Replies To: School Assignment

#2 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: School Assignment

Posted 10 October 2008 - 01:17 PM

  application.add(new Target(arw));



Shouldn't that be:

  application.getContentPane().add(new Target(arw));



Also - your main problem is your infinite loop:

while (arrows>0)
              a.drawArrow(g);
              arrows--;



This loop only considers a.drawArrow(g); to be part of the loop, and never executes the arrows--; statement. Are you by any chance a Python user?

while (arrows>0) {
              a.drawArrow(g);
              arrows--;
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1