Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JPanel example?  (Read 2211 times)
0 Members and 1 Guest are viewing this topic.
Offline DrQuincy

Junior Member




Vwls hv bn bnnd!


« Posted 2005-08-19 16:00:29 »

Hi

I'd like to do some simple animated graphics on a JPanel.  Does anyone know of an online example that includes (normal application, not an applet) some double-buffering and some threading?  All the examples I've found have been applets.

Thanks.
Offline weston

Junior Member





« Reply #1 - Posted 2005-08-19 21:06:41 »

here is a fairly simple example of a red sqaure bouncing around on a black background. Double-buffering is done automatically btw. If you want smoother animation, you will need to make sure that the main loop takes as close to the same amount of time to run each time as possible. Other technologies used to get better performance or quality with java2d are the BufferStrategy class, managed images, and the fullscreen exclusive mode. There is tons written about these technologies already, so just check out google first. Heres 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  
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  
import javax.swing.*;
import java.awt.*;

public class MyClass implements Runnable
{
   private JFrame mainFrame;
   private JPanel drawingPanel;  
   private boolean running;

   private Rectangle rect;
   private int xDirection;
   private int yDirection;
   private int xSpeed;
   private int ySpeed;
   
   public MyClass()
   {
      rect = new Rectangle(50, 50, 50, 50);

      mainFrame = new JFrame("Test");
      drawingPanel = new DrawingPanel();
      drawingPanel.setPreferredSize(new Dimension(640, 480));
      mainFrame.setContentPane(drawingPanel);
      mainFrame.pack();
      mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainFrame.setVisible(true);
     
      xDirection = 1;
      yDirection = 1;
      xSpeed = 2;
      ySpeed = 2;
      Thread mainLoop = new Thread(this);
      mainLoop.start();
   }

   public void run()
   {
      running = true;
      while(running)
      {
         updatePositions();
         drawingPanel.repaint();
         try
         {
            Thread.sleep(15);
         } catch(InterruptedException ex) {
            ex.printStackTrace();
         }
      }
   }

   private void updatePositions()
   {
      rect.x += xDirection*xSpeed;
      rect.y += yDirection*ySpeed;
     
      if(rect.x < 0)
      {
         rect.x = 0;
         xDirection = 1;
      }
      else if(rect.x+rect.width > drawingPanel.getWidth())
      {
         rect.x = drawingPanel.getWidth()-rect.width;
         xDirection = -1;
      }

      if(rect.y < 0)
      {
         rect.y = 0;
         yDirection = 1;
      }
      else if(rect.y+rect.height > drawingPanel.getHeight())
      {
         rect.y = drawingPanel.getHeight()-rect.height;
         yDirection = -1;
      }
   }
   
   public static void main(String[] args)
   {
      new MyClass();
   }

   class DrawingPanel extends JPanel
   {
      public void paintComponent(Graphics g)
      {
         super.paintComponent(g);
         Graphics2D g2 = (Graphics2D)g;
         
         //draw black background
        g2.setColor(Color.BLACK);
         g2.fillRect(0, 0, drawingPanel.getWidth(),  drawingPanel.getHeight());
         
         //draw red rectangle
        g2.setColor(Color.RED);
         g2.fillRect(rect.x, rect.y, rect.width, rect.height);
      }
   }
}

for(int i = 1; i > 0; i++)
{
System.out.println(i+" cups of java downed");
}
Offline DrQuincy

Junior Member




Vwls hv bn bnnd!


« Reply #2 - Posted 2005-08-19 21:18:08 »

Thank you! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ENC

Junior Member





« Reply #3 - Posted 2006-07-10 10:15:52 »

Is it possible to do double buffering with JPanel? Cause the above example uses the drawing tools from the Java Package..

as for me I am having trouble with mine.. cause I am using a buffer image... the background(buffer image) does not flicker, but as for my character (buffer image too) will flicker everytime...

can someone explain to me?

Then another question is that ,  can we use BufferStrategy on JPanel.. cause my images does not flicker on windows but on the Panel...

Cheers!
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #4 - Posted 2006-07-11 21:06:02 »

You can use BufferStrategy on a JFrame, but not directly on a JPanel. My somewhat inelegant solution is to use java.awt.Canvas as a drawing surface, since that supports setting a BufferStrategy separate of that belonging to its ancestral frame, while the rest of the application (stuff which doesn't require constant animation) uses swing's own double buffering system.

Also, the flickering depends on how you draw it. If you go all the way through swing (using only repaint(), for example, though that is less-than-optimal for one who wants high framerates) then it shouldn't flicker, but we don't know what you do so it's a bit difficult.
Offline Mr_Light

Senior Member




shiny.


« Reply #5 - Posted 2006-07-12 21:20:04 »

Heavy weight components only draw themselfs lightweight components get drawn by there parent

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (14 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!