Results 1 to 3 of 3
- 05-31-2009, 07:00 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 12
- Rep Power
- 0
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; } } }
- 05-31-2009, 07:01 PM #2
Member
- Join Date
- Apr 2009
- Posts
- 12
- Rep Power
- 0
This cannot work because I have a problem wiht :
switch (name1)
Can anybody help ??
-
Similar Threads
-
java annotations - small prog
By sebo in forum New To JavaReplies: 5Last Post: 10-24-2008, 05:48 AM -
want menu driven prog. who to take user i/p in the middle of the prog.
By Shyam Singh in forum New To JavaReplies: 1Last Post: 07-13-2008, 03:16 PM -
need a prog on java 2d applet
By bachelorswalk in forum Java AppletsReplies: 0Last Post: 12-25-2007, 05:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks