I was attempting to write my own simple version of the Piano Tiles game app in Java just as a fun exercise.
GameLogic.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class GameLogic extends JPanel
{
Tile[][] tileArray = new Tile[4][10];
int lowestIndex;
int targetTileIndex;
int counter = 0;
boolean calculating = false;
boolean gameStart = false;
public GameLogic()
{
super();
this.setPreferredSize(new Dimension(500, 400));
int tempY = 200;
int tempX = 0;
lowestIndex = 0;
targetTileIndex = 0;
for(int y = 0; y<10; y++)
{
int tempRandom = randomNum();
for(int x = 0; x<4; x++)
{
tileArray[x][y] = new Tile(tempX, tempY);
if(x == tempRandom)
{
tileArray[x][y].activate();
}
tempX+=65;
}
tempY-=100;
tempX = 0;
}
final javax.swing.Timer moveTimer = new javax.swing.Timer(1, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int y = 0; y<10; y++)
{
for(int x = 0; x<4; x++)
{
tileArray[x][y].moveDown();
}
}
if(tileArray[0][lowestIndex].getYPos()>=400)
{
int tempRandom = randomNum();
for(int x = 0; x<4; x++)
{
tileArray[x][lowestIndex].setYPos(-600);
//temp
tileArray[x][lowestIndex].deactivate();
if(x == tempRandom)
{
tileArray[x][lowestIndex].activate();
}
}
if(lowestIndex!=9)
{
lowestIndex++;
}
else
{
lowestIndex = 0;
}
}
repaint();
}
}
);
class MyMouseListener implements MouseListener
{
@Override
public void mouseClicked(MouseEvent e)
{
counter++;
System.out.println(counter);
System.out.println("Key!");
if(gameStart == false)
{
moveTimer.start();
}
// TODO Auto-generated method stub
int x = e.getX();
int y = e.getY();
int targetTileNumber = -1;
int targetX;
int targetY;
for(int a = 0; a<4; a++)
{
if(tileArray[a][targetTileIndex].getStatus())
{
targetTileNumber = a;
}
}
targetX = targetTileNumber*65;
targetY = tileArray[targetTileNumber][targetTileIndex].getYPos();
if(x>=targetX && x<=(targetX+65) && y>=targetY && y<=(targetY+100))
{
tileArray[targetTileNumber][targetTileIndex].deactivate();
if(targetTileIndex!=9)
{
targetTileIndex++;
}
else
{
targetTileIndex = 0;
}
}
else
{
}
repaint();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
this.addMouseListener(new MyMouseListener());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for(int y = 0; y<10; y++)
{
for(int x = 0; x<4; x++)
{
tileArray[x][y].paintComponent(g2);
}
}
}
public int randomNum()
{
Random rand = new Random();
return rand.nextInt(4);
}
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLayout(new FlowLayout());
f.add(new GameLogic());
f.pack();
f.setVisible(true);
}
}
Tile.java
import javax.swing.*;
import java.awt.*;
public class Tile extends JComponent
{
boolean activated = false;
int length;
int width;
int xPos;
int yPos;
public Tile(int xPos, int yPos)
{
super();
length = 100;
width = 65;
this.xPos = xPos;
this.yPos = yPos;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if(activated)
{
g2.setColor(Color.BLACK);
}
else
{
g2.setColor(Color.WHITE);
}
g2.fillRect(xPos, yPos, width, length);
g2.setColor(Color.BLACK);
g2.drawRect(xPos, yPos, width, length);
}
public void activate()
{
activated = true;
}
public void deactivate()
{
activated = false;
}
public boolean getStatus()
{
return activated;
}
public void moveDown()
{
this.yPos+=1;
repaint();
}
public int getYPos()
{
return yPos;
}
public void setYPos(int yPos)
{
this.yPos = yPos;
}
public void paint()
{
this.repaint();
}
}
Now one issue I am having is regarding the mouse listener. Rapid clicking causes it to not log every click skipping some instances of the click, which kind of causes issues regarding the logic checking at higher speeds of scrolling. I wasn't sure whether this is an issue with me writing in too many calculations at the instance of a mouse click. The only reasons I could come up with were either that or me creating some kind of buffer in order to facilitate rapid clicking.
Additionally, is my code okay overall or is my approach completely inefficient/off/wrong in any way?