Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
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  
  A JCanvas  (Read 1400 times)
0 Members and 1 Guest are viewing this topic.
Offline Alexian

Senior Newbie





« Posted 2006-10-19 14:18:57 »

Hi all.
A simple question for a difficult answer (maybe). What is the best way to create a JCanvas? I mean in java.awt package there is the Canvas object but it is a Heavyweight object and it isn't a good idea to mix Lightweight objects with HW objs (do not respect the z-order). How can I do? I tried to create a new one which extends JComponent and implements some methods for the creation of  a BufferStrategy, but I got some problems to show images (they blink).

I show you what I've done:
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  
public class JCanvas extends JComponent 
   [INNER CLASSES]

   private static final long serialVersionUID = 123456789L;
   private static final Dimension MINIMUM_SIZE = new Dimension(320, 200);
   
   transient ComponentPeer peer;
   private static VolatileImage vImage;
   private BufferStrategy strategy = null;
   
   public JCanvas() {
      super();
   }
     
   public void initStrategy() {
      if (this.getMinimumSize().height >= MINIMUM_SIZE.height && this.getMinimumSize().width >= MINIMUM_SIZE.width) {
         this.createBufferStrategy(2);
         this.strategy = this.getBufferStrategy();
      }
   }
   
    public BufferStrategy getBufferStrategy() {
        if (this.strategy == null) {
            createBufferStrategy(1);
        }
        return this.strategy;
    }
     
   public Graphics getBufferedGraphics() {
      return this.strategy != null ? this.strategy.getDrawGraphics() : this.getGraphics();
   }
   
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
   }
   
   void createBufferStrategy(int numBuffers) {
      BufferCapabilities bufferCaps;
      if (numBuffers > 1) {
         // Try to create a page-flipping strategy
        bufferCaps = new BufferCapabilities(
                     new   ImageCapabilities(true),
                     new ImageCapabilities(true),
                     BufferCapabilities.FlipContents.UNDEFINED);
         try {
            createBufferStrategy(numBuffers, bufferCaps);
            return;   // Success
        } catch   (AWTException e) {
            // Failed
        }
      }
      // Try a blitting (but still accelerated) strategy
     bufferCaps = new BufferCapabilities(
                  new   ImageCapabilities(true),
                  new ImageCapabilities(true), null);
      try {
         createBufferStrategy(numBuffers, bufferCaps);
         return;   // Success
     } catch   (AWTException e) {
         // Failed
     }
      // Try an unaccelerated   blitting strategy
     bufferCaps = new BufferCapabilities(
                  new   ImageCapabilities(false),
                  new ImageCapabilities(false), null);
      try {
         createBufferStrategy(numBuffers, bufferCaps);
         return;   // Success
     } catch   (AWTException e) {
         // Failed
     }
      // Code   should never reach here   (an   unaccelerated blitting
     // strategy   should always work)
     throw new InternalError("Could not create a   buffer strategy");
   }
   
   public void createBufferStrategy(int numBuffers, BufferCapabilities caps)
   throws AWTException
   {
      // Check arguments
     if (numBuffers < 1) {
         throw new IllegalArgumentException("Number of buffers must be at least 1");
      }
      if (caps ==   null) {
         throw new IllegalArgumentException("No capabilities   specified");
      }
      // Destroy old buffers
     if (this.strategy instanceof FlipBufferStrategy) {
         ((FlipBufferStrategy) this.strategy).destroyBuffers();
      }
      if (numBuffers == 1) {
         this.strategy = new SingleBufferStrategy(caps);
      } else {
         // assert numBuffers > 1;
        if (caps.isPageFlipping()) {
            this.strategy = new FlipBufferStrategy(numBuffers,   caps);
         } else {
            this.strategy = new BltBufferStrategy(numBuffers, caps);
         }
      }
   }
   
   protected void destroyBuffers() {
      if (peer !=   null) {
         peer.destroyBuffers();
      } else {
         throw new IllegalStateException("Component must   have a valid peer");
      }
   }
}
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #1 - Posted 2006-10-19 16:21:57 »

Use JPanel.

Offline Alexian

Senior Newbie





« Reply #2 - Posted 2006-10-19 16:32:40 »

Uuh... well I tried to use JPanel and it does not work with me, but it doesn't matter, I found the bug in my class... Grin How can I check if a component has a peer or not? I mean when I create for example a JPanel obj and I add it in the pane of a JFrame obj, there is a way to check the ComponentPeer of JPanel?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Member




shiny.


« Reply #3 - Posted 2006-10-19 17:04:38 »

I though the only Swing classes with peers where JFrame JDialog and euhm...

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.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (216 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!