Oct 5, 2014 at 5:38 PM
Join Date: Aug 3, 2014
Location: worm hole
Posts: 362
Age: 22
I'm going at this the wrong way I bet, but I've tried to make a thing using all my Java knowledge learnt in two days that's meant to be like a green square moving smoothly when keys are pressed.
It works, but it stutters and looks ugly.
Ideas would be neat.
Code:
Unless I'm doing something crucially impacting to the universe or this thing I want to do with my coding style, don't comment on it.
All I request is help making movement look smooth.
It works, but it stutters and looks ugly.
Ideas would be neat.
Code:
Code:
package redabsol0.Game1.Main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Game1Main extends JFrame implements Runnable, KeyListener
{
Thread t;
static int i = 0;
static boolean isDebugging = true;
static boolean isPinging = false;
static int gameState = 0;
public static int x = 10, y = 100;
static Game1Main main;
Game1Main()
{
main = this;
t = new Thread(this, "Game Runtime");
t.start();
final JPanel p = new JPanel();
p.setBackground(Color.RED);
if (isDebugging)
{
JMenuBar bar = new JMenuBar();
JMenu debug = new JMenu("Debug");
bar.add(debug);
JMenuItem bgColorChange = new JMenuItem("Change content pane");
bgColorChange.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
if(gameState == 0)
{
p.setBackground(Color.BLUE);
gameState = 1;
}
else
{
p.setBackground(Color.RED);
gameState = 0;
}
}
});
JMenuItem quit = new JMenuItem("Quit");
quit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
debug.add(bgColorChange);
debug.add(quit);
bar.add(debug);
this.setJMenuBar(bar);
}
this.setUndecorated(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setVisible(true);
this.add(p);
}
public static void main(String args[])
{
new Game1Main();
while (true)
{
if (isPinging)
{
i++;
}
}
}
public void run()
{
addKeyListener(this);
while (true)
{
repaint();
if (isPinging)
{
System.out.println("Ping: " + Integer.toString(i / 1000000));
i = 0;
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.GREEN);
g.fillRect(x, y, 100, 100);
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_D)
{
x+=10;
}
if(e.getKeyCode() == KeyEvent.VK_A)
{
x-=10;
}
if(e.getKeyCode() == KeyEvent.VK_W)
{
y-=10;
}
if(e.getKeyCode() == KeyEvent.VK_S)
{
y+=10;
}
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void keyTyped(KeyEvent e)
{
}
}
All I request is help making movement look smooth.