Estou começando a desenvolver um monor para jogos simples em Java, e a intenção é aplicá-lo mais tarde para celulares, com as devidas adaptações.
Estou cfazendo uns testes em 2D e gostaria de saber como eu poderia redizir as piscadas da tela durante o método repaint().
Segue o código teste que estou usando:
import java.awt.*;
import java.applet.Applet;
import java.awt.image.*;
import javax.swing.*;
import java.awt.geom.AffineTransform;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
public class Map_Line extends Frame implements ActionListener {
public static Map_Line Map;
public static JPanel status;
private BufferedImage bi, bi2;
public int x=150, y=150;
public static pega_botao botao;
public static fisica Fis;
public Graphics g = null;
public static Container container;
private static Timer timer = new Timer();
public ScheduleRunner scheduleRunner;
public void actionPerformed(ActionEvent e) {
}
public Map_Line() {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
Frame f = new Frame("Exibir Imagens");
addWindowListener(l);
pega_botao botao = new pega_botao();
addKeyListener(botao);
f.pack();
setTitle("teste");
setBackground(Color.black);
Image img = getToolkit().getImage("kuruma.gif");
Image img2 = getToolkit().getImage("kuruma.gif");
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.addImage(img2, 100);
tracker.waitForID(0);
} catch (Exception e) {}
int iw = img.getWidth(this);
int ih = img.getHeight(this);
bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
bi2 = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.drawImage(img,0,0,this);
big = bi2.createGraphics();
big.drawImage(img2,100,100,this);
setSize(new Dimension(350, 250));
setResizable(false);
setVisible(true);
Fis = new fisica();
doLater();
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
int bw = bi.getWidth(this);
int bh = bi.getHeight(this);
g2.drawImage(bi, null, x, y);
g2.drawImage(bi, null, 100, 100);
}
public void doLater(){
scheduleRunner = new ScheduleRunner();
timer.schedule( scheduleRunner, 30 );
}
public static void main(String args[]) {
Map = new Map_Line();
}
class ScheduleRunner extends TimerTask{
public void run(){
if (Fis.esta_pulando == true || Fis.esta_caindo == true){
Fis.pulo();
}
Map.repaint();
Map.doLater();
}
}
public class pega_botao implements KeyListener {
public int desenhando = 1;
public int sair = 0;
public String Sout;
public Graphics g;
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_LEFT){
Map.x -= 5;
Map.repaint();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
Map.x += 5;
Map.repaint();
}
if (e.getKeyCode() == KeyEvent.VK_UP){
if (Fis.esta_chao == true){
Fis.init_pulo();
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN){
Map.y += 5;
Map.repaint();
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
}
}
class fisica {
public boolean esta_pulando = false;
public boolean esta_chao = true;
public boolean esta_topo = false;
public boolean esta_colidindo = false;
public boolean esta_caindo = false;
public int hero_x, hero_y, inimigo_x, inimigo_y;
public int velocidade = 10;
public int velocidade_inicial = 10;
public int y_inicial;
public int obstaculo = 0;
public void init_pulo(){
y_inicial= Map.y;
esta_pulando = true;
}
public void pulo(){
/* função relacionada ao pulo. Ela e a função de colisão serão executadas durante o
Timer de todo o jogo, no Loop principal*/
if (velocidade == 0) {
esta_topo = true;
}
if (esta_chao == true && esta_pulando == true && esta_topo == false && esta_caindo == false ){
esta_chao = false;
Map.y -= velocidade;
velocidade --;
}
if (esta_chao == false && esta_pulando == true && esta_topo == false && esta_caindo == false){
Map.y -= velocidade;
velocidade --;
}
if (esta_topo == true){
esta_caindo = true;
esta_topo = false;
}
if(esta_caindo == true && esta_chao == false ) {
Map.y +=velocidade;
velocidade ++;
if (velocidade == velocidade_inicial){
esta_chao = true;
esta_pulando = false;
}
}
if (esta_chao == true && esta_pulando == false && obstaculo == 0){
if (velocidade != velocidade_inicial){
velocidade++;
Map.y += velocidade;
velocidade = velocidade_inicial;
}
}
if(esta_chao == true && esta_pulando == false){
esta_caindo = false;
}
if (esta_pulando == false){
if(Map.y != y_inicial){
Map.y = y_inicial;
}
}
Map.repaint();
}
public void colisao(){
this.hero_y = Map.y;
this.hero_x = Map.x;
}
}
}
Caso não tenha ficado claro, o que eu quero saber é como reduzir as piscadas durante o repaint(), nesse código teste.
Obrigado.