- 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();
}
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
Last edited: