Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Temporary textarea in game applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Temporary textarea in game applet" Watch "Temporary textarea in game applet" New topic
Author

Temporary textarea in game applet

Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
I'm writing an applet to play chess and I'd like the game history (just a list of moves) to appear in a textarea on screen. I don't mind if it fills the whole frame or is just overlaid on top of the game. I need to be able to output and input text through it. I also need to remove it (or make invisible?).

I have little experience with text or panels in applets so my questions may appear a bit simple. I currently use very basic graphics constructs:



Doing the above I have been able to make a text area appear but I can't make it go away. The chess game as it stands appears in the link below - if you click the pawn outside the board area then it shows the game history on screen but not in a textarea.

Any ideas?


Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Do I need to provide more info or is this just quite a difficult to do?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Why are you toying with custom painting?
Add the text area to a container, add the container to the applet.
To display the moves, use JTextArea#append
When you want to show/hide invoke JTextArea.setVisible(boolean visible)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Maneesh Godbole wrote:Why are you toying with custom painting?

because it worked for what I was doing.

Maneesh Godbole wrote:Add the text area to a container, add the container to the applet.
To display the moves, use JTextArea#append
When you want to show/hide invoke JTextArea.setVisible(boolean visible)
This is the bit I need help on
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Mich Robinson wrote:because it worked for what I was doing.

Well you are doing it wrong
Mich Robinson wrote:This is the bit I need help on

Recommended reading
http://download.oracle.com/javase/tutorial/uiswing/layout/using.html
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Maneesh Godbole wrote:Recommended reading ...
err ... thanks

Anybody got a few minutes to offer some help I can understand?
If it's not too much trouble then some suggestions for a few lines of code and roughly where to add it in my applet.
Unfortunately my attempts with panels haven't gone well even though I have tried reading the various tutorials.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

I went there and played a game. Fantastic! I had fun!
So, I am going all out to help you...

Firstly,
1) On clicking the pawn, the history toggles nicely. So, it is working fine. Where/what is the issue you are facing? based on your first message, you seem to want the history appear on the side...
2) And in the code posted, you are indeed using a TextArea...are you using AWT? instead of passing the history every time to the text area, you can just create a JTextArea and then call append() method on it as suggested.
or you can just call setText(gameHistory) - that will replace the whole text and you don't need to recreate it every time.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

On deeper thoughts, I think this is what you should do:

1) Set the layout of the main panel to CardLayout
2) Add a jpanel - name it boardPanel - this will display the board and you can continue to do your custom painting - instead of overriding, paint, you should override paintComponent
3) Add another jpanel - name it historyPanel - add a jtextarea to that.

now, when user clicks on the pawn, you can just call cardLayout.next() - as there are only 2 components, it will toggle nicely.

coming back to the historyPanel, I think a jTable will fit this requirement better than a text area. You can create a JTable with say 30 rows and 3 columns initially - this will show upto 90 moves. If it goes higher, you will have to add a column at runtime. This is assuming, you have one cell for both moves - you can also think of having one cell for each move of both the players - in this case, the row count will remain the same and you will create 6 columns instead of the initial 3 and if the game extends, you will keep adding 2 columns...

so: create a jtable, make it non-editable, call setShowGrid(false).

and then as and when moves are added, you have to call setValueAt(value, row, column) to set the move - if there are 2 columns for each player, this will be even more easier...
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Ranganathan Kaliyur Mannar wrote:I went there and played a game. Fantastic! I had fun!

Glad you liked it Did you win?

Ranganathan Kaliyur Mannar wrote:Where/what is the issue you are facing?

At the moment I'm just using drawString to show the moves. This works fine but the user can't copy the game or enter a game or enter a position using standard notation. This means the history function is just a pretty add-on rather than something useful. I don't care where the history appears as long as the user can copy all the text or paste the text of an existing game in. For this reason I suspect the textarea is more suited as I don't have to care about how many moves the user has.

Ranganathan Kaliyur Mannar wrote:are you using AWT?

I'm currently using AWT. The setText(gameHistory) sounds useful. My problem is getting anything with Panels to work. I just don't understand the basics of what to call and where to put the code in the applet. The manual pages look helpful but don't tell you what to import. The code snippets don't work together and they don't say where to put the code in an applet or whether it will even work in an applet. I can get quite frustrating! I'm also not keen on rewriting my whole program.

I think what I need to know is how to create the panel and add the textarea. Then how to toggle the textarea on and off. This is what I have at the moment and currently nothing shows up

import javax.swing.*;
...
public class chess extends Applet implements Runnable, MouseListener {
...
JPanel panel = new JPanel(new BorderLayout());
JTextArea HistoryPanel = new JTextArea("test", 30, 70);
pane.add(HistoryPanel, BorderLayout.PAGE_START)
...
init(
HistoryPanel.setVisible(true);

}

Many thanks for your time!

EDIT : just noticed your 2nd post and will try to alter my code to suit.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
I like the idea of the cards - that seems sensible but I'd prefer to stick with text area as it seems more flexible.

Added the following code but I'm just guessing what goes where.


What do I do to write (drawString, drawImage etc) to the board card?
How do I create the history card and add the text area?
How do I switch between them?

Afraid I'm clueless about this aspect of Java.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Mich Robinson wrote:Afraid I'm clueless about this aspect of Java.


Then it's about time you learned. You can't just say "Sorry, all I know how to do is drawString" if you expect to do anything else. So put aside the game for a while and go through some basic tutorials.

Unfortunately I don't think there are any AWT tutorials, since it's been obsolete for over 10 years now. So you'll have to make do with Swing tutorials.

Really. Read the tutorials. They come with code, so take the code and mess around with it. Change it around and see what happens. Keep going until you get comfortable with it, then go on to the next tutorial.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Paul Clapham wrote:Really. Read the tutorials. They come with code, so take the code and mess around with it. Change it around and see what happens. Keep going until you get comfortable with it, then go on to the next tutorial.

Taking the example link given previously - the code snippets don't work together. There's no clue offered as to where you place the code. It doesn't even mention that you have to import swing. I tried (and indeed am still trying) to take this code (and other code snippets taken from elsewhere) and get them to work.

From previous experience I know that after a few days I'll have something that half works but is a real mess. I guess I can't expect much more from code snippets that are gathered from lots of examples and then plugged together. After a week I'll then throw the code as I'm uncomfortable having it in my project.

The only reason why I'm trying to use swing/awt is to add a feature that my users have asked for (they want to copy the chess games they've played against my program onto other systems). My little program is free and moderately popular. I assumed I only need a few commands to display the text area, and then make it disappear, and I was hoping someone could give me a reasonably strong clue or a code sample that works. I didn't think it would take much time but obviously I was wrong there.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Mich Robinson wrote:Taking the example link given previously - the code snippets don't work together.


That's an example of what you are doing wrong. Code snippets are useless -- as you say, if you get a bunch of code snippets and throw them together then you do indeed end up with a real mess. So don't look for code snippets. And don't expect somebody to give you a tiny hint which will magically fix all your problems. You actually have to learn how things work. Sorry to throw cold water on your ambitions, but that's how it is.

But don't start with the tutorial about layout managers. (That was the link you referred to, right?) Start at the beginning of the Swing tutorial trail.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Paul Clapham wrote:Start at the beginning of the Swing tutorial trail.
Paul, I'm not trying to learn swing or awt - I just want to display some text in an applet that a user can copy and I came here to try and get some help. I already help others out on your sister forum "Java game development" and I assumed each sub forum would offer the same level of help.

To print text that can be copied is pretty simple in most languages (I moderate elsewhere on forums for PHP, HTML and Unix shell) and guess it would take a minute to explain. I just need a minute of someone's time to explain how to do it in Java. My existing applet is fairly complicated so I can't rewrite the whole thing to offer one tiny new feature.

I'm sure you're a nice guy Paul but if you're not willing to help then please feel free to dedicate your words of wisdom to another thread.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. Here it is in two sentences.

(1) Have a text area in your applet.

(2) Call setText() on that text area when you want to set its text.

Your initial code was kind of incomplete, but it looked like you were trying to create a text area from your paint method. Don't do that. Just put it there in the initialization. Don't do anything other than painting in the paint method.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Thanks - I'll experiment with that.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Hi,
I had explained how to use CardLayout in my second post. I am providing a code example here:
This explains how to toggle the view (in the next post I will talk about drawing on the boardPanel.
(class BoardPanel appears in the next post):

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Here is the code for BoardPanel:



and about users wanting to copy the history, I think you can just provide a simple 'Copy History' button somewhere - when user clicks on it, you write code to copy the moves directly to the clipboard:
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Thanks for your assistance - it's much appreciated.

I've moved the body of the paint process to the new class but sadly it uses 100's of variables and methods in the old class. I'll probably be quiet for a day or two as I need to specify what class all the existing items belong to. I assume there isn't a way to just say grab all the instance variables (& methods) from a class without having to do it for each occurrence.

I've no doubt there's a better way I should probably of written this but such is life
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

In that case, just make BoardPanel an inner class and then you would be able to access all variables including private ones by getting hold of the parent's instance:
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Excellent - I'll try that.

While searching the net for examples of CardLayouts I came across classes for transfering between java and the clipboard. Does this work as I think it does ie I could create a button etc and when the user presses it I could transfer the game history to their clipboard as if they've copied it? If so I'm made. Would it work with Linux and Apple? Assume it would go both ways ie I could import as well?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

That is what I had provided in my earlier post:
About users wanting to copy the history, I think you can just provide a simple 'Copy History' button somewhere - when user clicks on it, you write code to copy the moves directly to the clipboard:

the above code will copy all the text present in the text area and place it in the clipboard.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Sorry - you were way ahead of me then.

To use the above code do I need to have a swing text field on screen? I only ask because I don't have any swing stuff in place at the moment. I tried looking for code that might allow me to copy and paste a standard string and found the following.

It compiled fine and looked like it would work but I had a security error when running it. I assume the applet needs to be signed or the user needs to alter their security settings. The first option seems like overkill while the second is totally unreasonable. Does the JTextComponent method hit the same problem? Could I use the JText stuff on a simple string without invoking a swing interface? I'm just wary of devoting more time to a wild goose chase when I could be improving the games playing strength
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Applets are run in a sandbox with very limited possibilities. Among other, no access to the file system, to URLs other than those on the same server as the applet, and no access to the clipboard. Without these limitations a malicious site could include an applet that takes the current contents of the clipboard and sends it to their server. What if there's a password on the clipboard?

So yes, to bypass any of these possibilities the applet needs to be signed, and users must accept the signature.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Thanks for info - back to plan A and swing then.

It's a shame they couldn't just have a confirmation window to say "allow this applet to copy the clipboard" though.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Mich Robinson wrote:To use the above code do I need to have a swing text field on screen? I only ask because I don't have any swing stuff in place at the moment.


Yes... The copy() method that I used is present only in javax.swing.JTextComponent (parent of swing tect components). For AWT, I think you have to go through the Toolkit class only.
 
 
subject: Temporary textarea in game applet
 
Similar Threads
My paint method is moody today
back_buffer ?
Calling the createImage() function
Double Buffering Problem
Occasional random pauses during game