Results 1 to 2 of 2
Thread: java jgrasp pingpong
- 11-26-2010, 11:40 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
java jgrasp pingpong
Hi i was wondering if someone had the time and will to finish my applet (in jgrasp). Its an assignment for school that i really need to finish this weekend. Its the typical pong game, Two boards and a ball bouncing from side to side. I've managed to get the ball bounce around in a the box and I can move the two blocks up and down. Now to the problem, I can't figure out how to get the ball to bounce when it hits the blocks. The applet should also include a point-counting system thats counts every time the ball passes one of the blocks.
any ways here is the two parted applet:
Java Code:/* Interaktivitet - lyssnare*/ import java.awt.*; import java.applet.*; // behövs för lyssnare import java.awt.event.*; import java.awt.Choice; //implementerar lyssnare - actionlistener - för knappar public class StartGUI extends Applet implements ActionListener { Button startButton; Button pauseButton; StartCanvas pc; Choice level = null; Choice bc = null; Panel kn=new Panel(); public void init() { pc=new StartCanvas(this); startButton = new Button("Start game!"); pauseButton = new Button("Pause game!"); level = new Choice(); //lägg till nivå level.add("1"); level.add("2"); level.add("3"); level.add("4"); bc = new Choice(); //lägg till bakgrundsfärg bc.add("Yellow"); bc.add("Green"); bc.add("Gray"); bc.add("White"); // lägg till lyssnare startButton.addActionListener(this); pauseButton.addActionListener(this); setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); //fyll allt c.fill = GridBagConstraints.BOTH; //position c.gridx = 0; c.gridy = 0; //storlek på y c.ipady = getHeight()-25; add(pc, c); //fyll panell först kn.setLayout(new GridLayout(1,4,30,10)); kn.add(startButton); kn.add(pauseButton); kn.add(bc); kn.add(level); //och till sist hela applet c.ipady = 25; c.gridx = 0; c.gridy = 1; add(kn, c); } public void enableAll(boolean b) { level.setEnabled(b); bc.setEnabled(b); startButton.setEnabled(b); } //utförs när någon knapp trycks public void actionPerformed(ActionEvent evt) { //om det är ok knapp, rita om if (evt.getSource() == startButton) { pc.setVel(level.getSelectedItem()); pc.setPause(false); pc.setBackgroundCol(bc.getSelectedItem()); enableAll(false); } if(evt.getSource() ==pauseButton) { pc.setPause(true); enableAll(true); } } } /*trådar - animering*/ import java.awt.*; import java.applet.*; import java.awt.event.*; import java.awt.event.KeyEvent; //Implementera Runnable för trådar public class StartCanvas extends Canvas implements Runnable,KeyListener { public StartGUI sp; //tråd final int rx=10; final int ry=30; private Thread bollThread; //bållens x,y - koordinater private int x=50,y=100; //bollens hastighet x och y koordinater private int vx=5,vy=5; int ryh=150; int ryv=150; //true - om tråden körs , annars falsk boolean running = true; //för att pausa tråden boolean pause = true; //bollens radie private final int BR=10; //hur fort bilden uppdateras - konstant - nivå 1 private final int VEL=100; //hur fort bilden uppdateras private int vel=40; //bakgrundsfärg Color bgColor=Color.white; public StartCanvas(StartGUI p) { sp=p; //skapa och starta tråden bollThread= new Thread(this); bollThread.start(); addKeyListener( this ); } public void setVel(String nr) { vel=VEL/Integer.parseInt(nr); } public void update(Graphics g) { Graphics offgc; //skapa en buffer för att lagra bilden Image offscreen = createImage(getWidth(), getWidth()); offgc = offscreen.getGraphics(); // tömm hela ytan = fyll allt med bakgrundsfärg offgc.setColor(getBackground()); offgc.fillRect(0, 0,getWidth(), getWidth()); offgc.setColor(getForeground()); //omritning - lagra hela bilden i buffer paint(offgc); //när hela bilden blir klar skicka den till skärmen g.drawImage(offscreen, 0, 0, this); } //stoppa tråden , när appleten avslutas public void destroy() { // stoppa tråden running = false; bollThread = null; } public void paint(Graphics g) { setBackground(bgColor); //kolla X gränser if (checkXBoundaries()) vx=-vx; //kolla Y gränser if (checkYBoundaries()) { vy=-vy; } //uppdatera x koordinater x+=vx; y+=vy; g.setColor(new Color(0,0,255)); g.fillArc(x,y, BR, BR,0,360); //rita en röd cirkel g.fillRect(10,ryv,rx,ry); g.fillRect(360,ryh,rx,ry); } //bestäm färgen public void setBackgroundCol(String col) { if(col.equals("Green")) bgColor=Color.green; if(col.equals("Yellow")) bgColor=Color.yellow; if(col.equals("Gray")) bgColor=Color.gray; if(col.equals("White")) bgColor=Color.white; } //ha en paus public void setPause(boolean p) { pause=p; } public void keyPressed( KeyEvent e ){} public void keyReleased( KeyEvent e ) { } public void keyTyped( KeyEvent e ) { char c = e.getKeyChar(); if ( c == 'w' && ryv>5) ryv=ryv-5; if (c== 'd' && ryv+ry < getHeight()-5) ryv=ryv+5; if (c== 'o' && ryh >5) ryh=ryh-5; if (c== 'k' && ryh+ry < getHeight()-5) ryh=ryh+5; repaint(); e.consume(); } //kolla x och y gränser private boolean checkYBoundaries() { return (y<0 && BR<=ry || y+BR>getHeight()); } private boolean checkXBoundaries() { return (x<0 && BR<=rx|| x+BR>getWidth()); } //omritningen händer i denna tråd public void run() { //lopp där uppdateringen (omritningen) skers while (running) { if(!pause) { try { // vänta vel ms bollThread.sleep(vel); repaint(); } catch (InterruptedException e) { System.out.println(e); } } } } }
-
Please, this is not a homework finishing forum.
If you want our help, and are willing to do the work yourself, then please ask us specific questions about bugs in your code or concepts you're not clear on and we'll be more than happy to help you. Best of luck.
edit: Also consider creating more classes including a Ball class and a Paddle class. Then giving the game a "model" -- a non-gui class that runs the game and that the GUI portion uses to paint itself with. This assignment will be a lot easier if you can divide out your concepts so you can work on the ball bouncing logic in isolation of the GUI.Last edited by Fubarable; 11-27-2010 at 12:10 AM.
Similar Threads
-
Need hmwk help with Arrays in jgrasp
By cmps280CD in forum New To JavaReplies: 9Last Post: 09-14-2010, 03:23 AM -
jGRASP debugger
By TsAmE in forum New To JavaReplies: 2Last Post: 02-23-2010, 09:35 PM -
jGrasp compiler issues VISTA (cause we've all been here at one point)
By marcheghiano in forum Other IDEsReplies: 0Last Post: 11-24-2009, 04:49 PM -
JGrasp simple exercise?
By Boomer1 in forum Other IDEsReplies: 2Last Post: 11-02-2009, 04:23 AM -
GUI with jGRASP
By Matt Sakko in forum Other IDEsReplies: 0Last Post: 03-14-2009, 10:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks