Commit d0c8f600 authored by 9731301's avatar 9731301

add the main frame and one tank

parent eb723d7f
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="0b3d586d-9f0f-4ca4-b09d-03a200e1e684" name="Default Changelist" comment="" />
<list default="true" id="0b3d586d-9f0f-4ca4-b09d-03a200e1e684" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/GameLoop.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/GameState.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/ThreadPool.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/InitialFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/InitialFrame.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/MainFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/MainFrame.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/MainInitialPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/MainInitialPanel.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/MainPanel.java" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
......@@ -14,6 +25,9 @@
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="1dzFnqOFYxXXFNWi9beGa9Sc8UF" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
......@@ -22,6 +36,7 @@
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
</component>
......@@ -63,6 +78,8 @@
<workItem from="1593420197831" duration="14275000" />
<workItem from="1593598248640" duration="34000" />
<workItem from="1593723764237" duration="20000" />
<workItem from="1595279525335" duration="2577000" />
<workItem from="1595311289843" duration="13950000" />
</task>
<servers />
</component>
......@@ -70,37 +87,41 @@
<option name="version" value="1" />
</component>
<component name="WindowStateProjectService">
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1593430072090">
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1595312273481">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1593430072090" />
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1595312273481" />
<state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes" timestamp="1593430104059">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes/0.0.1536.824@0.0.1536.824" timestamp="1593430104059" />
<state width="1493" height="210" key="GridCell.Tab.0.bottom" timestamp="1593457418246">
<state width="1493" height="89" key="GridCell.Tab.0.bottom" timestamp="1595328847854">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="210" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1593457418246" />
<state width="1493" height="210" key="GridCell.Tab.0.center" timestamp="1593457418246">
<state width="1493" height="89" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1595328847854" />
<state width="1493" height="89" key="GridCell.Tab.0.center" timestamp="1595328847854">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="210" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1593457418246" />
<state width="1493" height="210" key="GridCell.Tab.0.left" timestamp="1593457418246">
<state width="1493" height="89" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1595328847854" />
<state width="1493" height="89" key="GridCell.Tab.0.left" timestamp="1595328847854">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="210" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1593457418246" />
<state width="1493" height="210" key="GridCell.Tab.0.right" timestamp="1593457418246">
<state width="1493" height="89" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1595328847854" />
<state width="1493" height="89" key="GridCell.Tab.0.right" timestamp="1595328847854">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="210" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1593457418246" />
<state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1593723763226">
<state width="1493" height="89" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1595328847854" />
<state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1595311302683">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1593723763226" />
<state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1595311302683" />
<state x="209" y="33" key="new project wizard" timestamp="1593723788909">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="209" y="33" key="new project wizard/0.0.1536.824@0.0.1536.824" timestamp="1593723788909" />
<state x="511" y="41" key="refactoring.ChangeSignatureDialog" timestamp="1595279815795">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="511" y="41" key="refactoring.ChangeSignatureDialog/0.0.1536.824@0.0.1536.824" timestamp="1595279815795" />
</component>
</project>
\ No newline at end of file
package UI;
/*** In The Name of Allah ***/
/**
* A very simple structure for the main game loop.
* THIS IS NOT PERFECT, but works for most situations.
* Note that to make this work, none of the 2 methods
* in the while loop (update() and render()) should be
* long running! Both must execute very quickly, without
* any waiting and blocking!
*
* Detailed discussion on different game loop design
* patterns is available in the following link:
* http://gameprogrammingpatterns.com/game-loop.html
*
* @author Seyed Mohammad Ghaffarian
*/
public class GameLoop implements Runnable {
/**
* Frame Per Second.
* Higher is better, but any value above 24 is fine.
*/
public static final int FPS = 30;
private MainFrame canvas;
private GameState state;
public GameLoop(MainFrame frame) {
canvas = frame;
}
/**
* This must be called before the game loop starts.
*/
public void init() {
// Perform all initializations ...
state = new GameState();
canvas.addKeyListener(state.getKeyListener());
canvas.addMouseListener(state.getMouseListener());
canvas.addMouseMotionListener(state.getMouseMotionListener());
}
@Override
public void run() {
boolean gameOver = false;
while (!gameOver) {
try {
long start = System.currentTimeMillis();
//
state.update();
canvas.render(state);
//
long delay = (1000 / FPS) - (System.currentTimeMillis() - start);
if (delay > 0)
Thread.sleep(delay);
} catch (InterruptedException ex) {
}
}
}
}
package UI;
/*** In The Name of Allah ***/
import java.awt.event.*;
/**
* This class holds the state of the game and all of its elements.
* This class also handles user inputs, which affect the game state.
*
* @author Seyed Mohammad Ghaffarian
*/
public class GameState {
public int locX, locY, diam;
public boolean gameOver;
private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT;
private boolean mousePress;
private int mouseX, mouseY;
private KeyHandler keyHandler;
private MouseHandler mouseHandler;
public GameState() {
// Initialize the game state and all elements ...
locX = 100;
locY = 100;
diam = 32;
gameOver = false;
keyUP = false;
keyDOWN = false;
keyRIGHT = false;
keyLEFT = false;
mousePress = false;
mouseX = 0;
mouseY = 0;
keyHandler = new KeyHandler();
mouseHandler = new MouseHandler();
keyHandler = new KeyHandler();
mouseHandler = new MouseHandler();
}
/**
* The method which updates the game state.
*/
public void update() {
//
// Update the state of all game elements
// based on user input and elapsed time ...
//
if (mousePress) {
locY = mouseY - diam / 2;
locX = mouseX - diam / 2;
}
if (keyUP)
locY -= 8;
if (keyDOWN)
locY += 8;
if (keyLEFT)
locX -= 8;
if (keyRIGHT)
locX += 8;
locX = Math.max(locX, 0);
locX = Math.min(locX, MainFrame.GAME_WIDTH - diam);
locY = Math.max(locY, 0);
locY = Math.min(locY, MainFrame.GAME_HEIGHT - diam);
}
public KeyListener getKeyListener() {
return keyHandler;
}
public MouseListener getMouseListener() {
return mouseHandler;
}
public MouseMotionListener getMouseMotionListener() {
return mouseHandler;
}
/**
* The keyboard handler.
*/
class KeyHandler extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
keyUP = true;
break;
case KeyEvent.VK_DOWN:
keyDOWN = true;
break;
case KeyEvent.VK_LEFT:
keyLEFT = true;
break;
case KeyEvent.VK_RIGHT:
keyRIGHT = true;
break;
case KeyEvent.VK_ESCAPE:
gameOver = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
keyUP = false;
break;
case KeyEvent.VK_DOWN:
keyDOWN = false;
break;
case KeyEvent.VK_LEFT:
keyLEFT = false;
break;
case KeyEvent.VK_RIGHT:
keyRIGHT = false;
break;
}
}
}
/**
* The mouse handler.
*/
class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
mousePress = true;
}
@Override
public void mouseReleased(MouseEvent e) {
mousePress = false;
}
@Override
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
}
}
......@@ -5,10 +5,17 @@ import javax.swing.*;
public class InitialFrame extends JFrame {
private MainInitialPanel mainInitialPanel;
private boolean isClickOnPlay = false;
public InitialFrame(String title) {
super(title);
setVisible(true);
mainInitialPanel = new MainInitialPanel();
add(mainInitialPanel);
isClickOnPlay = mainInitialPanel.isClickOnPlay();
}
public boolean isClickOnPlay() {
return isClickOnPlay;
}
}
package UI;
/*** In The Name of Allah ***/
import javax.swing.*;
import java.awt.EventQueue;
import javax.swing.JFrame;
/**
* Program start.
*
* @author Seyed Mohammad Ghaffarian
*/
public class Main {
public static void main (String [] args){
InitialFrame initialFrame = new InitialFrame("tank trouble");
initialFrame.setLocation(200,200);
initialFrame.setSize(400, 400);
initialFrame.setVisible(true);
initialFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
public static void main(String[] args) {
// Initialize the global thread-pool
ThreadPool.init();
// Show the game menu ...
// InitialFrame initialFrame = new InitialFrame("Trouble Tank");
System.out.println("yes");
// if (initialFrame.isClickOnPlay()) {
// After the player clicks 'PLAY' ...
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MainFrame frame = new MainFrame("Trouble Tank");
frame.setLocationRelativeTo(null); // put frame at center of screen
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.initBufferStrategy();
// Create and execute the game-loop
GameLoop game = new GameLoop(frame);
game.init();
ThreadPool.execute(game);
// and the game starts ...
}
});
// }
}
}
package UI;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class MainFrame extends JFrame {
public MainFrame (){
MainPanel mainPanel = new MainPanel();
this.add(mainPanel);
this.pack();
private long lastRender;
private BufferStrategy bufferStrategy;
private ArrayList<Float> fpsHistory;
private Image image1;
public static final int GAME_HEIGHT = 720; // 720p game resolution
public static final int GAME_WIDTH = 16 * GAME_HEIGHT / 9; // wide aspect ratio
public MainFrame (String gameName){
lastRender = -1;
setTitle(gameName);
pack();
setSize(GAME_WIDTH , GAME_HEIGHT);
setResizable(false);
int lastRender = -1;
fpsHistory = new ArrayList<>(100);
try{
image1 = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tankBody_blue_outline.png"));
}
catch(IOException e){
System.out.println(e);
}
}
/**
* This must be called once after the JFrame is shown:
* frame.setVisible(true);
* and before any rendering is started.
*/
public void initBufferStrategy() {
// Triple-buffering
createBufferStrategy(3);
bufferStrategy = getBufferStrategy();
}
/**
* Game rendering with triple-buffering using BufferStrategy.
*/
public void render(GameState state) {
do {
// The following loop ensures that the contents of the drawing buffer
// are consistent in case the underlying surface was recreated
do {
// Get a new graphics context every time through the loop
// to make sure the strategy is validated
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
try {
doRendering(graphics, state);
} finally {
// Dispose the graphics
graphics.dispose();
}
// Repeat the rendering if the drawing buffer contents were restored
} while (bufferStrategy.contentsRestored());
// Display the buffer
bufferStrategy.show();
// Tell the system to do the drawing NOW;
// otherwise it can take a few extra ms and will feel jerky!
Toolkit.getDefaultToolkit().sync();
// Repeat the rendering if the drawing buffer was lost
} while (bufferStrategy.contentsLost());
}
/**
* Rendering all game elements based on the game state.
*/
// Draw all game elements according
// to the game 'state' using 'g2d' ...
private void doRendering(Graphics2D g2d, GameState state) {
// Draw background
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, GAME_WIDTH , GAME_HEIGHT);
// Draw ball
g2d.setColor(Color.BLACK);
g2d.fillOval(state.locX, state.locY, state.diam, state.diam);
g2d.drawImage(image1 , state.locX , state.locY , null);
// Print FPS info
long currentRender = System.currentTimeMillis();
if (lastRender > 0) {
fpsHistory.add(1000.0f / (currentRender - lastRender));
if (fpsHistory.size() > 100) {
fpsHistory.remove(0); // remove oldest
}
float avg = 0.0f;
for (float fps : fpsHistory) {
avg += fps;
}
avg /= fpsHistory.size();
String str = String.format("Average FPS = %.1f , Last Interval = %d ms",
avg, (currentRender - lastRender));
g2d.setColor(Color.CYAN);
g2d.setFont(g2d.getFont().deriveFont(18.0f));
int strWidth = g2d.getFontMetrics().stringWidth(str);
int strHeight = g2d.getFontMetrics().getHeight();
g2d.drawString(str, (GAME_WIDTH - strWidth) / 2, strHeight+50);
}
lastRender = currentRender;
// Print user guide
String userGuide
= "Use the MOUSE or ARROW KEYS to move the BALL. "
+ "Press ESCAPE to end the game.";
g2d.setFont(g2d.getFont().deriveFont(18.0f));
g2d.drawString(userGuide, 10, GAME_HEIGHT - 10);
// Draw GAME OVER
if (state.gameOver) {
String str = "GAME OVER";
g2d.setColor(Color.WHITE);
g2d.setFont(g2d.getFont().deriveFont(Font.BOLD).deriveFont(64.0f));
int strWidth = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, (GAME_WIDTH - strWidth) / 2, GAME_HEIGHT / 2);
}
}
}
......@@ -6,11 +6,17 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainInitialPanel extends JPanel{
private boolean clickOnPlay;
public MainInitialPanel(){
clickOnPlay = false;
initComponents();
}
public boolean isClickOnPlay() {
return clickOnPlay;
}
public void initComponents(){
JTextField gottenSecretCode = new JTextField();
gottenSecretCode.setPreferredSize(new Dimension(gottenSecretCode.getWidth()+200 , gottenSecretCode.getHeight()+30));
......@@ -21,8 +27,7 @@ public class MainInitialPanel extends JPanel{
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (true){
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
clickOnPlay = true;
}
else {
JOptionPane.showMessageDialog(logInBtn,"error");
......
package UI;
import javax.swing.*;
public class MainPanel extends JPanel {
}
package UI;
/*** In The Name of Allah ***/
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* This class holds a global thread-pool for executing our threads.
*
* @author Seyed Mohammad Ghaffarian
*/
public class ThreadPool {
private static ExecutorService executor;
/**
* Initializes a new CachedThreadPool.
* @see java.util.concurrent.Executors#newCachedThreadPool()
*/
public static void init() {
executor = Executors.newCachedThreadPool();
}
/**
* {@link java.util.concurrent.ExecutorService#execute(java.lang.Runnable)}
*/
public static void execute(Runnable r) {
if (executor == null)
init();
executor.execute(r);
}
/**
* {@link java.util.concurrent.ExecutorService#shutdown()}
*/
public static void shutdown() {
executor.shutdown();
}
/**
* {@link java.util.concurrent.ExecutorService#shutdownNow()}
*/
public static void shutdownNow() {
executor.shutdownNow();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment