Flickering issues with a pong game - java

Hello I am having these flickering issues with this pong game, and was hoping someone else could steer me in the right direction. been at this for like an hour or 2 and can not figure it out.
It would be greatly appreciated ?.? like the ball and score are the worst and disappear almost completely when I embed the game.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Pong extends Applet implements MouseMotionListener, KeyListener {
int doubleb,i,my,bx,by,px,py,compx,compy,width,height,speedx,speedy,bwidth,bheight,pwidth,pheight,score;
boolean started;
private Timer timer1;
#Override
public void init() {
setSize(800,500); //setting screan size, declaring background color and some of my listeners
width = getSize().width;
height = getSize().height;
setBackground(Color.black);
pheight = 120;
pwidth = 15;
bheight = 30;
bwidth = 30;
addKeyListener(this); //listers used for the mouse and keybored input
addMouseMotionListener(this);
px = 35;
compx = width - 35 - pwidth;
newgame();
timer1 = new Timer(10,new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) { //momvement, physics, collusion
height = getSize().height;
width = getSize().width;
bx += speedx;
by += speedy;
if (by <= 0 || by + bheight >= height) {
speedy = -speedy;
}
if (bx <= px + pwidth && by + bheight >= py && by <= py + pheight && bx > px) {
speedx = -speedx;
++score;
}
if (bx + bwidth >= compx && by + bheight >= compy && by <= compy + pheight && bx < compx + pwidth) {
speedx = -speedx;
}
if (speedx < 0) {
if (compy + pheight / 2 != height / 2) {
if (compy + pheight / 2 > height / 2) {
compy -= -speedx;
}
else {
compy += -speedx;
}
}
}
else {
if (by + bheight / 2 <= compy + pheight / 2) {
compy -= speedx;
}
else {
compy += speedx;
}
}
if (compy < 0) {
compy = 0;
}
if (compy + pheight > height) {
compy = height - pheight;
}
if (bx + bwidth < 0) {
py = height / 2 - pheight / 2;
timer1.stop();
started = false;
}
repaint();
}
});
}
#Override
public void mouseMoved(MouseEvent e) {
if (started) {
my = e.getY();
if (my + pheight / 2 > height) {
my = height - pheight / 2;
}
if (my < pheight / 2) {
my = pheight / 2;
}
py = my - pheight / 2;
repaint();
}
}
#Override
public void mouseDragged(MouseEvent e) { }
#Override
public void paint(Graphics g) {
Font font1 = new Font("Arial", Font.BOLD, 18); //set font
Font font2 = new Font("Arial", Font.BOLD,40); //set font
g.setColor(Color.white);
g.drawRect(0,0,width - 1,height - 1);
g.fillRect(px,py,pwidth,pheight);
g.fillRect(compx,compy,pwidth,pheight);
g.setFont(font1);
g.drawString("Score: " + score,20,20); //paints the sorce
if (started) {
g.fillArc(bx,by,bwidth,bheight,0,360);
}
else {
g.setFont(font2); //set font
g.setColor(Color.green); //set color
g.drawString("Pong",width / 2 - 46,height / 2 - 16); //draws text to the screen
g.setFont(font1);
g.drawString("Press 's' to start",width / 2 - 69,height / 2 + 30);
}
}
public void newgame() {
py = height / 2 - pheight / 2;
compy = py;
bx = width / 2 - bwidth / 2;
by = height / 2 - bheight / 2;
speedx = 10;
speedy = 10;
score = 0;
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == 's') {
started = true;
newgame();
timer1.start();
}
}
#Override
public void keyTyped(KeyEvent e) { }
#Override
public void keyReleased(KeyEvent e) { }
}

you might want to look into double buffering
Basically, the issue you're having is that you clear the screen in order to redraw it. However, before the Graphics objects is completely finished updating, it gets drawn to the screen. This results in a half finished screen, and a noticeable flickering effect.
The fix to this is to instead draw your image to a seperate Graphics object first, then when it is done, swap that Graphics object with the one being displayed.
more information can be found here

Related

Ball Collision - Collide with paddle

I'm trying to make a ball switch it's Y-Direction when it collides with the restrictions of the paddle. It is most likely a simple answer. Help would be appreciated. Thank you.
I labeled a couple of things to help.
public class GamePanel extends JPanel implements MouseMotionListener {
int Playerx;
int width;
int height;
// Ball Size
float radius = 20;
float diameter = radius * 2;
// Center of Call
float X = radius + 25;
float Y = radius + 10;
// Direction
float dx = 12;
float dy = 12;
GamePanel(){
setPreferredSize(new Dimension(1440, 900));
setFocusable(true);
setBackground(Color.BLACK);
addMouseMotionListener(this);
Thread thread = new Thread() {
public void run() {
while (true) {
width = getWidth();
height = getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
}else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
if(X-radius>=Playerx && X-radius <- Playerx + 100 && Y+radius>=800 && Y+radius<=810){
dy = -dy;
Y = height - radius;
}//Statement Above is the issue area
repaint();
try {
Thread.sleep(25);
} catch (InterruptedException ex) {
}
}
}
};
thread.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
if(Playerx<=50 && Playerx>= 0){g.fillOval(10,800,100,10);}
if(Playerx>50 && Playerx<1390){g.fillOval(Playerx-50,800,100,10);}
if(Playerx>=1390 && Playerx<1440){g.fillOval(1340,800,100,10);}
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
}
#Override
public void mouseDragged(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
Playerx = e.getX();
repaint();
}
}
I've tried setting different restrictions and different Y-Directions but it doesn't seem to work. The Ball goes straight through the paddle as if it's not there.
I managed to fix it.
I realized that my Playerx was in the middle of the paddle making everything offset 50.
float X = radius;
float Y = radius;
and
public void run() {
while (true) {
counter++;
width = getWidth();
height = getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
}else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}if(X > Playerx-50 && X < Playerx+50 && Y+radius > paddleHeight && Y < 715){
dy = -dy;
Y = paddleHeight - radius;
}

Midpoint Ellipse Algorithm in Java

I am trying to write a program to allow a user to draw an ellipse by using clicks. The user left-clicks at first to select the radius, then right-clicks to select the horizontal radius, then right-clicks again to select the vertical radius. Nothing is drawn after clicking. I don't understand where the error is.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ellipse extends JPanel implements MouseListener{
Graphics P;
public Ellipse()
{
addMouseListener(this);
}
static int Radius = 0;
int CenterX, CenterY, RadiusX, RadiusY;
public void paintComponent(Graphics g)
{
P=g;
EllipseMidpoint(CenterX, CenterY, RadiusX, RadiusY);
}
public void EllipseMidpoint(int Cx, int Cy, int Rx, int Ry)
{
int Rx2 = Rx * Rx;
int Ry2 = Ry * Ry;
int twoRx2 = 2 * Rx2;
int twoRy2 = 2 * Ry2;
int x = 0;
int y = Ry;
int p;
int px= 0;
int py = twoRx2 * y;
PlotEllipsePoint(Cx, Cy, x, y);
//Region 1
p = (int)(Ry2 - (Rx2 * Ry) + (0.25 + Rx2));
while (px < py)
{
x = x + 1;
px = twoRy2 + px;
if (p < 0)
{
p = Ry2 + px + p;
}
else
{
y = y - 1;
py = twoRx2 - py;
p = Ry2 + px - py + p;
}
PlotEllipsePoint(Cx, Cy, x, y);
}
//Region2
p = (int)(Ry2 * (x + 0.5) * (x + 0.5) + Rx2 * (y - 1) * (y - 1) - Rx2 * Ry2);
while (y > 0)
{
y = y - 1;
py = twoRx2 - py;
if (p > 0)
{
p = Rx2 - py + p;
}
else
{
x = x + 1;
px = twoRy2 + px;
p = Rx2 + px - py + p;
}
PlotEllipsePoint(Cx, Cy, x, y);
}
}
public void PlotEllipsePoint(int CX, int CY, int X, int Y)
{
drawPixel(CX + X, CY + Y);
drawPixel(CX - X, CY + Y);
drawPixel(CX + X, CY - Y);
drawPixel(CX - X, CY - Y);
}
public void drawPixel(int x, int y)
{
P.fillOval(x, y, 5, 5);
}
public void mousePressed(MouseEvent e)
{
if (e.getButton() == MouseEvent.BUTTON1)
{
CenterX = e.getX();
CenterY = e.getY();
}
else if (e.getButton() == MouseEvent.BUTTON3)
{
Radius = Radius + 1;
if (Radius == 1)
{
RadiusX = (int) Math.pow((Math.pow((e.getX() - CenterX), 2) + Math.pow((e.getY() - CenterY), 2)), 0.5);
}
else if (Radius == 2)
{
RadiusY = (int) Math.pow((Math.pow((e.getX() - CenterX), 2) + Math.pow((e.getY() - CenterY), 2)), 0.5);
}
PlotEllipsePoint(CenterX, CenterY, RadiusX, RadiusY);
}
}
public static void main(String[] args)
{
JFrame JF = new JFrame("Ellipse");
JF.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JF.setSize(500,500);
Ellipse E = new Ellipse();
JF.getContentPane().add(E);
JF.setVisible(true);
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
Three things immediately jump out...
You should be calling super.paintComponent before doing any custom painting (and there is no need for paintComponent to be public), see Performing Custom Painting and Painting in AWT and Swing for more details
NEVER, EVER maintain a reference to the graphics context, you want something painted, you make a request to the repaint manager and you wait until one of your paint methods is called. Painting is controlled by the repaint manager an repaints may occur at any time, most of the time without yout knowledge or interaction.
You never call repaint to ask the repaint manager to repaint your component...At the end of your mousePressed method, call repaint()...

Detecting Key Presses in Java

I am making a game where there are two balls on the screen. One is computer controlled and moves by itself and the other you control with the left and right keys.
public class StartingPoint extends Applet implements Runnable, KeyListener
{
int x = 356;
int y = 74;
double dx = 5;
double dy = 6;
int radius = 20;
//private Image i;
//private Graphics doubleG;
int x2 = 0;
int y2 = 0;
double dx2 = 0;
double dy2 = 0;
int radius2 = 20;
boolean left = false;
boolean right = false;
#Override
public void init()
{
setSize(800, 600);
setVisible(true);
setFocusable(true);
System.out.println("Game Started!");
System.out.println("init");
}
#Override
public void start()
{
Thread thread = new Thread(this);
thread.start();
System.out.println("start");
}
#Override
public void run()
{
System.out.println("run");
while (true)
{
x += dx;
y += dy;
repaint(); //Goes to update method
if (x + dx > this.getWidth() -radius -1)
{
x = this.getWidth() - radius - 1;
dx = - dx;
}
else if (x + dx < 0 + radius)
{
x = 0 + radius;
dx = -dx;
}
else
{
x += dx;
}
if (y + dy > this.getHeight() -radius -1)
{
y = this.getHeight() - radius - 1;
dy = - dy;
}
else if (y + dy < 0 + radius)
{
y = 0 + radius;
dy = -dy;
}
else
{
y += dy;
}
System.out.println("New stuffs");
x2 += dx2;
y2 += dy2;
repaint(); //Goes to update method
if (x2 + dx2 > this.getWidth() -radius2 -1)
{
x2 = this.getWidth() - radius2 - 1;
dx2 = - dx2;
}
else if (x2 + dx2 < 0 + radius2)
{
x2 = 0 + radius2;
dx2 = -dx2;
}
else
{
x2 += dx2;
}
if (y2 + dy2 > this.getHeight() -radius2 -1)
{
y2 = this.getHeight() - radius2 - 1;
dy2 = - dy2;
}
else if (y2 + dy2 < 0 + radius2)
{
y2 = 0 + radius2;
dy2 = -dy2;
}
else
{
y += dy;
}
try
{
Thread.sleep(17);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void keyPressed(KeyEvent e)
{
System.out.println("KeyPressed");
switch(e.getKeyCode())
{
case KeyEvent.VK_LEFT:
moveLeftPressedCode();
System.out.println("VK LEFT");
break;
case KeyEvent.VK_RIGHT:
moveRightPressedCode();
System.out.println("VK RIGHT");
break;
}
}
public void keyReleased(KeyEvent e2)
{
System.out.println("KeyReleased");
switch(e2.getKeyCode())
{
case KeyEvent.VK_LEFT:
moveLeftReleasedCode();
break;
case KeyEvent.VK_RIGHT:
moveRightReleasedCode();
break;
}
}
public void moveLeftPressedCode()
{
left = true;
System.out.println("");
}
public void moveLeftReleasedCode()
{
left = false;
}
public void moveRightPressedCode()
{
right = true;
}
public void moveRightReleasedCode()
{
right = false;
}
public void keyEventLeft()
{
if(dx2 >= -7)
{
while (left = true)
{
dx2 = - 7;
}
}
}
public void keyEventRight()
{
if(dx2 <= 7)
{
while (right = true)
{
//nothing yet
}
}
}
#Override
public void stop()
{
}
#Override
public void destroy()
{
}
#Override
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
g.setColor(Color.GREEN);
g.fillOval(x2 - radius2, y2 - radius2, radius2 * 2, radius2 * 2);
}
#Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
The problem that I am having is that when I press the left key (or right for that matter), the method doesn't get called and thus is not detecting the key press. How do I fix this or what do I need to add to be able to get the game to detect my key presses? -Thank you.

Java - Objects follow each other, when given diffrent tasks

I have been working on this java application.
So far it has no meaning, just a randomly colored ball bouncing around.
But now, when i wanted to add another ball to the bouncing app, the balls followed each other.
This is my code so far.
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JPanel implements Runnable {
Color color = Color.red;
int dia = 60;
Diameter of the objects.
long delay = 20;
Delay time.
private int x = (int)Math.floor(Math.random() * 580);
private int y = (int)Math.floor(Math.random() * 900);
private int xx = (int)Math.floor(Math.random() * 580);
private int yy = (int)Math.floor(Math.random() * 900);
Above is the objects position.
private int dx = (int)Math.floor(Math.random() * 7);
private int dy = (int)Math.floor(Math.random() * 7);
private int dxx = (int)Math.floor(Math.random() * 7);
private int dyy = (int)Math.floor(Math.random() * 7);
Above is object speed.
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
g.fillOval(x,y,60,60);
g.setColor(color);
g.fillOval(xx,yy,60,60);
}
The graphics.
And below is just calculations, thread.sleep and the JFrame.
public void run() {
while(isVisible()) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
System.out.println("interrupted");
}
move();
repaint();
}
}
public void move() {
if(x + dx < 0 || x + dia + dx > getWidth()) {
dx *= -1;
color = getColor();
}
if(y + dy < 0 || y + dia + dy > getHeight()) {
dy *= -1;
color = getColor();
}
if(xx + dxx < 0 || xx + dia + dxx > getWidth()) {
dxx *= -1;
color = getColor();
}
if(yy + dyy < 0 || yy + dia + dyy > getHeight()) {
dyy *= -1;
color = getColor();
}
x += dx;
y += dy;
xx += dx;
yy += dy;
}
private Color getColor() {
int rval = (int)Math.floor(Math.random() * 256);
int gval = (int)Math.floor(Math.random() * 256);
int bval = (int)Math.floor(Math.random() * 256);
return new Color(rval, gval, bval);
}
private void start() {
while(!isVisible()) {
try {
Thread.sleep(25);
} catch(InterruptedException e) {
System.exit(1);
}
}
Thread thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
public static void main(String[] args) {
MainFrame test = new MainFrame();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(640, 960);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation(dim.width/2-f.getSize().width/2, dim.height/2-f.getSize().height/2);
f.setVisible(true);
test.start();
}
}
I just can not figure it out.
I know the answer is going to be simple.
You should define a single class Ball and create two instances of it rather than repeating the variables and have an x,y co-ordinate and velocity dx and dy inside that class.
The two follow each other ebcause you add the same velocity to both balls all the time:
x += dx;
y += dy;
xx += dx;
yy += dy;

Why is java application running smoother when moving mouse over it? Video included

I'm working with tutorial from this site - "Fixed timestep" section.
Here's the code - http://pastebin.com/QaHgcLaR
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameLoopTest extends JFrame implements ActionListener
{
private GamePanel gamePanel = new GamePanel();
private JButton startButton = new JButton("Start");
private JButton quitButton = new JButton("Quit");
private JButton pauseButton = new JButton("Pause");
private boolean running = false;
private boolean paused = false;
private int fps = 60;
private int frameCount = 0;
public GameLoopTest()
{
super("Fixed Timestep Game Loop Test");
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
JPanel p = new JPanel();
p.setLayout(new GridLayout(1,2));
p.add(startButton);
p.add(pauseButton);
p.add(quitButton);
cp.add(gamePanel, BorderLayout.CENTER);
cp.add(p, BorderLayout.SOUTH);
setSize(500, 500);
startButton.addActionListener(this);
quitButton.addActionListener(this);
pauseButton.addActionListener(this);
}
public static void main(String[] args)
{
GameLoopTest glt = new GameLoopTest();
glt.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object s = e.getSource();
if (s == startButton)
{
running = !running;
if (running)
{
startButton.setText("Stop");
runGameLoop();
}
else
{
startButton.setText("Start");
}
}
else if (s == pauseButton)
{
paused = !paused;
if (paused)
{
pauseButton.setText("Unpause");
}
else
{
pauseButton.setText("Pause");
}
}
else if (s == quitButton)
{
System.exit(0);
}
}
//Starts a new thread and runs the game loop in it.
public void runGameLoop()
{
Thread loop = new Thread()
{
public void run()
{
gameLoop();
}
};
loop.start();
}
//Only run this in another Thread!
private void gameLoop()
{
//This value would probably be stored elsewhere.
final double GAME_HERTZ = 30.0;
//Calculate how many ns each frame should take for our target game hertz.
final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
//At the very most we will update the game this many times before a new render.
//If you're worried about visual hitches more than perfect timing, set this to 1.
final int MAX_UPDATES_BEFORE_RENDER = 5;
//We will need the last update time.
double lastUpdateTime = System.nanoTime();
//Store the last time we rendered.
double lastRenderTime = System.nanoTime();
//If we are able to get as high as this FPS, don't render again.
final double TARGET_FPS = 60;
final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
//Simple way of finding FPS.
int lastSecondTime = (int) (lastUpdateTime / 1000000000);
while (running)
{
double now = System.nanoTime();
int updateCount = 0;
if (!paused)
{
//Do as many game updates as we need to, potentially playing catchup.
while( now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER )
{
updateGame();
lastUpdateTime += TIME_BETWEEN_UPDATES;
updateCount++;
}
//If for some reason an update takes forever, we don't want to do an insane number of catchups.
//If you were doing some sort of game that needed to keep EXACT time, you would get rid of this.
if ( now - lastUpdateTime > TIME_BETWEEN_UPDATES)
{
lastUpdateTime = now - TIME_BETWEEN_UPDATES;
}
//Render. To do so, we need to calculate interpolation for a smooth render.
float interpolation = Math.min(1.0f, (float) ((now - lastUpdateTime) / TIME_BETWEEN_UPDATES) );
drawGame(interpolation);
lastRenderTime = now;
//Update the frames we got.
int thisSecond = (int) (lastUpdateTime / 1000000000);
if (thisSecond > lastSecondTime)
{
System.out.println("NEW SECOND " + thisSecond + " " + frameCount);
fps = frameCount;
frameCount = 0;
lastSecondTime = thisSecond;
}
//Yield until it has been at least the target time between renders. This saves the CPU from hogging.
while ( now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES)
{
Thread.yield();
//This stops the app from consuming all your CPU. It makes this slightly less accurate, but is worth it.
//You can remove this line and it will still work (better), your CPU just climbs on certain OSes.
//FYI on some OS's this can cause pretty bad stuttering. Scroll down and have a look at different peoples' solutions to this.
try {Thread.sleep(1);} catch(Exception e) {}
now = System.nanoTime();
}
}
}
}
private void updateGame()
{
gamePanel.update();
}
private void drawGame(float interpolation)
{
gamePanel.setInterpolation(interpolation);
gamePanel.repaint();
}
private class GamePanel extends JPanel
{
float interpolation;
float ballX, ballY, lastBallX, lastBallY;
int ballWidth, ballHeight;
float ballXVel, ballYVel;
float ballSpeed;
int lastDrawX, lastDrawY;
public GamePanel()
{
ballX = lastBallX = 100;
ballY = lastBallY = 100;
ballWidth = 25;
ballHeight = 25;
ballSpeed = 25;
ballXVel = (float) Math.random() * ballSpeed*2 - ballSpeed;
ballYVel = (float) Math.random() * ballSpeed*2 - ballSpeed;
}
public void setInterpolation(float interp)
{
interpolation = interp;
}
public void update()
{
lastBallX = ballX;
lastBallY = ballY;
ballX += ballXVel;
ballY += ballYVel;
if (ballX + ballWidth/2 >= getWidth())
{
ballXVel *= -1;
ballX = getWidth() - ballWidth/2;
ballYVel = (float) Math.random() * ballSpeed*2 - ballSpeed;
}
else if (ballX - ballWidth/2 <= 0)
{
ballXVel *= -1;
ballX = ballWidth/2;
}
if (ballY + ballHeight/2 >= getHeight())
{
ballYVel *= -1;
ballY = getHeight() - ballHeight/2;
ballXVel = (float) Math.random() * ballSpeed*2 - ballSpeed;
}
else if (ballY - ballHeight/2 <= 0)
{
ballYVel *= -1;
ballY = ballHeight/2;
}
}
public void paintComponent(Graphics g)
{
//BS way of clearing out the old rectangle to save CPU.
g.setColor(getBackground());
g.fillRect(lastDrawX-1, lastDrawY-1, ballWidth+2, ballHeight+2);
g.fillRect(5, 0, 75, 30);
g.setColor(Color.RED);
int drawX = (int) ((ballX - lastBallX) * interpolation + lastBallX - ballWidth/2);
int drawY = (int) ((ballY - lastBallY) * interpolation + lastBallY - ballHeight/2);
g.fillOval(drawX, drawY, ballWidth, ballHeight);
lastDrawX = drawX;
lastDrawY = drawY;
g.setColor(Color.BLACK);
g.drawString("FPS: " + fps, 5, 10);
frameCount++;
}
}
private class Ball
{
float x, y, lastX, lastY;
int width, height;
float xVelocity, yVelocity;
float speed;
public Ball()
{
width = (int) (Math.random() * 50 + 10);
height = (int) (Math.random() * 50 + 10);
x = (float) (Math.random() * (gamePanel.getWidth() - width) + width/2);
y = (float) (Math.random() * (gamePanel.getHeight() - height) + height/2);
lastX = x;
lastY = y;
xVelocity = (float) Math.random() * speed*2 - speed;
yVelocity = (float) Math.random() * speed*2 - speed;
}
public void update()
{
lastX = x;
lastY = y;
x += xVelocity;
y += yVelocity;
if (x + width/2 >= gamePanel.getWidth())
{
xVelocity *= -1;
x = gamePanel.getWidth() - width/2;
yVelocity = (float) Math.random() * speed*2 - speed;
}
else if (x - width/2 <= 0)
{
xVelocity *= -1;
x = width/2;
}
if (y + height/2 >= gamePanel.getHeight())
{
yVelocity *= -1;
y = gamePanel.getHeight() - height/2;
xVelocity = (float) Math.random() * speed*2 - speed;
}
else if (y - height/2 <= 0)
{
yVelocity *= -1;
y = height/2;
}
}
public void draw(Graphics g)
{
}
}
}
After run this code, the ball has kind of lag, but there is still 60 FPS. After I move mouse over application's window and move it in random directions, the ball is moving smoothly. It happens even if window application isn't focused! What's wrong? Can it be fixed?
I'm using Ubuntu 13.04 with Oracle JDK7.
I've found that it happens with every application. Similar things happens even in LWJGL application, but effect of the "lag" is much less than in swing application.
17 sec video showing my problem
http://www.youtube.com/watch?v=J8SBjKncgRw
I had same problem under Kubuntu 13.04
I googled something which works for me: http://www.java-gaming.org/index.php?topic=19224.0
The basic idea is to put Toolkit.getDefaultToolkit().sync(); after drawing something. In your code it should be after drawGame(interpolation);.
The explanation seems to be that the window system manages the update intervals, so it is not Java's fault and only occures with some window mangers.
The repaints should be triggered from a Swing based Timer, which ensures that GUI updates are called on the EDT. See Concurrency in Swing for more details.
This appears to be a bug in the VM since Java 6. I had the same problem and found an ugly, but simple workaround: Create a Robot object and let it press a key or position the mouse in each cycle. The animation will then run smoothly. Example:
final Robot robot = new Robot();
javax.swing.Timer timer = new javax.swing.Timer(initialDelay, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// update your image...
robot.keyPress(62);
}
});
See also: Java animation stutters when not moving mouse cursor
You could invoke setIgnoreRepaint(true) and use a BufferStrategy for drawing that particular component instead. A BufferStrategy allows you to perform the drawing whenever you want. You can also invoke paintComponent methods inside your drawing method.

Categories

Resources