Hello,
today I wrote a short programm, to play around with canvas and a little with the MouseListener.
For the Graphic part I used ra4kings given code or rather the one from http://ra4king.is-a-geek.net/javadocs/java/awt/image/BufferStrategy.html.
My Programm works fine, as long as I have a while(true) loop to refresh my frame contents. Buf if I remove the loop and add render() (rander is the method that redraws my canvas, it’s also the one with the while(true) loop from before) tags, in exchange, to the methods that modify my graphicoutput, then I only see my “image” for a split second and then I only see gray until I do something that activates the render method again.
Hope you can help me with the short Explanation I gave you, if it’s not enough tell me and I’ll add the code
biro
Edit:
It’s me again. I got my Programm so far, that it shows me the correct screen after I drag my mouse. But before I do that (in between the time after my startup and before the first mouseDragged() method starts) I only see a gray screen, except for a short “blinking” in which I believe to see the correct screen.
Here the important parts of the code:
public synchronized void render() {
// Prepare for rendering the next frame
// ...
// Render single frame
BufferedImage g = null; //Green Image for a green background
BufferedImage s = null; //Black Image for a black background
try {
g = ImageIO.read(new File("green.png"));
s = ImageIO.read(new File("schwarz.png"));
} catch (IOException ie) {
ie.printStackTrace();
System.exit(0);
}
do {
// The following loop ensures that the contents of the drawing buffer
// are consistent in case the underlying surface was recreated
do {
// Get a new graphics context every time through the loop
// to make sure the strategy is validated
Graphics graphics = strategy.getDrawGraphics();
// Render to graphics
// ...
for (int x = 0; x < xMax(); x++) {
for (int y = 0; y < yMax(); y++) {
BufferedImage draw = null;
int drawx = this.activetilex+x;
int drawy = this.activetiley+y;
if (drawx >= this.tilesx || drawy >= this.tilesy || drawx < 0 || drawy < 0) { //Außerhalb des Arrays
draw = s;
} else {
switch (feld[drawx][drawy].getTyp()) {
case 1: //Grün
draw = g;
break;
default:
draw = s;
}
}
if (x == 0 && y == 0) {
BufferedImage newdraw = draw.getSubimage(this.activex, this.activey, this.tilewidth-this.activex, this.tileheigth-this.activey);
graphics.drawImage(newdraw, 0, 0, null);
} else if (x == 0) {
BufferedImage newdraw = draw.getSubimage(this.activex, 0, this.tilewidth-this.activex, this.tileheigth);
graphics.drawImage(newdraw, 0, y*this.tileheigth-this.activey, null);
} else if (y == 0) {
BufferedImage newdraw = draw.getSubimage(0, this.activey, this.tilewidth, this.tileheigth-this.activey);
graphics.drawImage(newdraw, x*this.tilewidth-this.activex, 0, null);
} else {
graphics.drawImage(draw, x*this.tilewidth-this.activex, y*this.tileheigth-this.activey, null);
}
}
}
// Dispose the graphics
graphics.dispose();
// Repeat the rendering if the drawing buffer contents
// were restored
} while (strategy.contentsRestored());
// Display the buffer
strategy.show();
// Repeat the rendering if the drawing buffer was lost
} while (strategy.contentsLost());
}
This is my render method.
private Canvas board;
private BufferStrategy strategy;
private MapMover mouselistener;
private GuiListener listener;
private int tilewidth = 50; //Breite eines Felds in Pixeln / Width of one field in pixels
private int tileheigth = 50; //Höhe eines Felds in Pixeln / Heigth of one field in pixels
private int showwidth = 800; //Fensterbreite /Windowwidth
private int showheigth = 600;//Fensterhöhe /Windowheigth
private int activetilex = 0; //Welches Tile sehen wir gerade /Which tile on the x axis do we see
private int activetiley = 0; //dito /Same as above for the y axis
private int activex = 0; //Verschiebung des Tiles noch links / adjustment of the tile to the left
private int activey = 0; //Verschiebung des tiles nach oben /adjustment of the tile to top
private int tilesx = 25; //Anzahl an Tiles auf der x-Achse insgesammt /Sum of tiles on the x-axis
private int tilesy = 15; //Anzahl an Tiles auf der y-Achse insgesammt /Sum of tiles on the y-axis
Here the class attributes, these also have getter and setter methods!
public Gui() {
super();
initField();
listener = new GuiListener(this);
initGUI();
board.createBufferStrategy(2);
this.strategy = board.getBufferStrategy();
mouselistener = new MapMover(this);
board.addMouseListener(mouselistener);
board.addMouseMotionListener(mouselistener);
this.setVisible(true);
render();
}
And here my startup routine
Like I said the render method link in the startup method fabricates a short correct screenblink and than the screen get’s gray agains.
Because the MouseListener and MouseMotionListener is doing it’s job correct I won’t post it now, if you ant it pls tell me.
Many thanks for your help
biro