Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Swing components + images based applications  (Read 2047 times)
0 Members and 1 Guest are viewing this topic.
Offline xiss burg

Junior Newbie





« Posted 2007-04-15 23:28:05 »

I'm developing a game and I'm basically using BufferedImage. I need a text field in my game so that the user can type his nickname and such...this text field must appear over  all the images on the background. How can I use JFormattedText field here?!? I tried using it with partial success because it appears and disappears(flickering) while I render. Have no idea  on how to do it. Please Help!!! Thanks  Wink   
Offline fletchergames

Senior Member





« Reply #1 - Posted 2007-04-16 17:07:52 »

It's hard to help you without knowing how you're putting the JFormattedText field on top of the images.

Are you using a JDialog?  I've had similar problems when using dialogs.  Some kind of dialog bug was fixed in the newer versions of Java, but I have no idea whether that would help.

If you're not using a JDialog but are using passive rendering, it might help to use active rendering.

Does this happen with other Components, or is the JFormattedText field the only component you're using.
Offline xiss burg

Junior Newbie





« Reply #2 - Posted 2007-04-16 20:16:24 »

Thanks for replying. The same problem occurs with any swing component. I really would like to know a correct method to do such things. I'm almost sure I'm not doing it in a 'near to correct' fashion. Because what I do  is the following: first I draw all the game images on an offscreen image(a BufferedImage)called backBuffer, then I do jframe.getGraphics().drawImage(backBuffer, 0, 0, null) to present all the contents to the screen(all this works OK). What I need is to draw the JFormattedTextField on the backBuffer before presenting the backBuffer to the screen. What I did previously was use functions like paintAll() and paintComponents() on the backBuffer. It works but no image is rendered on the background, only the swing components. I get a full gray background. Do anyone knows a good way to do it? Thanks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #3 - Posted 2007-04-16 23:20:16 »

I did a lot with Swing  Grin  However, I would need a code sample (please shorten it to the bare minimum) ...

Offline xiss burg

Junior Newbie





« Reply #4 - Posted 2007-04-17 01:35:39 »

Thanks for replying. This is a test code I did to try making that thing work(hope it is understandable)

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  
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class Main extends JFrame implements ActionListener, Runnable
{
    JFormattedTextField text;
    JButton button;
    Thread thread;
    Graphics2D g2D;
    BufferedImage backbuffer;
    BufferedImage bi;
   
    public void actionPerformed(ActionEvent e)
    {
        JOptionPane.showMessageDialog(null, text.getText());
    }
   
    public Main()
    {
        super("test");
       
        MaskFormatter mf = null;
       
        try
        {
            mf = new MaskFormatter("**********");
            mf.setValidCharacters(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-");
        }
        catch(Exception e)
        {
            System.out.println("Got exception! quaquaquaqua!!!: " + e.getMessage());
        }
       
        text = new JFormattedTextField(mf);
        text.setColumns(10);
        text.setSize(100, 30);
       
        button = new JButton("OK");
        button.setSize(100, 30);
        button.addActionListener(this);
       
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        panel.add(text);
        panel.add(button);
        panel.setOpaque(false);
        setLayout(new GridBagLayout());
        getContentPane().add(panel);
       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        setBounds(200, 200, 300, 300);
        setVisible(true);
        backbuffer = new BufferedImage(290, 290, BufferedImage.TYPE_4BYTE_ABGR);
        g2D = backbuffer.createGraphics();
       
        thread = new Thread(this);
        thread.start();
       
        try
        {
            bi = ImageIO.read(new File("Image.jpg"));
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }

   public void run()
   {
      while (true)
      {
         g2D.setColor(Color.WHITE);
         g2D.fillRect(0, 0, 290, 290);
         g2D.drawImage(bi, 0, 0, null);
         g2D.setColor(Color.BLACK);
         g2D.setFont(new Font("Rainbow6", Font.PLAIN, 50));
         g2D.drawString("Xiss Burg", 10 + (int)(30 * Math.cos(System.currentTimeMillis() * 0.001)), 100 + (int)(30 * Math.sin(System.currentTimeMillis() * 0.001)));
         paintComponents(g2D);
         getGraphics().drawImage(backbuffer, 0, 0, null);


         try
         {
            thread.sleep(30);
         }
         catch (InterruptedException e)
         {
            JOptionPane.showMessageDialog(null, "The thread has thrown an InterruptedException:\n"
                                       + e.getMessage());
         }
      }
   }
   
    public static void main(String args[])
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                Main m = new Main();
            }
        }
        );
    }
}
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #5 - Posted 2007-04-17 02:16:54 »

This worked for me.
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  
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class Test extends JFrame implements ActionListener, Runnable
{
    JFormattedTextField text;
    JButton button;
    Thread thread;
    Graphics2D g2D;
    BufferedImage backbuffer;
    BufferedImage bi;
    JPanel panel;
   
    public void actionPerformed(ActionEvent e)
    {
        JOptionPane.showMessageDialog(null, text.getText());
    }
   
    public Test()
    {
        super("test");
       
        MaskFormatter mf = null;
       
        try
        {
            mf = new MaskFormatter("**********");
            mf.setValidCharacters(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-");
        }
        catch(Exception e)
        {
            System.out.println("Got exception! quaquaquaqua!!!: " + e.getMessage());
        }
       
        text = new JFormattedTextField(mf);
        text.setColumns(10);
       
        button = new JButton("OK");
        button.setSize(100, 30);
        button.addActionListener(this);
       
        panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        panel.add(text);
        panel.add(button);
        panel.setOpaque(false);
        setContentPane(panel);    //better to set the panel as the content pane than to add it.
     
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        setBounds(200, 200, 300, 300);
        setVisible(true);
        backbuffer = new BufferedImage(290, 290, BufferedImage.TYPE_4BYTE_ABGR);
       
        thread = new Thread(this);
        thread.start();
       
        try
        {
            bi = ImageIO.read(getClass().getResourceAsStream("/Image.jpg"));
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }

    public void run()
    {
        while (true)
        {
            g2D = backbuffer.createGraphics();
            Insets border = getInsets();
            g2D.setColor(Color.WHITE);
            g2D.fillRect(0, 0, 290, 290);
            g2D.drawImage(bi, 0, 0, null);
            g2D.setColor(Color.BLACK);
            g2D.setFont(new Font("Rainbow6", Font.PLAIN, 50));
            g2D.drawString("Xiss Burg", 10 + (int)(30 * Math.cos(System.currentTimeMillis() * 0.001)), 100 + (int)(30 * Math.sin(System.currentTimeMillis() * 0.001)));
            getContentPane().paintComponents(g2D);   //paint to the correct component.  the JPanel contained the button and text field.
           g2D.dispose();
            getGraphics().drawImage(backbuffer, border.left, border.top, null); //need to take into account the JFrame border.


            try
            {
                Thread.sleep(30);
            }
            catch (InterruptedException e)
            {
                JOptionPane.showMessageDialog(null, "The thread has thrown an InterruptedException:\n"
                                                    + e.getMessage());
            }
        }
    }
   
    public static void main(String args[])
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                Test m = new Test();
            }
        }
        );
    }
}


Offline broumbroum

Junior Member





« Reply #6 - Posted 2007-04-17 16:16:12 »

hi!
I have considered your lists for a ltille while.. It is worth to make the threads interruptible at any point of your application state. Thus I often recommend to load Timers instead of Threads with internal loops, because of Java Access Security enabled for IO operations mostly.
Then try to replace the runned Thread with a simple javax.swing.Timer implementing the given ActionListener. Thereafter your application will become more flexible. Mainly :
1  
2  
javax.swing.Timer timer = new javax.swing.Timer(tickTime, actionListener);
timer.start();

could be stopped when required:
1  
timer.stop();


 Roll Eyes

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline xiss burg

Junior Newbie





« Reply #7 - Posted 2007-04-17 22:20:55 »

Thank you CaptainJester, it works the way I want  Cheesy Thanks for showing me 'Insets'. I didn't know it.

And broumbroum, thanks for the suggestion. But I didn't understand why you are suggesting it. could you explain better or show me any article that explains it better?!

Thank yall
Offline broumbroum

Junior Member





« Reply #8 - Posted 2007-04-18 00:20:57 »

I suggested to use a Timer instead of a Thread instance in that case, because of the requested ImageIO process at the begining of the code seen beyond my post. To be clearier, the loop while(true) has better to be replaced with a Timer sending succeeding ActionEvents. Plus you can restart the Timer at any time with its restart() method without losing your Timer instance. And finally it would be great to instance FileChannelImageInput for an image file or ImageInputStream, because of their flexibility regarding Thread-safety....
This worked for me.
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  
(...)
 thread = new Thread(this);
        thread.start();
       
        try
        {
            bi = ImageIO.read(getClass().getResourceAsStream("/Image.jpg"));
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
 (...)
 public void run()
    {
        while (true)
        {
            g2D = backbuffer.createGraphics();
            Insets border = getInsets();
            g2D.setColor(Color.WHITE);
            g2D.fillRect(0, 0, 290, 290);
            g2D.drawImage(bi, 0, 0, null);
            g2D.setColor(Color.BLACK);
            g2D.setFont(new Font("Rainbow6", Font.PLAIN, 50));
            g2D.drawString("Xiss Burg", 10 + (int)(30 * Math.cos(System.currentTimeMillis() * 0.001)), 100 + (int)(30 * Math.sin(System.currentTimeMillis() * 0.001)));
            getContentPane().paintComponents(g2D);   //paint to the correct component.  the JPanel contained the button and text field.
           g2D.dispose();
            getGraphics().drawImage(backbuffer, border.left, border.top, null); //need to take into account the JFrame border.


            try
            {
                Thread.sleep(30);
            }
            catch (InterruptedException e)
            {
                JOptionPane.showMessageDialog(null, "The thread has thrown an InterruptedException:\n"
                                                    + e.getMessage());
            }
        }
    }


.
For instance, try to interrupt the thread while it is running Image operation like loading and/or drawing simultaneously. You should notice the lock on the runnin Thread.
Further simply imagine loading multiple images for drawing them next in the same Thread. How could you stop the operation if wanted so?
http://builder.com.com/5100-6370-5144546.html , a web page talkin about it.

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline xiss burg

Junior Newbie





« Reply #9 - Posted 2007-04-18 20:47:52 »

I already developed a game using a very similar algorithm, using a thread with a while(true)(I learned it from a Java game programming tutorial), and it works fine. I never had any problems with that, and I do operations like load images(when loading the game for example) inside this thread with no problems. The Timer also seems very good to do such things. I'll try it out on my next applications. Thanks again.
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.

BurntPizza (7 views)
2014-09-21 02:42:18

BurntPizza (8 views)
2014-09-21 01:30:30

BurntPizza (11 views)
2014-09-21 00:34:41

moogie (11 views)
2014-09-21 00:26:15

UprightPath (23 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56
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!