PongPingGameFrame
Code:
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class PongPingGameFrame extends JFrame {
volatile private boolean showConfig;
volatile private static boolean gameRunning;
private ConfigScreen configScreen;
private GamePanel gamePanel;
private Thread configScreenT;
private Thread gamePanelT;
public PongPingGameFrame(){
this.setBounds(300,300,500,500);
this.setGameRunning(false);
this.setShowConfig(true);
gamePanel = new GamePanel(this);
configScreen = new ConfigScreen(this);
configScreenT = new Thread(configScreen);
gamePanelT = new Thread(gamePanel);
this.setVisible(true);
}
public static void main(String[] ar){
EventQueue.invokeLater(new Runnable(){
PongPingGameFrame frame = new PongPingGameFrame();
@Override
public void run() {
if(frame.isGameRunning()){
frame.gamePanelT.start();
System.out.print("GAME PANELLL");
}
else
{
try{
frame.configScreenT.start();
System.out.print("CONFIGGG");
}
catch(NullPointerException n){}
}
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.repaint();
}
});
}
public boolean isShowConfig() { return showConfig; }
public void setShowConfig(boolean showConfig) { this.showConfig = showConfig; }
public boolean isGameRunning() { return gameRunning; }
public void setGameRunning(boolean gameRunning) { this.gameRunning = gameRunning; }
}
GamePanel
Code:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements Runnable {
private PongPingGameFrame frame;
public GamePanel(PongPingGameFrame frame){
this.frame = frame;
this.setBounds(frame.getBounds());
}
public void paintComponent(Graphics g){
Graphics2D g2D = (Graphics2D)g;
g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
}
@Override
public void run() {
frame.add(this);
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ConfigScreen (pause)
Code:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.EventListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ConfigScreen extends JPanel implements ActionListener, Runnable {
/******************************************************************************
* Private members
*
* backgroundColor Color
* width int
* height int
* stickColor Color
* stickSize int
* stickSpeed int
* ballSize int
* gameRunning boolean
* parrentFrame JFrame with no getter and setter
*/
private Color backgroundColor;
private int width;
private int height;
private Color sticksColor;
private int stickSize;
private int stickSpeed;
private int ballSize;
private boolean gameRunning;
private PongPingGameFrame parrentFrame;
/*************************************
* Getter And Setter methods
*/
public Color getBackgroundColor() { return backgroundColor; }
public void setBackgroundColor(Color backgroundColor) { this.backgroundColor = backgroundColor; }
public void setWidth(int w){ this.width = w; }
public int getWidth(){ return this.width; }
public void setHeight(int h){ this.height = h; }
public int getHeight(){return this.height; }
public Color getSticksColor() { return sticksColor; }
public void setSticksColor(Color sticksColor) { this.sticksColor = sticksColor; }
public int getStickSpeed() { return stickSpeed; }
public void setStickSpeed(int stickSpeed) { this.stickSpeed = stickSpeed; }
public int getBallSize() { return ballSize; }
public void setBallSize(int ballSize) { this.ballSize = ballSize; }
public boolean isGameRunning() { return gameRunning; }
public void setGameRunning(boolean gameRunning) { this.gameRunning = gameRunning; }
/**
* End Getter And Setters
*///////////////////////////////////////////////////////////////////////////////////////////////////////
private JButton resumeB;
private JButton startB;
private JButton exitB;
/**********************
* init()
* Set default Background and Sticks Color
*/
public void init(){
this.setBackgroundColor(Color.BLACK);
this.width = parrentFrame.getWidth();
this.height = parrentFrame.getHeight();
this.stickSize = width / 4;
this.setBallSize(stickSize / 4);
this.setStickSpeed(height / 100);
this.setSticksColor(Color.GREEN);
this.setGameRunning(false);
System.out.println(" \ninit");
}
private void initComponents(){
this.resumeB = new JButton("Resume");
resumeB.setBounds(width / 3 - this.getWidth() / 49,
height / 3 - 40,
width / 3,
height / 10 );
resumeB.setEnabled(false);
this.startB = new JButton("Start Game");
startB.setBounds(width / 3 - this.getWidth() / 49,
resumeB.getY() + resumeB.getHeight() + 10,
width / 3,
height / 10 );
startB.addActionListener(this);
this.exitB = new JButton("Exit");
exitB.setBounds(width / 3 - this.getWidth() / 49,
startB.getY() + startB.getHeight() + 30,
width / 3,
height / 10 );
this.setLayout(null);
this.add(resumeB);
this.add(startB);
this.add(exitB);
System.out.println("\ncomponent init");
}
public ConfigScreen(PongPingGameFrame parrentFrame){
this.parrentFrame = parrentFrame;
init();
initComponents();
this.setBounds(parrentFrame.getBounds());
System.out.println("\ntgread init");
}
@Override
public void run() {
parrentFrame.add(this);
this.setVisible(true);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource().equals(startB)){
parrentFrame.setGameRunning(true);
System.out.print("kliketd");
parrentFrame.repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Ovi delovi shto se ticu Akcija i Niti su mi bitni.
Molim iskusne programere da mi daju neku smernicu unapred hvala