Solved

GUI's in Java

Posted on 2010-11-17
13
600 Views
Last Modified: 2012-05-10
Hey everyone, I hope some of you have a lot of experience with GUI's in Java. I've never made one. Since I'm lazy and I have this membership I thought I would ask some experts to see if anyone has anything really useful.

I need to quickly and easily make a simple GUI for a chess program I am making. It needs to be a window with a picture of the current chessboard state, and a box (kindof like the CLI) that displays info and takes in new moves and orders as string inputs (like a CLI).

Please post helpful information, recommendations from experience, simple examples, and library recommendations. Please post relevant examples based on your personal experience and recommendations. Also, anything specifically involving a picture made up of sub-pictures would be great. I.E.- how to form a chessboard graphically and simply.

Thanks Experts!

-Jeff
0
Comment
Question by:jeffiepoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 92

Accepted Solution

by:
objects earned 350 total points
ID: 34159692
0
 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 150 total points
ID: 34162626
Have a look a this :
http://www.geekpedia.com/code134_Chess-Game-In-Java.html
you have only to download and rename accordingly some pictures from somewhere.
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 34181384
objects, that is a wonderful example. I have been able to implement my own basic chessboard now.

Valeri, that is an amazing example. I'm going to need to reference that for buttons.

Speaking of buttons, that is all I need to get working now. I can't figure this out for the life of me. For instance. How would I add a simple "Hello World" underneath or on top of the chessboard objects cited? I just need to figure out how to get a button to show up anywhere outside the chessboard and I can do the rest.

Thanks for any additional help guys!

-Jeff
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 92

Expert Comment

by:objects
ID: 34181504
create another panel and add your label and chessboard to that

JPanel panel = new JPanel(new BorderLayout());
panel.add(BorderLayout.NOTH, new JLabel("Hello world"));
panel.add(BorderLayout.CENTER, chessboard);
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 34181606
When I add that code in to the exact example given, the Board now becomes blank. It doesn't seem to work.
0
 
LVL 92

Expert Comment

by:objects
ID: 34181660
you need to also add the panel to your gui (probably where you currently add the board)
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 34202093
So i added

    JPanel panel2;   // to the global variables list right by chessboard, (from your last comment)

//I added this right above where the pieces (pictures) are put in, like you suggested
       panel2 = new JPanel(new BorderLayout());
       panel2.add(BorderLayout.NORTH, new JLabel("Hello world"));
       panel2.add(BorderLayout.CENTER, chessBoard);

It still seems to be blank
0
 
LVL 92

Expert Comment

by:objects
ID: 34202117
where do u add panel2 to your gui?
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 34202711
for instance, referenceing the link above: http://www.roseindia.net/java/example/java/swing/chess-application-swing.shtml

I added it as follows. Try this out
private static final long serialVersionUID = 1L;
	JLayeredPane layeredPane;
    JPanel chessBoard;
    JPanel panel2;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
	
	public ChessGUI(){
		
		Dimension boardSize = new Dimension(600, 600);
		
        //  Use a Layered Pane for this this application
        layeredPane = new JLayeredPane();
       getContentPane().add(layeredPane);
       layeredPane.setPreferredSize(boardSize);
       layeredPane.addMouseListener(this);
       layeredPane.addMouseMotionListener(this);

       //Add a chess board to the Layered Pane 

       chessBoard = new JPanel();
       layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
       chessBoard.setLayout( new GridLayout(8, 8) );
       chessBoard.setPreferredSize( boardSize );
       chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

       for (int i = 0; i < 64; i++) {
           JPanel square = new JPanel( new BorderLayout() );
           chessBoard.add( square );

           int row = (i / 8) % 2;
           if (row == 0)
               square.setBackground( i % 2 == 0 ? Color.blue : Color.white );
           else
               square.setBackground( i % 2 == 0 ? Color.white : Color.blue );
       }
       
       panel2 = new JPanel();
       panel2.setLayout(new BorderLayout());
       panel2.add(BorderLayout.NORTH, new JLabel("Hello world"));
       panel2.add(BorderLayout.CENTER, chessBoard);
       
       //Add a few pieces to the board

       JLabel piece = new JLabel( new ImageIcon("piece.jpeg") );
       JPanel panel = (JPanel)chessBoard.getComponent(0);
       panel.add(piece);

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 34202758
>        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);

>        panel2.add(BorderLayout.CENTER, chessBoard);

your adding the chessboard in two places
a component can only be added to 1 container

and you never add panel2 to anything (so it never appears)

try this:


       chessBoard = new JPanel();
       chessBoard.setLayout( new GridLayout(8, 8) );
       chessBoard.setPreferredSize( boardSize );
       chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

       for (int i = 0; i < 64; i++) {
           JPanel square = new JPanel( new BorderLayout() );
           chessBoard.add( square );

           int row = (i / 8) % 2;
           if (row == 0)
               square.setBackground( i % 2 == 0 ? Color.blue : Color.white );
           else
               square.setBackground( i % 2 == 0 ? Color.white : Color.blue );
       }
       
       panel2 = new JPanel();
       panel2.setLayout(new BorderLayout());
       panel2.add(BorderLayout.NORTH, new JLabel("Hello world"));
       panel2.add(BorderLayout.CENTER, chessBoard);

       layeredPane.add(panel2, JLayeredPane.DEFAULT_LAYER);
       
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 34202936
That makes sense why that would help. I made the recommended changes and still the same results. Thanks for sticking with me here. The file is attached and should compile correctly as I uploaded it.
ChessGUI.java
0
 
LVL 92

Expert Comment

by:objects
ID: 34202976
as you're using a layed panel you need to explicitly set the size of panel2
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 34209064
This is what finally worked for me. You don't add the new panel to the layered pane, you add the layered pane to the panel, then add the new panel to the (getContentPane()) whatever that is.
layeredPane = new JLayeredPane();
       //getContentPane().add(layeredPane);
       layeredPane.setPreferredSize(boardSize);
       layeredPane.addMouseListener(this);
       layeredPane.addMouseMotionListener(this);

       chessBoard = new JPanel();
       layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
       chessBoard.setLayout( new GridLayout(8, 8) );
       chessBoard.setPreferredSize( boardSize );
       chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

       for (int i = 0; i < 64; i++) {
           JPanel square = new JPanel( new BorderLayout() );
           chessBoard.add( square );

           int row = (i / 8) % 2;
           if (row == 0)
               square.setBackground( i % 2 == 0 ? Color.darkGray : Color.white );
           else
               square.setBackground( i % 2 == 0 ? Color.white : Color.DARK_GRAY );
       }
       
       panel2 = new JPanel();
       panel2.setPreferredSize(new Dimension(800, 800));
       //layeredPane.add(panel2, JLayeredPane.DEFAULT_LAYER);
       panel2.setLayout(new BorderLayout());
       

       panel2.add(BorderLayout.NORTH, new JLabel("Hello world"));
       panel2.add(BorderLayout.CENTER, layeredPane);
       getContentPane().add(panel2);

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a previously published article (http://www.experts-exchange.com/articles/10331/Automatic-Duplex-Scanning-in-PaperPort-Versions-11-12-14.html) here at Experts Exchange, I explained how to achieve duplex (double-sided) scanning in Nuance's PaperPor…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The goal of the tutorial is to teach the user how to edit a basic portrait of someone. This tutorial shows how to edit two basic photographs, one of a car and one of a person.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question