Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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  
  painting swing components on buffered image  (Read 2106 times)
0 Members and 1 Guest are viewing this topic.
Offline Kova

Senior Member





« Posted 2006-10-14 14:56:11 »

Hello.
In my game I use active rendering with 1 buffered image. I do setIgnoreRepaint(true) on JPanel and draw everything on buffered image and then display the image with one drawImage() call. So now I want to add JTextField to my game so players can chat in-game. How do I do that?
I've tried to bypass drawing on buffered image and I added it to JPanel and then called this.paintComponents(g) but I get JTextField with no width at all (only borders visible) and on upper middle of the screen, not where I set bounds to, plus it's flickering. So the right thing to do is to draw it on buffered image I guess, how do I do that? Thank you.
Offline Kova

Senior Member





« Reply #1 - Posted 2006-10-14 22:34:59 »

I've made some progress, here's what I wrote in JavaDesktop forums:

http://forums.java.net/jive/thread.jspa?threadID=19129

Hello.
I have a game and I would like to add chat option for players. I use active rendering with one buffered image (first I render everything to the image and then render image to the screen). So how do I add a swing component properly?
If I just use .setBounds() container.add(chat_tf) it dosn't get rendered (which is normal since I must manually render it) but it's there, you can select it and write stuff (although you don't see it). When I try to call chat_tf.paint(my_buf_img) then it gets rendered but it's always at 0,0,width,height. If I change width/height it changes accordingly, but it is always drawn starting at (0,0).
Anybody knows how to solve this or any better way to do it? Thank you.
Offline Kova

Senior Member





« Reply #2 - Posted 2006-10-16 22:26:53 »

so as it seems this isn't so noobish of me afterall... the solution is to subclass JTextField, override paint() method so JTextField is painted on BufferedImage first, and then when need for active rendering it uses that image for display ... it's all in the sample code below, thanks to leouser from JavaDesktop forums for making most of it:


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  
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class ActiveSwingRenderByLeouser extends JPanel implements Runnable {
   private volatile boolean running = true;
    private BufferedJTextField chat_tf = new BufferedJTextField();
    private Image dbImage = null;
    private Graphics dbg;

    public static void main(String[] args) {
        Runnable run = new Runnable() {
           public void run() {
              new ActiveSwingRenderByLeouser();
            }
       };
        SwingUtilities.invokeLater(run);
    }

    public ActiveSwingRenderByLeouser() {
          JFrame vframe = new JFrame("Active Swing Rendering to buffer image test");
          setIgnoreRepaint(true);
          setOpaque(true);
          this.setLayout(null);

          vframe.setContentPane(this);
          vframe.setBounds(0,0,320,240);
          vframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          vframe.setVisible(true);

          chat_tf.setBounds(50,150,200,20);
          this.add(chat_tf); // comment this to see that paint() is just painting from 0,0 on Graphics (image)

         Thread animator = new Thread(this);
         animator.start();
    }

    public void run() {
        while (running) {
           render();
           try {
               Thread.sleep(50);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
        }
    }

    public void render() {
        if (dbImage == null) {
            dbImage = createImage(320, 240);
            if (dbImage == null) {
                return;
            }
           dbg = dbImage.getGraphics();
        }
        dbg.setColor(Color.GRAY);
        dbg.fillRect(0, 0, 320, 240);
        dbg.setColor(Color.BLUE);
        dbg.drawRect(50, 100, 50, 50);
        dbg.drawRect(75, 75, 50, 50);
        dbg.drawRect(100, 50, 50, 50);
        dbg.drawOval(150, 75, 100, 75);

        synchronized(chat_tf){
           Image image = chat_tf.image;
            if(image != null){
                dbg.drawImage(image, chat_tf.getX(), chat_tf.getY(), null);
            }
        }

       Graphics g;
       try {
          g = this.getGraphics();
         if ((g != null) && (dbImage != null))
             g.drawImage(dbImage, 0, 0, null);
         Toolkit.getDefaultToolkit().sync();
         g.dispose();
       }
       catch (Exception e) {
         System.out.println("Graphics error: " + e);
      }
    }

    static class BufferedJTextField extends JTextField {
       BufferedImage image;
        int biwidth;
        int biheight;

        public void paint(Graphics g) {
            synchronized (this) {
                int width = getWidth();
                int height = getHeight();
                if (width <= 0 || height <= 0) return;
                if (image == null || width != biwidth || height != biheight) {                    
                   image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                    biwidth = width;
                    biheight = height;
                }
                Graphics g2 = image.createGraphics();
                try{
                    super.paint(g2);
                } finally {
                   g2.dispose();
                }
                g.drawImage(image, 0, 0, null);
            }
        }

    }

}


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

Junior Member





« Reply #3 - Posted 2006-10-18 01:58:36 »

First you must understand about layering... all objects are "painted" on the JPanel/ JFrame via layers...

because you are using the paint() or paintComponents() or drawImage(), the layering for the image is very high hence the components at a lower layering level will be "painted" over.

What you can do is instead of painting over the panel.. just set the image to be the bg of the panel and painting your components over it... ^^

You can use AWT for more complex and fleixble configurations or use Swing for more complex and flexible user interfaces ^^
Offline Kova

Senior Member





« Reply #4 - Posted 2006-10-18 13:46:00 »

First you must understand about layering... all objects are "painted" on the JPanel/ JFrame via layers...
because you are using the paint() or paintComponents() or drawImage(), the layering for the image is very high hence the components at a lower layering level will be "painted" over.
What you can do is instead of painting over the panel.. just set the image to be the bg of the panel and painting your components over it... ^^
You can use AWT for more complex and fleixble configurations or use Swing for more complex and flexible user interfaces ^^

No, first you must understand what this topic is about. Read carefully, you totally didn't understand any single point of it... but thank you for at least paying attention to it.
btw. I know how containers are painted
Offline Kova

Senior Member





« Reply #5 - Posted 2006-10-19 13:52:55 »

also I've found different kind of solution here on forums, didn't read it yet, but CommanderKeith said it worked:

http://www.java-gaming.org/forums/index.php?topic=13211.0
Offline ENC

Junior Member





« Reply #6 - Posted 2006-10-20 02:04:55 »

lolz... this what noobs are for... to post things which look stupid and learn from them.. lolz  Grin
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.

atombrot (27 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!