1 Replies - 3781 Views - Last Post: 28 January 2011 - 06:57 AM Rate Topic: -----

#1 latha_pushpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-January 11

program on ball breaker using java with AWT

Posted 28 January 2011 - 06:52 AM

001 OuterFrame.java:  

002 import javax.swing.JFrame;    

003 public class OuterFrame extends JFrame{  

004 //This is the outer frame in which the game panel is to be put    

005 OuterFrame()    

006 {     

007 setSize(400,400);    

008 setResizable(false);    

009 setTitle("BALL BREAKER");   

010 setLocationRelativeTo(null);    

011 setVisible(true);    

012 setDefaultCloseOperation(DISPOSE_ON_CLOSE);   

013 }   

014 public static void main(String[] args) {    

015 new OuterFrame();    

016 }    

017 }  

018 Wall.java:  

019 import javax.swing.JPanel;    

020 import java.awt.Graphics;    

021 import java.awt.Graphics2D;    

022 import java.awt.event.ActionEvent;    

023 import java.awt.event.ActionListener;    

024 import java.awt.event.KeyAdapter;    

025 import java.awt.event.KeyEvent;    

026 import java.awt.Color;    

027 import javax.swing.Timer;    

028 import java.awt.Toolkit;   

029 public class Wall extends JPanel implements ActionListener {    

030 private Timer timer;  

031 private BaseBoard;  

032 //This class is the Panel in which the game components will be put    

033 Wall()   

034 {   

035 addKeyListener(new BoardAdapter());    

036 setBackground(Color.yellow);    

037 setForeground(Color.BLACK);    

038 setDoubleBuffered(true); // using a buffer to paint    

039 timer = new Timer(5,this);    

040 timer.start();  

041 BaseBoard baseboard = new BaseBoard();   

042 }    

043 public void actionPerformed(ActionEvent e) {  

044 baseboard.move(e);  

045 repaint();    

046 try {    

047 Thread.sleep(20);    

048 }   

049 catch(Exception ex) {    

050 ex.printStackTrace();   

051 }    

052 }  

053 @Override    

054 public void paint(Graphics g) {    

055 super.paint(g);    

056 Graphics2D g2d = (Graphics2D)g;    

057 g2d.drawImage(baseboard.getBaseBoard(),baseboard.getX(),350,40,15, Color.yellow, this);    

058 g2d.setColor(Color.red);    

059 g2d.fillOval(200,335, 10, 10);    

060 }    

061 private class BoardAdapter extends KeyAdapter {    

062 //If there is any key pressed/relased when the Wall(the panel) is    

063 //displayed then such presses or releases are to be passed to the    

064 //the BaseBoard class for handling such events.    

065 @Override    

066 public void keyPressed(KeyEvent e) {    

067 baseboard.keyPressed(e);    

068 }    

069 @Override    

070 public void keyReleased(KeyEvent e) {    

071 baseboard.keyReleased(e);    

072 }   

073 }    

074 }   

075 BaseBoard.java:  

076 import java.awt.Image;  

077 import javax.swing.ImageIcon;    

078 import java.awt.event.KeyEvent;    

079 Public static void main(String[] args) {   

080 public class BaseBoard {    

081 //This is the class for the BaseBoard is given to user to preventing    

082 //the ball from falling.    

083 //User controls movement of this board using the arrow keys    

084 ImageIcon ii;  

085 Image image;    

086 private String baseboardimg = "baseboard.jpeg";   

087 private int x;    

088 private int dx;    

089 BaseBoard() {    

090 ii = new ImageIcon(this.getClass().getResource(baseboardimg));    

091 image = ii.getImage();    

092 x = 200;    

093 }    

094 public void move() {    

095 x += dx;    

096 }  public int getX() {    

097 return x;    

098 }    

099 public void keyPressed(KeyEvent e) {  

100 int key = e.getKeyCode();    

101 if(key == KeyEvent.VK_LEFT) {    

102 dx = -20;    

103 }    

104 else if(key == KeyEvent.VK_RIGHT) {    

105 dx = 20;    

106 }    

107 }    

108 public void keyReleased(KeyEvent e) {    

109 int key = e.getKeyCode();    

110 if(key == KeyEvent.VK_LEFT) {    

111 dx = 0;    

112 }    

113 if(key == KeyEvent.VK_RIGHT) {    

114 dx = 0;    

115 }    

116 }    

117 public Image getBaseBoard() {    

118 return image;    

119 }    

120 } 


This is the code for the basic game components and the controls, seperate code is required for ball and paddle

(They used the word Puddle which is supposed to be paddle in this case because A puddle is a small accumulation of
 liquid, usually water, on a surface. It can form either by pooling in a depression on the surface, 
 or by surface tension which is not quite relevant with the game component's actual behaviour.) 



Is This A Good Question/Topic? 0
  • +

Replies To: program on ball breaker using java with AWT

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,594
  • Joined: 27-December 08

Re: program on ball breaker using java with AWT

Posted 28 January 2011 - 06:57 AM

I'm pretty sure the code you posted isn't yours. I'm not sure anyone can help you with code that isn't yours when you post it with the line numbers, don't properly indent it, don't state your question, and don't describe your problems or errors.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1