Results 1 to 3 of 3
  1. #1
    eliCanzee is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default What I need to do more in this prog??

    PHP Code:
    ***** 이건 CannonBall의 부모클래스 Ball.java입니다.*****
    
    import java.awt.*;
    
    public class Ball  {
        protected Rectangle location;
        protected double dx;
        protected double dy;
        protected Color color;
            
        public Ball (int x, int y, int r)
        {
            location = new Rectangle(x-r, y-r, 2*r, 2*r);
            dx = 0;
            dy = 0;
            color = Color.blue;
        }
        
            // functions that set attributes
        public void setColor (Color newColor)
                    { color = newColor;    }
    
        public void setMotion (double ndx, double ndy)
                    { dx = ndx; dy = ndy; }
        
            // functions that access attributes of ball
        public int radius ()
                    { return location.width / 2; }
        
        public int x ()
                    { return location.x + radius(); }
        
        public int y ()
                    { return location.y + radius(); }
        
        public double xMotion ()
                    { return dx; }
    
        public double yMotion ()
                    { return dy; }
    
        public Rectangle region () { return location; }
    
            // functions that change attributes of ball
        public void moveTo (int x, int y)
                    { location.setLocation(x, y); }
    
        public void move ()
                    { location.translate ((int) dx, (int) dy); }
        
        public void paint (Graphics g)
        {
            g.setColor (color);
            g.fillOval (location.x, location.y, location.width, location.height);
        }
    }
    
    
    
    
    ***** 이건 CannonWorld.java 입니다. ******
    
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.Graphics;
    import javax.swing.JOptionPane;
    import java.awt.Color;    
    
    class CannonWorld extends Frame{
    
        public static void main (String [] args){
            CannonWorld world = new CannonWorld();
            world.show();
        }
        
        public static final int FrameWidth = 600;
        public static final int FrameHeight = 400;
            
             
        public int angle = 45;
        public String message = "Angle : " + angle;
        public CannonBall cannonBall = null;
        public static final int barrelLength = 30;
        public static final int barrelWidth = 10;
        public Scrollbar slider;
        public String name;
        public int name1;
        public final int RED   = 0;         // Java doesn't have enums (yet),
          public final int GREEN = 1;         //   so we get to do this kind
          public final int BLUE  = 2;   
    /*    private class FireButtonListener implements ActionListener{
            public void actionPerformed (ActionEvent e){
                double radianAngle = angle * Math.PI / 180.0;
                double sinAngle = Math.sin(radianAngle);
                double cosAngle = Math.cos(radianAngle); 
                
                cannonBall = new CannonBall(
                     20 + (int) (barrelLength * cosAngle),
                     dy(5+(int) (barrelLength * sinAngle)),
                     5,12 * cosAngle, -12 * sinAngle);
                repaint();
            }
        }*/
        
        public interface MouseListener{
            public void mouseClicked(MouseEvent e);
            public void mouseEntered(MouseEvent e);
            public void mouseExited(MouseEvent e);
            public void mousePressed(MouseEvent e);
            public void mouseReleased(MouseEvent e);
        }
        
        private class MouseKeeper extends MouseAdapter{
         public void mousePressed(MouseEvent e) {    
            int x=e.getX();        
            int y=e.getY();        
            if ((x>FrameWidth - 40) && (y>FrameHeight - 40)){
                double radianAngle = angle * Math.PI / 180.0;
                double sinAngle = Math.sin(radianAngle);
                double cosAngle = Math.cos(radianAngle); 
                
                cannonBall = new CannonBall(
                     550 - (int) (barrelLength * cosAngle),
                     dy(5+(int) (barrelLength * sinAngle)),
                     5,-12 * cosAngle, -12 * sinAngle);
                repaint();
            }
         }
       }     
        private class QuitButtonListener implements ActionListener{
                public void actionPerformed (ActionEvent e){
                System.exit(0);
                }
        }
        
        private class ScrollBarListener implements AdjustmentListener{
            public void adjustmentValueChanged ( AdjustmentEvent e){
                angle = slider.getValue();
                message = "Angle: " + angle;
                repaint();
            }
        }    
       
        public CannonWorld () {
        
            setSize (FrameWidth,FrameHeight);
            setTitle ("Cannon Game");
            
            name = JOptionPane.showInputDialog("Hold 'Red', 'Green', or 'Blue' constant?");
         
         
            if(name.equalsIgnoreCase("RED"))        // do not use ==
                name1 = RED;
             else if(name
             .equalsIgnoreCase("GREEN"))
                name1= GREEN;
             else if(name.equalsIgnoreCase("BLUE"))
                name1 = BLUE;
             else
                name1 = BLUE;
         
        /*    Button      = new Button("fire");
            fire.addActionListener(new FireButtonListener());
            add ("North", fire); */
            
            this.addMouseListener(new MouseKeeper());
            
            slider = new Scrollbar(Scrollbar.VERTICAL,angle,5,0,90);
            slider.addAdjustmentListener(new ScrollBarListener());
            add ("West", slider);
            
            Button quit = new Button("quit");
            quit.addActionListener(new QuitButtonListener());
            add ("North", quit);
        }
        
        public int dy (int y){
            return FrameHeight -y;
        }    
        
        public void paint (Graphics g){
            int x=550;  //location of cannnon
            int y=5;
            double radianAngle = angle*Math.PI/180.0;
            int lv=(int) (barrelLength*Math.sin(radianAngle));
            int lh=(int) (barrelLength*Math.cos(radianAngle));
            int sv=(int) (barrelWidth*Math.sin(radianAngle+Math.PI/2));
            int sh=(int) (barrelWidth*Math.cos(radianAngle+Math.PI/2));
        
            // draw cannon
            
            g.setColor(Color.green);
            g.drawLine(x,dy(y),x-lh,dy(y+lv));
            g.drawLine(x-lh, dy(y+lv),x-lh-sh ,dy(y+lv+sv));
            g.drawLine(x-lh-sh,dy(y+lv+sv),x-sh,dy(y+sv));
            g.drawLine(x-sh,dy(y+sv),x,dy(y));
            g.drawOval(x-4,dy(y+10),12,12);
            
             //draw target
         
             g.setColor(Color.red);
             g.fillRoundRect(FrameWidth-550,dy(12),50,10,6,6);
          
            //draw circle button
             g.setColor(Color.yellow);
             g.fillRect(FrameWidth-40,FrameHeight-40,30,30);
             g.setColor(Color.red);
             g.fillOval(FrameWidth-40,FrameHeight-40,30,30);     
            
            
            
             //draw cannon ball
             
             if(cannonBall!=null){
                 cannonBall.move();
                cannonBall.paint(g);
                if (dy(cannonBall.y())>0)
                 repaint();
                else {
                    int targetX=FrameWidth-550;
                    if((cannonBall.x() > targetX) &&
                            (cannonBall.x() < (targetX +50)))
                        message="You Hit It!";
                    else
                        message="Missed!";
                    cannonBall=null;
                    }
                }
            
                //draw message
                g.drawString(message,FrameWidth/2,FrameHeight/2);
            }    
        }        
    
    
    
    
    ***** 이건 CannonBall.java 입니다. ******
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    
    public class CannonBall extends Ball {
         final int RED   = 0;         // Java doesn't have enums (yet),
        final int GREEN = 1;         //   so we get to do this kind
        final int BLUE  = 2; 
                
        public CannonBall (int sx, int sy, int r, double dx, double dy)
        {
            super (sx, sy, r);
            setMotion(dx,dy);
        }
        
        public static final double GravityEffect = 0.3;
    
        public void move(){
         dy=dy+GravityEffect;
          
        switch (name1) 
        {
            case RED:
                super.setColor(Color.red);
                super.move();
                break;
        
            case BLUE:
                super.setColor(Color.blue);
                super.move();
                break;
        
            case GREEN:
                super.setColor(Color.green);
                super.move();
                break;
        
            default :
                super.move();
                break;
        }
      }
    }

  2. #2
    eliCanzee is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    This cannot work because I have a problem wiht :
    switch (name1)
    Can anybody help ??

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    the int variable name1 was declared in the CannonWorld class and is invisible to the CannonBall class. You may want to give your CannonBall class an int name1 variable and a public method with which to set this int variable inside of the CannonBall object before you call move on it.

Similar Threads

  1. java annotations - small prog
    By sebo in forum New To Java
    Replies: 5
    Last Post: 10-24-2008, 06:48 AM
  2. Replies: 1
    Last Post: 07-13-2008, 04:16 PM
  3. need a prog on java 2d applet
    By bachelorswalk in forum Java Applets
    Replies: 0
    Last Post: 12-25-2007, 06:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •