Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

Swing Question: Dynamic Labels

Thread view: 
KDawg44 - 01 Apr 2008 01:37 GMT
Hi,

For a development class we have to solve the Knight problem using a
GUI.  I want to make this a dynamic sized board.

Is there an easy way to have a textfield take a number, then
dynamically draw the board using an array of labels?  Or would I want
to use something like a JDialog, get the number, then draw the main
GUI (and how would I go about doing that)?

Thank you for your help.

Kevin
Danno - 01 Apr 2008 03:21 GMT
> Hi,
>
> For a development class we have to solve the Knight problem using a
> GUI.  I want to make this a dynamic sized board.

> Is there an easy way to have a textfield take a number, then
> dynamically draw the board using an array of labels?  Or would I want
[quoted text clipped - 4 lines]
>
> Kevin

Question: What is each square or "textfield" going to have in it?
Mark Space - 01 Apr 2008 03:41 GMT
> Is there an easy way to have a textfield take a number, then
> dynamically draw the board using an array of labels?  Or would I want

Maybe.  You will have to learn some Java -- there's no super easy, but
you might be able to use the GridLayout.  Try reading this for now, you
are going to need it.

<http://java.sun.com/docs/books/tutorial/uiswing/>

more to come (NetBeans just crashed on me in the middle of debugging
session....)
Mark Space - 01 Apr 2008 04:16 GMT
>> Is there an easy way to have a textfield take a number, then
>> dynamically draw the board using an array of labels?  Or would I want
>
> Maybe.  You will have to learn some Java -- there's no super easy, but

That was supposed to be "no super easy way."

> you might be able to use the GridLayout.  Try reading this for now, you
> are going to need it.

Yep, works, although you still have some work to do.

First, use JOptionPane.showInputDialog() to get the size of the board.

        String result = JOptionPane.showInputDialog(
                "Enter the number of squares per side", "5" );

Then use the GridLayout() to add your labels.  This is text, but you
could make an Icon and get something like graphics (probably).  You
*must* learn how JPanels, JFrames and a layout manager works, so hit
that tutorial link I have you.  Especially JFrames.

A grid layout lets you add() components, starting in the upper left hand
corner, going left to right, and then fills each row completely before
going to the next one.  It's very simple, but probably all you need.
You can fill it with two simple for loops.

Here you go, good luck.

package simpleswinggrid;

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Board extends JFrame
{

    public static void main(String[] args) {
        String result = JOptionPane.showInputDialog(
                "Enter the number of squares per side", "5" );
        int sides = Integer.parseInt( result );
        if( sides > 10 ) {
            throw new RuntimeException( "Sides can't exceed 10" );
        }
        new Board( sides ).setVisible( true );
    }

    public Board( int sides ) {
        super( "A Sample Game Board");
        GridLayout layout = new GridLayout( sides, sides );
        JPanel panel = new JPanel( layout );

        for( int i = 0; i < sides; i++ ) {
            for( int j = 0; j < sides; j++ ) {
                panel.add( new JLabel( "("+i+","+j+")" ) );
            }
        }
        setContentPane( panel );
        pack();
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        setLocationRelativeTo(null);
    }
}
wizard of oz - 01 Apr 2008 05:19 GMT
Do you mean the Knights tour?

You could of course simply have a JFrame with a JPanel upon which you draw
whatever you need to draw (the path perhaps?). Simply divide the height and
width of the panel by the # rows +1 and # cols +1 to determine your cell
size. The Top left corner of cell (x, y) can be found by multiplying x by
the cell width and y by the cell height. You might want to allow a few
pixels around the whole thing so that there is a bit of padding from the
edges of the window frame. You could also use Borders to achieve this
affect.
Example:
cellWidth = (knightsTourPanel.getWidth () - PADDING * 2) / columnCount;

cellLeft = cellWidth x colNum + PADDING;

Similarly for the cell Height and cellTop coordinate. I'll leave it to you
to figure that out and how to figure out the bottom left coordinates and
center point.

BTW. The way you stated your question, it sounds like using a GUI is part of
the learning activity - shouldn't you do your own assignment?
Anyway here is a tip, don't work out the tour in your paint method - work
out the path in another thread and save the path in memory (e.g. a linked
list). The paint method should simply display the contents stored in memory
(i.e. be very fast).

Good luck with it

> Hi,
>
[quoted text clipped - 9 lines]
>
> Kevin
Chase Preuninger - 01 Apr 2008 15:21 GMT
Look at GridLayout.

http://groups.google.com/group/java-software-develoupment?hl=en
KDawg44 - 02 Apr 2008 02:27 GMT
On Apr 1, 10:21 am, Chase Preuninger <chasepreunin...@gmail.com>
wrote:
> Look at GridLayout.
>
> http://groups.google.com/group/java-software-develoupment?hl=en

Thanks to all. I have what I need.

Thanks!  Yes its an assignment that I should be doing myself but since
the dynamic part was not part of it and I am trying to extend my
learning so I think its okay.  :)

Thanks to everyone.
Roedy Green - 02 Apr 2008 16:50 GMT
>Is there an easy way to have a textfield take a number, then
>dynamically draw the board using an array of labels?  Or would I want
>to use something like a JDialog, get the number, then draw the main
>GUI (and how would I go about doing that)?

The way I would do it is with a custom component.  It would place the
pieces with drawImage. It would decide which square had been clicked
by dividing x and y by a constant.  Back in the Java 1.0 days, you
pretty well had to do it this way in any grid game to get decent
speed, though chess is a pretty slow-paced.  After you get that
working, allow scaling of the component, when everything grows,
including the images.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.