Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

Help! Creating Brick Buster in Java

eyjey_29

The Patriot
Advanced Member
Messages
684
Reaction score
0
Points
26
basically meron na ako nagawa na program.
dadagdagan nalang siya or gagawing brick buster...
sana may makapag modify agad. need lang po talaga mga java master
View attachment 261442

im using eclipse with windows builder pro plugin po
hope may makapansin ng aking kahilingan

post ko narin po sample code:

*Game.java*

package gui;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;


public class Game extends JComponent {

private Ellipse2D.Double ball = new Ellipse2D.Double (100, 100, 15, 15);
private RoundRectangle2D.Double bat = new RoundRectangle2D.Double (200, 200, 100, 10, 20, 20);


private double speed = 10.0;

private int xDirectionBall = 1;
private int yDirectionBall = 1;

private double batSpeed = 10.0;

private BufferedImage buffer;
private boolean checkIntersection = true;

public Game() {
addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {

}


public void mouseMoved(MouseEvent e) {
bat.x = e.getX() - bat.getWidth () /2;
bat.y = e.getY() - bat.getHeight () /2;

}

});

addMouseListener (new MouseAdapter() {

public void mouseClicked(MouseEvent e) {
ball.x = e.getX();
ball.y = e.getY();


}

});

KeyboardFocusManager.getCurrentKeyboardFocusManager() .addKeyEventDispatcher (new KeyEventDispatcher() {
public boolean dispatchKeyEvent (KeyEvent e) {

int key = e.getKeyCode();

switch (key) {
case KeyEvent.VK_UP:
bat.y -= batSpeed;
break;
case KeyEvent.VK_DOWN:
bat.y += batSpeed;
break;
case KeyEvent.VK_LEFT:
bat.x -= batSpeed;
break;
case KeyEvent.VK_RIGHT:
bat.x += batSpeed;
break;
}
return false;

}
});

addComponentListener(new ComponentAdapter() {


public void componentResized(ComponentEvent e) {
buffer = null;

}

});


Cursor hiddenCursor = getToolkit() .createCustomCursor(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), new Point(1, 1), "");
setCursor(hiddenCursor);
}



protected void paintComponent(Graphics g) {

if(buffer == null) {
buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_BGR);
}

Graphics2D g2 = (Graphics2D)buffer.getGraphics();

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2.setColor(Color.black);
g2.fillRect(0, 0, getWidth(), getHeight());

g2.setColor(Color.green);
g2.fill(ball);

g2.setColor(Color.yellow);
g2.fill(bat);

g.drawImage(buffer, 0, 0, null);



}

public void update() {

ball.x += xDirectionBall * speed;
ball.y += yDirectionBall * speed;

if(ball.x < 0) {
xDirectionBall = 1;
ball.x = 0;

}
else if(ball.x > getWidth() - ball.getBounds().width) {
xDirectionBall = -1;
ball.x = getWidth() - ball.getBounds().width;
}

if(ball.y < 0) {
yDirectionBall = 1;
ball.y = 0;

}
else if(ball.y > getHeight() - ball.getBounds().height) {
yDirectionBall = -1;
ball.y = getHeight() - ball.getBounds().height;

}

if(ball.intersects(bat.getBounds2D())) {
if(checkIntersection) {
xDirectionBall = -xDirectionBall;
yDirectionBall = -yDirectionBall;
checkIntersection = false;
}

}
else {
checkIntersection = true;

}


repaint();

}


public void update(Graphics g) {
paint(g);
}
}


*KeyEventDispacher.java*

package gui;

import java.awt.KeyEventDispatcher;
import java.awt.event.KeyEvent;

public class KeyEventDispacher implements KeyEventDispatcher {

@Override
public boolean dispatchKeyEvent(KeyEvent arg0) {
// TODO Auto-generated method stub
return false;
}

}

*Main.java*

package gui;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.Timer;


public class Main extends JApplet implements ActionListener {

private Timer timer;
private Game game;
private StartPanel startPanel;
private CardLayout cards;




@Override
public void destroy() {
System.out.println("destroy");
}

@Override
public void init() {

cards = new CardLayout();
startPanel = new StartPanel();
game = new Game();

startPanel.setListener(new StartPanelListener() {
public void startGame() {
cards.show(Main.this.getContentPane(), "game");


}
});

timer = new Timer(20, this);

setSize(600, 500);
setLayout(cards);



add(startPanel, "start");
add(game, "game");



System.out.println("init");
}

@Override
public void start() {
System.out.println("start");

timer.start();

}

@Override
public void stop() {
System.out.println("stop");

timer.stop();

}

@Override
public void actionPerformed(ActionEvent e) {
game.update();


}

}

*StartPanel.java*

package gui;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class StartPanel extends JPanel {

private StartPanelListener listener;


/**
* Create the panel.
*/
public StartPanel() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{445, 0};
gridBagLayout.rowWeights = new double[]{0.0, 0.0};
setLayout(gridBagLayout);

JLabel lblSwingAppletDemo = new JLabel("Swing Applet Demo");
lblSwingAppletDemo.setFont(new Font("Tahoma", Font.PLAIN, 20));
GridBagConstraints gbc_lblSwingAppletDemo = new GridBagConstraints();
gbc_lblSwingAppletDemo.anchor = GridBagConstraints.SOUTH;
gbc_lblSwingAppletDemo.weighty = 0.5;
gbc_lblSwingAppletDemo.insets = new Insets(0, 0, 5, 0);
gbc_lblSwingAppletDemo.gridx = 0;
gbc_lblSwingAppletDemo.gridy = 0;
add(lblSwingAppletDemo, gbc_lblSwingAppletDemo);

JButton btnNewSimulation = new JButton("New Simulation");
btnNewSimulation.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
fireStartGame();

}
});

GridBagConstraints gbc_btnNewSimulation = new GridBagConstraints();
gbc_btnNewSimulation.insets = new Insets(20, 0, 0, 0);
gbc_btnNewSimulation.anchor = GridBagConstraints.NORTH;
gbc_btnNewSimulation.weighty = 0.5;
gbc_btnNewSimulation.gridx = 0;
gbc_btnNewSimulation.gridy = 1;
add(btnNewSimulation, gbc_btnNewSimulation);

}

public void setListener(StartPanelListener listener) {
this.listener = listener;

}

private void fireStartGame() {
if(listener != null) listener.startGame();

}

}

*StartPanelListener*

package gui;

public interface StartPanelListener {
public void startGame();


}
 

Attachments

  • bounce.jpg
    bounce.jpg
    30.3 KB · Views: 6
Last edited:
up lang! help naman po...
 
Sir, gawa ka pa ng 3 object, isa para sa paddle, sa ball, at bricks,..need mo ilagay sa isang container lahat ng bricks para sa update, checking ng position, rendering and collision...

salamat salamat idol:clap:
 
Back
Top Bottom