Animasyon oluşturmak için bir dizi görüntüyü görüntüleyen bir sınıf hazırladım. Animasyonun, programın geri kalanından ayrı bir iş parçacığı üzerinde yer almasını istiyorum. Ancak, animasyon sınıfını başlatmaya çalıştığımda bir hata alıyorum.Yeni Konu JFrame ile çalışmadı
Bu animasyon sınıfının bazıları (daha fazlası var ama alakasız olan): Ben de o zaman tüm nesneyi yeni bir yapım, animasyon sınıfı Runnable hale getirmeyi çalıştılar
/**
* Starts a new thread to play the animation on
*/
public void start()
{
playing = true;
animateThread = (new Thread(() -> run()));
animateThread.start();
}
/**
* Will go through sprites and display the images
*/
public void run()
{
int index = 0;
while (playing)
{
if (index > sprites.length)
{
index = 0;
}
try
{
g.drawImage(sprites[index].getImage(), x, y, null);
animateThread.sleep(speed);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
index++;
}
}
Konu ama aynı hatayı aldım.
Bu
JFrame tutan ve animasyon başlar (daha vardır fakat ilgisiz olduğu) sınıfı geçerli: gerçekten değil Yanipublic static void main(String[] args)
{
AnimationTester tester = new AnimationTester();
tester.frame.setResizable(false);
tester.frame.setTitle("Tester");
tester.frame.add(tester);
tester.frame.pack();
tester.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tester.frame.setLocationRelativeTo(null);
tester.frame.setVisible(true);
//Make the window not have to be clicked on to get input (Set is as the main focus when it begins)
tester.requestFocusInWindow();
//Start the program
tester.start();
}
public void start()
{
createGraphics();
animation.start();
}
public void createGraphics()
{
BufferStrategy bs = getBufferStrategy();
//Checks to see if the BufferStrategy has already been created, it only needs to be created once
if (bs == null)
{
//Always do triple buffering (put 3 in the param)
createBufferStrategy(3);
return;
}
//Links the bufferStrategy and graphics, creating a graphics context
g = bs.getDrawGraphics();
try
{
animation = new Animation(ImageIO.read(getClass().getResource("/SpriteSheet.jpg")), 16, 2, 200, 250, 250, 2.0);
animation.addGraphics(g);
}
catch (IOException e)
{
e.printStackTrace();
}
}
için
çalışmak gerekendir konusunda daha fazla ayrıntı için
BufferStrategy
ve BufferStrategy and BufferCapabilities de yakından bakın Durumu güncellemek ve ekrana dönüştürmek için – MadProgrammerAldığınız hata nedir? Sidenote: "if (index> sprites.length)" dosyasında bir hata var. Bu, endeksin "sprite [indeks]" deki sprite erişmek için kullanıldığında sprites.length'e eşit olmasına izin verir. ArrayIndexOutOfBoundsException. "if (index == sprites.length)" olarak değiştirilmelidir. – mmaarouf