Porblema al pintar en JFrame
Publicado por Kirk Taison (2 intervenciones) el 18/08/2011 21:27:55
Hola a tod@s, soy nuevo por aquí y más o menos nuevo con el lenguaje java. He buscado algún hilo similar a mi problema pero no encuentro nada, pido disculpas y cierre del hilo en caso de que haya uno.
Os planteo el problema (quizás algo básico). Estoy trasteando un poco con el código del clásico juego del PingPong, donde se crea una primera ventana "Lanzamientos" donde corre el juego. Tengo modificado el juego, para que se pare en algunos movimientos, se salga del bucle de juego y continue en la llamada al drawResult(). En este último método, quiero que se abra una ventana similar a la anterior, y pintar a mi gusto (figuras como rectángulos, círculos, líneas...o pintar el fondo, etc..)
Ahí viene el problema...me crea una ventana nueva llamada "MP", pero no hace caso de lo que le digo...no me pinta el fondo de negro como tengo puesto, o el rectángulo, nada...me crea la ventana con su título pero con el fondo gris por defecto. A veces (en algunas pruebas) me salta el temido java.lang.NullPointerException en la línea indicada del drawResult().
Aquí os dejo el código, lo he simplificado quitando lo que considero redundante.
Un saludo y muchas gracias de antemano.
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;
import javax.swing.JFrame;
public class Direcciones extends JFrame {
private int windowWidth = 800;
private int windowHeight = 600;
public static void main(String[] args) {
new Direcciones();
}
public Direcciones() {
super ("Lanzamiento");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(windowWidth, windowHeight);
this.setResizable(false);
this.setLocation(600, 100);
this.setVisible(true);
this.createBufferStrategy(2);
.
.
.//Se inicia el juego, donde se va llamando al drawFrame() en un bucle
drawResult();
}
private void drawFrame() {
// code for the drawing goes here
BufferStrategy bf = this.getBufferStrategy();
Graphics g = null;
try {
g = bf.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, windowWidth, windowHeight);
drawBall(g); //Va pintando la bola
drawPaddle(g); //y la barra
} finally {
g.dispose();
}
// Shows the contents of the backbuffer on the screen.
bf.show();
//Tell the System to do the Drawing now, otherwise it can take a few extra ms until
//Drawing is done which looks very jerky
Toolkit.getDefaultToolkit().sync();
}
private void drawResult(){
JFrame Result = new JFrame();
Result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Result.setSize(windowWidth, windowHeight);
Result.setTitle("MP");
Result.setLocation(200, 100);
Result.setResizable(true);
Result.setVisible(true);
Result.createBufferStrategy(2);
BufferStrategy bf = Result.getBufferStrategy();
Graphics g = null;
try {
g = bf.getDrawGraphics(); //Aquí me marca el NullPointerException
g.setColor(Color.BLACK);
g.fillRect(0, 0, windowWidth, windowHeight);
g.drawRect( 50,50,45,60 );
} finally {
// It is best to dispose() a Graphics object when done with it.
g.dispose();
}
bf.show();
Toolkit.getDefaultToolkit().sync();
}
private void drawBall(Graphics g) {
}
private void drawPaddle(Graphics g) {
//
}
Os planteo el problema (quizás algo básico). Estoy trasteando un poco con el código del clásico juego del PingPong, donde se crea una primera ventana "Lanzamientos" donde corre el juego. Tengo modificado el juego, para que se pare en algunos movimientos, se salga del bucle de juego y continue en la llamada al drawResult(). En este último método, quiero que se abra una ventana similar a la anterior, y pintar a mi gusto (figuras como rectángulos, círculos, líneas...o pintar el fondo, etc..)
Ahí viene el problema...me crea una ventana nueva llamada "MP", pero no hace caso de lo que le digo...no me pinta el fondo de negro como tengo puesto, o el rectángulo, nada...me crea la ventana con su título pero con el fondo gris por defecto. A veces (en algunas pruebas) me salta el temido java.lang.NullPointerException en la línea indicada del drawResult().
Aquí os dejo el código, lo he simplificado quitando lo que considero redundante.
Un saludo y muchas gracias de antemano.
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;
import javax.swing.JFrame;
public class Direcciones extends JFrame {
private int windowWidth = 800;
private int windowHeight = 600;
public static void main(String[] args) {
new Direcciones();
}
public Direcciones() {
super ("Lanzamiento");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(windowWidth, windowHeight);
this.setResizable(false);
this.setLocation(600, 100);
this.setVisible(true);
this.createBufferStrategy(2);
.
.
.//Se inicia el juego, donde se va llamando al drawFrame() en un bucle
drawResult();
}
private void drawFrame() {
// code for the drawing goes here
BufferStrategy bf = this.getBufferStrategy();
Graphics g = null;
try {
g = bf.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, windowWidth, windowHeight);
drawBall(g); //Va pintando la bola
drawPaddle(g); //y la barra
} finally {
g.dispose();
}
// Shows the contents of the backbuffer on the screen.
bf.show();
//Tell the System to do the Drawing now, otherwise it can take a few extra ms until
//Drawing is done which looks very jerky
Toolkit.getDefaultToolkit().sync();
}
private void drawResult(){
JFrame Result = new JFrame();
Result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Result.setSize(windowWidth, windowHeight);
Result.setTitle("MP");
Result.setLocation(200, 100);
Result.setResizable(true);
Result.setVisible(true);
Result.createBufferStrategy(2);
BufferStrategy bf = Result.getBufferStrategy();
Graphics g = null;
try {
g = bf.getDrawGraphics(); //Aquí me marca el NullPointerException
g.setColor(Color.BLACK);
g.fillRect(0, 0, windowWidth, windowHeight);
g.drawRect( 50,50,45,60 );
} finally {
// It is best to dispose() a Graphics object when done with it.
g.dispose();
}
bf.show();
Toolkit.getDefaultToolkit().sync();
}
private void drawBall(Graphics g) {
}
private void drawPaddle(Graphics g) {
//
}
Valora esta pregunta


0