M
McMeier
Gast
Hallo,
ich hätte mal eine frage und zwar warum das Bild bei meinem Code flackert. Ich dachte Swing wäre Standardmäßig Doublebuffered. Ich habe sogar zusätzlich noch im Constructor meiner Klasse setDoubleBuffered(true); geschrieben. Hier mal der Code.
Ach und mal noch ne Info, mit update.... hat es bisher nicht geflackert erst seit dem ich das mit dem Antialiasing habe. das musste aber sein weil sonst die RoundRects nicht richtig dargestellt werden...
Danke sehr
ich hätte mal eine frage und zwar warum das Bild bei meinem Code flackert. Ich dachte Swing wäre Standardmäßig Doublebuffered. Ich habe sogar zusätzlich noch im Constructor meiner Klasse setDoubleBuffered(true); geschrieben. Hier mal der Code.
Java:
package graphics;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import logical.*;
@SuppressWarnings("serial")
public class MainGraphics extends JPanel implements Runnable{
long nTime=1;
long oTime=2;
double timeDif;
double fps = 0;
double frames;
int length;
int lx;
int ly;
boolean gameover = false;
boolean second=false;
boolean levelup;
ImageIcon iii = new ImageIcon("bin/grass.jpg");
ImageIcon iiiapl= new ImageIcon("bin/apple.png");
Image img;
Image apple;
Color cl;
Snake s = new Snake();
Target t= new Target();
Random rand = new Random();
int a=0;
int r = 0;
Thread animator;
DecimalFormat df = new DecimalFormat("#0.00");
public MainGraphics()
{
img = iii.getImage();
apple = iiiapl.getImage();
setBackground(Color.BLACK);
setDoubleBuffered(true);
this.addKeyListener(s);
}
public void addNotify() {
super.addNotify();
animator = new Thread(this);
animator.start();
}
public void paint( Graphics g )
{
super.paint(g);
Graphics2D g2d= (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(t.getTargetColor());
g2d.drawImage(apple,t.getTargetX(),t.getTargetY(),this);
g2d.setColor(Color.darkGray);
g2d.fillRect(0, 260, 300, 2);
g2d.setColor(Color.lightGray);
g2d.drawString("Score:"+s.getScore(), 5, 275);
g2d.drawString("Lifes:"+s.getLives(), 245, 275);
g2d.drawString("Level:"+s.getLevel(),170,275);
while(length>=a){
g2d.setColor(s.getSnakeColor());
g2d.fillRoundRect(s.getx(a), s.gety(a), 6, 6, 4,4);
a++;
}
if(levelup){
g2d.setColor(Color.ORANGE);
g2d.drawString("Level UP!!", lx, ly);
}
if(gameover==true){
g2d.setColor(Color.DARK_GRAY);
g2d.drawString("GAME OVER",110,130);
g2d.drawString("Press \"R\" to Restart", 90, 140);
}
}
private boolean secondOver(){
oTime=nTime;
nTime=System.nanoTime();
timeDif+=(nTime-oTime)/1000000000.0;
if(timeDif>0.01){
second=true;
timeDif=0;
}
else
second=false;
return second;
}
public void run()
{
while(true)
{
if(s.isLevelUp()){
levelup=true;
lx=120;
ly=60;
}
if(secondOver()){
ly+=1;
}
if(ly>120)
levelup=false;
a=1;
length=s.getLength();
gameover=s.isGameOver();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.update(getGraphics());
}
}
}
Ach und mal noch ne Info, mit update.... hat es bisher nicht geflackert erst seit dem ich das mit dem Antialiasing habe. das musste aber sein weil sonst die RoundRects nicht richtig dargestellt werden...
Danke sehr