I'm making the game "Who is millionaire".

This is the help panel, which let user choose one of the options such as: calling friend, asking audience, etc.

But I have a problem, the options are ellipses, which are drawn in class Help_Option extends JComponent. When I test this class Help_Option individually, it works fine. But when I add the Help_Option object into the game panel, actually a sub-panel in the frame, it just displays a line on the panel, it doesn't draw my ellipse.

This is my code:

Note: a is JFrame, I don't copy the whole method initialize(JFrame a) cos it's quite long and I don't think that the error comes from there.

   /******Helper panel**********/
            JPanel help_area_container = new JPanel();

            help_area_container.setBorder(BorderFactory.createLineBorder(Color.BLUE, 3));
            help_area_container.setLayout(new GridLayout(4,0));

                JPanel voting_container = new JPanel();
                JPanel calling_container = new JPanel();
                JPanel half_container = new JPanel();
                JPanel take_container = new JPanel();
                JPanel[] all_help_container = new JPanel[]{voting_container, calling_container, half_container, take_container};
                for(int i = 0; i < all_help_container.length; i++){
                    all_help_container[i].setBorder(BorderFactory.createLineBorder(Color.RED));
                    all_help_container[i].setPreferredSize(new Dimension(350, help_area_container.getPreferredSize().height/4));
                }

                for(int j = 0; j < all_help_container.length; j++){
                    help_area_container.add(all_help_container[j]);
                }

                Help_Option voting_option = new Help_Option(all_help_container[0].getPreferredSize().width, all_help_container[0].getPreferredSize().height);
                voting_option.setPreferredSize(new Dimension(all_help_container[0].getPreferredSize().width, all_help_container[0].getPreferredSize().height));
                all_help_container[0].add(voting_option);

a.add(help_area_container, BorderLayout.EAST);
            /*****************************/

This is the Help_Option class:

class Help_Option extends JComponent implements MouseMotionListener{
    private static int x, y;
    private Ellipse2D ellipse;
    private Color c = Color.BLACK;    

    public Help_Option(int x, int y){
        Help_Option.x = x;
        Help_Option.y = y;
        ellipse = new Ellipse2D.Double(0, 0, x, y);
        this.addMouseMotionListener(this);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.BLUE);
        g2d.draw(ellipse);

        g2d.setColor(c);
        g2d.fill(ellipse);        

        g2d.setColor(Color.RED);
        g2d.setFont(new Font("TimesRoman", Font.BOLD, 20));
        g2d.drawString("Here I am", 250, 100);
    }

    public void setColor(Color c){
        this.c = c;
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if(ellipse.contains(e.getX(), e.getY())){
            setColor(Color.GREEN);
            repaint();
        }else{
            setColor(Color.BLACK);
            repaint();
        }
    }
}

And this is the class that i used to test Help_Option class:

public class Help extends JFrame{

    public static void main(String [] agrs){
        Help h = new Help();
        h.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        h.init();
    }

    public void init(){
        this.setLayout(new FlowLayout());
        this.setSize(2000, 1000);

        JPanel a = new JPanel();
        a.setPreferredSize(new Dimension((int)a.getSize().width/3, (int)a.getSize().height/2));
        a.setBorder(BorderFactory.createLineBorder(Color.yellow, 3));
        Help_Option k = new Help_Option(a.getPreferredSize().width, a.getPreferredSize().height/2);
        k.setPreferredSize(new Dimension(a.getPreferredSize().width, a.getPreferredSize().height));
        a.add(k);

        this.add(a);
        this.setVisible(true);
    }
}

EDIT

This is the link to my classes, please take a look at them. The error is described above.

有帮助吗?

解决方案

It is that you haven't set values for your first couple of JPanels and they are returning preferred sizes of 0. Dimensions of 0,0

So you should add values to the JPanels there.

public static void main(String[] args) {

    JPanel help_area_container = new JPanel();

    help_area_container.setBorder(BorderFactory.createLineBorder(
            Color.BLUE, 3));
    help_area_container.setLayout(new GridLayout(4, 0));

//Should have set sizes below
    JPanel voting_container = new JPanel();
//voting_container.setSize(50,50);
    JPanel calling_container = new JPanel();
    JPanel half_container = new JPanel();
    JPanel take_container = new JPanel();
    JPanel[] all_help_container = new JPanel[] { voting_container,
            calling_container, half_container, take_container };
    for (int i = 0; i < all_help_container.length; i++) {
        all_help_container[i].setBorder(BorderFactory
                .createLineBorder(Color.RED));
        all_help_container[i].setPreferredSize(new Dimension(350,
                help_area_container.getPreferredSize().height / 4));
    }

    for (int i = 0; i < all_help_container.length; i++) {

        System.out.println(all_help_container[i].getSize());

    }

}

// where you can change the size
    all_help_container[0].setSize(50, 50);

    System.out.println("----");

    for (int i = 0; i < all_help_container.length; i++) {

        System.out.println(all_help_container[i].getSize());

    }
}

Hopefully this will help you with the sizing of your GUI. You will have to adjust the different panels to suit your needs. As I'm guessing that this panels will contain some other stuff in them.

You may want to create custom JPanels for each of these, so that you can easily check if they are the right size.

public class VotingPanel extends JPanel {

    public VotingPanel(){

      //All your variables such as size and color schemes

    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top