Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Tekkerue (posted 2014-08-16 06:22:17, viewed 43 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236  
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;
import javax.imageio.ImageIO;

public class TimingCheck extends JFrame implements ActionListener {
   static TimingCheck timingCheck;
   
   int               frameRate = 100;
   int               targetLoopDelay = (int)(1000 / frameRate);
   
   JPanel            mainPanel;   
   JPanel            center;
   JPanel            top;
   JPanel            middle;
   JPanel            bottom;
   
   JButton            buttonPlay;
   
   JLabel[]            beatDisplay = new JLabel[2];
   Color[]             colors = new Color[] { Color.green, Color.yellow };
   long[]            labelIndex = new long[2];
   int[]               currentColor = new int[2];
   int[]               currentCount = new int[2];
   
   BufferedImage[]    animation = new BufferedImage[2];
   AnimationPanel[]   animationPanel = new AnimationPanel[2];
   int[]               imageIndex = new int[] { 0, 0 };
   boolean[]         rightDirection = new boolean[] { true, true };
   
   long               frameCount;
   long[]            animationFrame = new long[2];
   long[]            beatDisplayFrame = new long[2];
   
   boolean            isPlaying = false;
   long               startTime;
   long               currentTime;
   long               nextTime;
   long               sleepTime;
   long               loopCount;
   int               skippedFrames = 1;
   int               maxSkippedFrames = 4;
   
   Audio               audio = new Audio();
   
   public static void main (String[] args) {      
      timingCheck = new TimingCheck();
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            timingCheck.createAndShowUI();
         }
      });
   }
   
   public TimingCheck(){      
      try {
         animation[0] = ImageIO.read(getClass().getResource("animation.jpg"));
         animation[1] = ImageIO.read(getClass().getResource("animation.jpg"));
      } catch (IOException ioe){}
      
      for (int i=0; i<2; i++){
         animationPanel[i] = new AnimationPanel(animation[i]);
         animationPanel[i].setMinimumSize(new Dimension(121,100));
         animationPanel[i].setPreferredSize(new Dimension(121,100));
      }
   }
   
   public void createAndShowUI(){
      initMainPanel();
      initMainFrame();
      addContent();
   }
   private void initMainPanel(){
      mainPanel = new JPanel();
      mainPanel.setLayout(new BorderLayout());
      mainPanel.setPreferredSize(new Dimension(400,400));
      mainPanel.setOpaque(true);
   }
   private void initMainFrame(){
      setContentPane(mainPanel);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setResizable(false);
      pack();
      setLocationRelativeTo(null);
      setVisible(true);
   }
   private void addContent(){
      center = new JPanel();
      center.setOpaque(true);
      
      top = new JPanel();
      top.setOpaque(true);
      top.setPreferredSize(new Dimension(400, 100));
      middle = new JPanel();
      middle.setOpaque(true);
      middle.setPreferredSize(new Dimension(400, 60));
      bottom = new JPanel();
      bottom.setOpaque(true);
      bottom.setPreferredSize(new Dimension(400, 120));
      
      buttonPlay = new JButton("PLAY / STOP");
      buttonPlay.addActionListener(this);
      buttonPlay.setPreferredSize(new Dimension(200, 50));
      
      for (int i=0; i<2; i++){
         beatDisplay[i] = new JLabel("S" + (i+1));
         beatDisplay[i].setPreferredSize(new Dimension(160, 50));
         beatDisplay[i].setHorizontalAlignment(SwingConstants.CENTER);
         beatDisplay[i].setOpaque(true);
         beatDisplay[i].setFont(new Font("Serif", Font.BOLD, 40));
         animationPanel[i].setSubImage(imageIndex[i]);
      }
      
      top.add(buttonPlay);
      middle.add(beatDisplay[0]);
      middle.add(beatDisplay[1]);
      bottom.add(animationPanel[0]);
      bottom.add(animationPanel[1]);
      center.add(top);
      center.add(middle);
      center.add(bottom);
      mainPanel.add(center, BorderLayout.CENTER);
   }
   
   public void actionPerformed(ActionEvent e) {
      if (!isPlaying) {
         isPlaying = true;
         
         audioStart();
         new Thread(loop).start();
      }
      else {
         isPlaying = false;
      }
   }
   
   public void audioStart(){
      audio.isPlaying = true;
      audio.createPlayer();         
      new Thread(audio).start();
   }
   
   // LOOP
   public Runnable loop = new Runnable(){
      public void run(){
         loopCount = 1;
         nextTime = System.currentTimeMillis();
         
         while (isPlaying){
            currentTime = System.currentTimeMillis();
            
            if (currentTime >= nextTime){
               update();
               nextTime += targetLoopDelay;
               
               if ((currentTime < nextTime) || (skippedFrames > maxSkippedFrames)){
                  edtPaint();
                  skippedFrames = 1;
               }
               else{
                  skippedFrames++;
               }
               
               frameCount++;
            }
            else{
               sleepTime = (nextTime - currentTime);
               try {
                  Thread.sleep(sleepTime);
               } catch(InterruptedException ie){}
            }         
         }
      }
   };
   
   // UPDATE
   public void update(){
      for (int i=0; i<2; i++){
         updateAnimation(i);
         updateBeatDisplay(i);
      }
   }
   public void updateAnimation(int i){
      if (frameCount >= animationFrame[i]){            
         animationPanel[i].setSubImage(imageIndex[i]);
         
         imageIndex[i] += rightDirection[i] ? 1 : -1;
         if (imageIndex[i] == 0) {
            rightDirection[i] = true;
         }
         if (imageIndex[i] == 8) {
            rightDirection[i] = false;
         }
         
         animationFrame[i] += (long)(frameRate / 8);
         // division rounding fix
         if (frameCount % 25 == 0){
            animationFrame[i]++;
         }
      }
   }
   public void updateBeatDisplay(int i){
      if (frameCount >= beatDisplayFrame[i]){
         currentColor[i] = (int)(labelIndex[i] % 2);
         currentCount[i] = (int)((labelIndex[i] % 4) + 1);
         labelIndex[i]++;
         
         beatDisplayFrame[i] += frameRate;
      }
   }
   
   // PAINT
   public void edtPaint(){
      final int[] finalCount = currentCount;
      final int[] finalColor = currentColor;
      
      SwingUtilities.invokeLater(new Runnable(){
         public void run(){
            for (int i=0; i<2; i++){
               paintAnimation(i);
               paintBeatDisplay(i, finalCount, finalColor);
            }
         }
      });
   }
   public void paintAnimation(int i){
      animationPanel[i].repaint();
   }
   public void paintBeatDisplay(int i, int[] finalCount, int[] finalColor){
      beatDisplay[i].setText(""+finalCount[i]);
      beatDisplay[i].setBackground(colors[finalColor[i]]);
   }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68  
import java.io.*;
import javax.sound.sampled.*;

public class Audio extends Thread {
   int tempo = 60;
   
   double frequencyOfTick = 800;
   int sampleRate = 44100;
   int bufferSize = 2000;
   
   byte[] buffer;
   int tickBufferSize;
   double[] tickBuffer;
   SourceDataLine line = null;
   int tickLength = sampleRate / 10;
   
   boolean isPlaying = false;
   
   public Audio(){
      tickBufferSize = 44100;
      generateTick();
   }
   
   public void generateTick(){
      tickBuffer = new double[tickBufferSize];
      for (int i = 0; i < tickLength; i++)
         tickBuffer[i] = Math.sin(2 * Math.PI * i / (sampleRate/frequencyOfTick));
      for (int i = tickLength; i < tickBufferSize; i++)
         tickBuffer[i] = 0;
   }
   
   public void createPlayer(){
      AudioFormat af = new AudioFormat(sampleRate, 16, 1, true, false);
      try {
         line = AudioSystem.getSourceDataLine(af);
         line.open(af);
         line.start();
      }
      catch (Exception ex) { ex.printStackTrace(); }
   }
   
   public void run(){
      buffer = new byte[bufferSize];
      int b=0;
      int t=0;
      
      while (isPlaying) {
         if (t >= tickBufferSize)
            t = 0;
         
         short maxSample = (short) ((tickBuffer[t++] * Short.MAX_VALUE));
         buffer[b++] = (byte) (maxSample & 0x00ff);         
         buffer[b++] = (byte) ((maxSample & 0xff00) >>> 8);
         
         if (b >= bufferSize) {
            line.write(buffer, 0, buffer.length);
            b=0;
         }
      }
      
      destroyPlayer();
   }
   
   public void destroyPlayer(){
      line.drain();
      line.close();
   }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21  
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class AnimationPanel extends JPanel{
   BufferedImage img;
   BufferedImage subImg;
   
   public AnimationPanel(BufferedImage img){
      this.img = img;
      setLayout(null);
   }
   
   public void setSubImage(int frame){
      subImg = img.getSubimage(120*frame, 0, 121, 100);
   }
   
   public void paintComponent(Graphics g){
      g.drawImage(subImg, 0, 0, null);      
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (25 views)
2014-09-21 02:42:18

BurntPizza (16 views)
2014-09-21 01:30:30

moogie (19 views)
2014-09-21 00:26:15

UprightPath (26 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (44 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (100 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!