8 Replies - 524 Views - Last Post: 26 April 2011 - 07:35 AM Rate Topic: -----

#1 damiencourt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 11-April 11

A good way to develop GUI ?

Posted 25 April 2011 - 06:36 PM

Hi there,
I have a class called Table which extends JFrame. I'll use it to display a poker table, so it will have a few JButtons, labels, images and I'll also try to add a chat box(not sure what to use for that yet) I'm not very experienced with graphics and I'm not sure what's the right way to develop the table.
At the moment I'm using paint() to draw the background and table images. The buttons and labels I add to a JPanel. The problem is that only one of the buttons shows up when I start the application and the rest appear when I move the mouse over their supposed location. If I don't override paint, all buttons show up.
What should I do? Stop overriding paint and display the images in a label or try to draw the buttons from within paint(can I do that?) I doubt both are good ideas, so if anyone knows a better way to do it, please let me know.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: A good way to develop GUI ?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: A good way to develop GUI ?

Posted 25 April 2011 - 08:30 PM

Does your paint() method looks like that ?

public void paint(Graphics g) {
    super.paint(g);     // to draw the background and the JComponent
    // my personal drawing
    ... here
}


Was This Post Helpful? 0
  • +
  • -

#3 damiencourt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 11-April 11

Re: A good way to develop GUI ?

Posted 26 April 2011 - 04:12 AM

No, I was doing that:

public void paint(Graphics g)
{
g.drawImage(backImage,0,0,null);
g.drawImage(tableImage,100,80,null);
buildPanel();
}



buildPanel() is used to add the different components.
public void buildPanel()
{
panel.setLayout(null);
panel.add(callButton);
callButton.setBounds(300,440,70,30);
Was This Post Helpful? 0
  • +
  • -

#4 damiencourt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 11-April 11

Re: A good way to develop GUI ?

Posted 26 April 2011 - 04:23 AM

No, I was doing that:
public void paint(Graphics g)
    {
    	g.drawImage(backImage,0,0,null);
    	g.drawImage(tableImage,100,80,null);
    }[code]


buildPanel() is used to add the different components.
public void buildPanel()
	{
		panel.setLayout(null);
		panel.add(callButton);
                callButton.setBounds(300,440,70,30);
                add(panel);
         }[code]

I call buildPanel in the constructor of the Table object. I tried using super.paint(g) but got the same results

PS: Aaarhg, sorry for the earlier post, sosmehow submitted it before finishing

This post has been edited by damiencourt: 26 April 2011 - 04:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1907
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: A good way to develop GUI ?

Posted 26 April 2011 - 04:36 AM

The panel will remember all the buttons that are on it. When you override paint do this:

public void paint(Graphigs g) {
  // Draw any backgrounds here (underneath the buttons)

  // Now get Java to draw all the buttons for you.
  super.paint(g);

  // Draw any foregrounds here (on top of the buttons)
}

This post has been edited by cfoley: 26 April 2011 - 04:36 AM

Was This Post Helpful? 0
  • +
  • -

#6 damiencourt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 11-April 11

Re: A good way to develop GUI ?

Posted 26 April 2011 - 06:22 AM

Nope...still it only displays one of the buttons, and the rest become visible if I move the mouse over their location.
I made the code as simple as possible and posted it below. It's pretty straightforward so I removed the comments as well to make it shorter. I hope you'll spot what Im doing wrong.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

public class Table extends JFrame
{
	private BufferedImage tImage	= null;
	private BufferedImage bImage    = null;
	private JButton	foldButton 	    = new JButton("Fold");
	private JButton	callButton 	    = new JButton("Call");
	private JButton x2		   = new JButton("2x");
	private JButton x3		   = new JButton("3x");
	private JPanel panel 		= new JPanel();

        private String	path = "table.png";
	private String	path2 = "bg.jpg";

	public Table()
	{
		loadImages();
		setSize(880,600);
		buildPanel();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void buildPanel()
	{
		panel.setLayout(null);
		panel.add(callButton);
		panel.add(foldButton);
		panel.add(x2);
		panel.add(x3);
		foldButton.setBounds(200,440,70,30);
		callButton.setBounds(300,440,70,30);
		x2.setBounds(200,500,50,30);
		x3.setBounds(260,500,50,30);
		add(panel);

	}
	public void loadImages()
	{
	 try
		{
			bImage =  ImageIO.read(new File(path2));
			tImage = ImageIO.read(new File(path));

		}catch (IOException ex){ ex.printStackTrace();}

	}

    public void paint(Graphics g)
    {
      	g.drawImage(bImage,0,0,null);
    	g.drawImage(tImage,100,80,null);
    	//super.paint(g);
    }
}


This post has been edited by damiencourt: 26 April 2011 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1907
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: A good way to develop GUI ?

Posted 26 April 2011 - 06:27 AM

You have commented out your super.paint(g)
Was This Post Helpful? 0
  • +
  • -

#8 damiencourt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 11-April 11

Re: A good way to develop GUI ?

Posted 26 April 2011 - 06:33 AM

I know, if I call super.paint(g) the background images don't get displayed.
If I don't call it everything shows up, but in the manner I explained above

I just noticed that the only button that gets displayed immediately is the one that is the closest to the top left corner...

This post has been edited by damiencourt: 26 April 2011 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1907
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: A good way to develop GUI ?

Posted 26 April 2011 - 07:35 AM

If you were using a JPanel, you could call setOpaque(false);. That option is not available on a JFrame. My suggestion is to move your drawing to a class that extends JPanel and put that into your JFrame.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1