Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: 1 [2]
31  Game Development / Newbie & Debugging Questions / Re: New to game programming on: 2011-04-26 07:10:54
Hello,
thanks for your answer.
But how does it help to implement a new Thread for the drawing? because I don't want an endless loop to redraw my graphic content, because it eats to many ressources (with the endlessloop before which is like a new thread, one of my cores was 100% used). Hope you can explain that to me, why I should implement a thread there^^

biro
32  Game Development / Newbie & Debugging Questions / Re: New to game programming on: 2011-04-25 15:19:27
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   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!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   
                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  
33  Game Development / Newbie & Debugging Questions / Re: New to game programming on: 2011-04-24 18:10:00
Hello,
I have a question to the above posted code.

}while(strategy.contentsRestored());

For what does this loop wait?

biro
Pages: 1 [2]
 
Ecumene (57 views)
2017-09-30 02:57:34

theagentd (80 views)
2017-09-26 18:23:31

cybrmynd (187 views)
2017-08-02 12:28:51

cybrmynd (185 views)
2017-08-02 12:19:43

cybrmynd (192 views)
2017-08-02 12:18:09

Sralse (203 views)
2017-07-25 17:13:48

Archive (767 views)
2017-04-27 17:45:51

buddyBro (902 views)
2017-04-05 03:38:00

CopyableCougar4 (1452 views)
2017-03-24 15:39:42

theagentd (1327 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!