Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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]
1  Java Game APIs & Engines / Java 2D / Re: drawing with BufferStrategy in windowed mode on: 2006-10-14 21:49:44
Thank you all for your answers!

I tried CommanderKeith's recomendation and worked just fine.

Regards,
Gabriel
2  Java Game APIs & Engines / Java 2D / drawing with BufferStrategy in windowed mode on: 2006-10-12 00:48:39
Hi,

I'm developing a 2D game and I'm creating a JFrame with a JPanel inside.

For drawing, I create a double buffer in the JFrame. The game actually is a subclass of JPanel, so I add that JPanel into the JFrame's content pane.

The problem I have is that for drawing the JPanel, I get a Graphics object from the BufferStrategy, but this Graphics object is aligned to the JFrame, not the internal JPanel, so the first row of 2D map tiles are drawed BEHIND the window's title bar, I guess because the (0,0) coordinate is relative to the JFrame, not the JPanel.

I could guess too that in full-screen mode this would be the right behavior, but I want my game to be windowed.

So the question is how I can draw my first row of tiles from the (0,0) of the JPanel so that they are not behind the window's title bar?

TIA,
Gabriel
3  Game Development / Newbie & Debugging Questions / Re: Can a Swing GUI be used in a direct-rendering game? on: 2006-04-20 22:05:52
Hi,

I was reading your postings since I wanted to use Swing as data input framework for my game so that I don't need to develop one of my own.

Here I send you a little test program which open a dialog for text input and it seems to work OK. Don't know if it represent the kind of problems you have with your rendering code.

I followed the active rendering examples from the book Killer Game Programming it seems to be deadlock free (can't assure it though).

The problem I found with this is that it seems to me that the code is somewhat slow and the cube doesn't get rendered solid but I shows some flickering. So I post the code to see if anyone has a better idea of why those things are happening.

Regards,
Gabriel

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  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.sun.j3d.utils.timer.*;

public class ActiveSwingTest implements Runnable {

  static final int VEL = 1;

  // Number of frames with a delay of 0 ms before the animation thread yields
  // to other running threads.
  private static final int NO_DELAYS_PER_YIELD = 16;

  // no. of frames that can be skipped in any one animation loop
  // i.e the games state is updated but not rendered
  private static int MAX_FRAME_SKIPS = 5; // was 2;

  JFrame f;
  JPanel panel;
  Image backBuffer;

  JDialog dialog;

  private long gameStartTime;
  private long prevStatsTime;
  private boolean running;
  private Graphics2D graphics;
  private long period;
  private long framesSkipped = 0;

  int x = 0;
  int y = 0;
  int vx = VEL;
  int vy = VEL;

  public ActiveSwingTest() {
    initGraphics();
  }

  public void initGraphics() {
    panel = new JPanel();
    panel.setPreferredSize(new Dimension(800, 600));
    panel.setFocusable(true);
    panel.requestFocus();
    panel.setIgnoreRepaint(true);
    readyForTermination();

    f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(panel);
    f.setIgnoreRepaint(true);

    f.setResizable(false);
    f.pack();

    backBuffer = createBackBuffer();
    if(backBuffer == null) {
      return;
    }

    f.setVisible(true);
  }

  public void run() {
    long timeDiff = 0;
    long overSleepTime = 0L;
    int noDelays = 0;
    long excess = 0L;

    gameStartTime = J3DTimer.getValue();
    prevStatsTime = gameStartTime;
    long beforeTime = gameStartTime;

    running = true;

    graphics = (Graphics2D) backBuffer.getGraphics();

    while(running) {
      update(timeDiff);
      render(graphics);

      paintScreen();

      long afterTime = J3DTimer.getValue();
      timeDiff = afterTime - beforeTime;
      long sleepTime = (period - timeDiff) - overSleepTime;

      if(sleepTime > 0) { // some time left in this cycle
        try {
          Thread.sleep(sleepTime / 1000000L); // nano -> ms
        }
        catch(InterruptedException ex) {}
        overSleepTime = (J3DTimer.getValue() - afterTime) - sleepTime;
      }
      else { // sleepTime <= 0; the frame took longer than the period
        excess -= sleepTime; // store excess time value
        overSleepTime = 0L;

        if(++noDelays >= NO_DELAYS_PER_YIELD) {
          Thread.yield(); // give another thread a chance to run
          noDelays = 0;
        }
      }

      beforeTime = J3DTimer.getValue();

      /* If frame animation is taking too long, update the game state
         without rendering it, to get the updates/sec nearer to
         the required FPS. */

      int skips = 0;
      while((excess > period) && (skips < MAX_FRAME_SKIPS)) {
        excess -= period;
        update(timeDiff); // update state but don't render
        skips++;
      }
      framesSkipped += skips;
    }

    System.exit(0); // so window disappears
  }

  private void showDialogo() {
    if ( dialog == null ) {
      dialog = new JDialog(f, "Example dialog", true);
      final JTextField t = new JTextField("hello");
      JButton bok = new JButton("OK");
      bok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              System.out.println("text="+t.getText());
              dialog.setVisible(false);
              dialog.dispose();
              dialog = null;
            }
          });
      JButton bcancel = new JButton("Cancel");
      bcancel.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                  dialog.setVisible(false);
                }
              });

      final Container c = dialog.getContentPane();
      c.setLayout(new BorderLayout());
      c.add(t, BorderLayout.CENTER);

      final JPanel buttonPanel = new JPanel();

      buttonPanel.add(bok);
      buttonPanel.add(bcancel);

      c.add(buttonPanel, BorderLayout.PAGE_END);

      dialog.pack();
      dialog.setLocationRelativeTo(f);

      dialog.setVisible(true);
    }
    else {
      dialog.setVisible(true);
    }
  }

  private void paintScreen() {
    // use active rendering to put the buffered image on-screen
    try {
      final Graphics g = panel.getGraphics();
      if(g != null) {
        g.drawImage(backBuffer, 0, 0, null);
      }
      g.dispose();
    }
    catch(Exception e) {
      System.out.println("Graphics context error: " + e);
    }
  }

  private Image createBackBuffer() {
    final Image dbImage = panel.createImage(800, 600);
    if(dbImage == null) {
      System.out.println("could not create the backbuffer image!");
    }
    return dbImage;
  }

  private void readyForTermination() {
    panel.addKeyListener(new KeyAdapter() {
      // listen for esc, q, end, ctrl-c on the canvas to
      // allow a convenient exit from the full screen configuration
      public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if((keyCode == KeyEvent.VK_ESCAPE) || (keyCode == KeyEvent.VK_Q) ||
           (keyCode == KeyEvent.VK_END) ||
           ((keyCode == KeyEvent.VK_C) && e.isControlDown())) {
          running = false;
        }
        else if ( keyCode == KeyEvent.VK_D ) {
          showDialogo();
        }
      }
    });
  }

  private void update(long dt) {
    x += vx;
    y += vy;

    if ( x < 0 ) {
      x = 0;
      vx = VEL;
    }
    else if ( x > 700 ) {
      x = 700;
      vx = -VEL;
    }

    if ( y < 0 ) {
      y = 0;
      vy = VEL;
    }
    else if ( y > 500 ) {
      y = 500;
      vy = -VEL;
    }
  }

  private void render(Graphics2D g) {
    g.setColor(Color.RED);
    g.fillRect(0, 0, 800, 600);
    g.setColor(Color.WHITE);
    g.fillRect(x, y, 100, 100);
  }

  public static void main(String[] args) {
    ActiveSwingTest test = new ActiveSwingTest();
    new Thread(test).start();
  }

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

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

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

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

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

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

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

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

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

theagentd (1373 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!