Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,139 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,087 people online right now. Registration is fast and FREE... Join Now!




School Assignment

 
Reply to this topicStart new topic

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

gallor889
post 10 Oct, 2008 - 09:56 AM
Post #1


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 1

CODE
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.
User is offlineProfile CardPM

Go to the top of the page

JeroenFM
post 10 Oct, 2008 - 12:17 PM
Post #2


D.I.C Head

Group Icon
Joined: 30 Jun, 2008
Posts: 182



Thanked 9 times

Dream Kudos: 100
My Contributions


CODE

  application.add(new Target(arw));


Shouldn't that be:

java

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


Also - your main problem is your infinite loop:

java

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?

java

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

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 12:25PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month