Reputation: 41
I have the following code:
package de.luiskun.handling;
import de.luiskun.UtilityTool;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class PixelArtCreator extends JPanel implements Runnable{
BufferedImage imgBefore;
Thread gameThread;
public static final int width = 900, height = 500;
int fps = 60;
public PixelArtCreator() {
this.setPreferredSize(new Dimension(width, height));
this.setDoubleBuffered(true);
this.setLayout(null);
this.setDoubleBuffered(true);
this.setFocusable(true);
imgBefore = UtilityTool.SetupImage("res/preparation/00001", 512, 512);
}
@Override
public void paintComponent(Graphics g) {
System.out.println(1);
Graphics2D g2 = (Graphics2D) g;
if(imgBefore != null) {
g2.drawImage(imgBefore, 10, 10, 512, 512, null);
}
}
public void startGameThread() {
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void run() {
double drawInterval = 1000000000 / fps; // 0.0166s
double delta = 0;
double lastTime = System.nanoTime();
double currentTime;
while (gameThread != null) {
currentTime = System.nanoTime();
delta += (currentTime - lastTime) / drawInterval;
lastTime = currentTime;
if (delta >= 1) {
System.out.println(2);
//update();
this.repaint();
delta--;
}
}
}
}
The main class:
package de.luiskun;
import de.luiskun.handling.PixelArtCreator;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(PixelArtCreator.width+14, PixelArtCreator.height+38);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setResizable(false);
PixelArtCreator s = new PixelArtCreator();
frame.add(s);
frame.setVisible(true);
s.setUpTimer();
}
}
In the main method I create a JFrame and thenn add the PixelArtCreator. Then i call startGameThread.
The console only prints out '2', but not '1'. I dont know why repaint doesnt call paintcomponent.
I used this code in many projects, but now it strangely doesnt work anymore.
Upvotes: -2
Views: 71
Reputation: 41
I changed some things in the Main method and now it works. But I didnt found the problem from before.
package de.luiskun;
import de.luiskun.handling.PixelArtCreator;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
PixelArtCreator pac = new PixelArtCreator();
window.add(pac);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
pac.startTimer();
}
}
package de.luiskun.handling;
import de.luiskun.UtilityTool;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
public class PixelArtCreator extends JPanel {
BufferedImage imgBefore;
Thread gameThread;
Timer timer;
public static final int width = 900, height = 532;
int fps = 60;
public PixelArtCreator() {
this.setPreferredSize(new Dimension(width, height));
// this.setBounds(0,0,width,height);
this.setLayout(null);
this.setDoubleBuffered(true);
this.setFocusable(true);
imgBefore = UtilityTool.SetupImage("res/preparation/00001", 512, 512);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(imgBefore != null) {
g2.drawImage(imgBefore, 10, 10, 512, 512, null);
}
}
public void startTimer() {
timer = new Timer(10,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
timer.start();
}
}
Upvotes: 1