简体   繁体   English

在秋千Java中绘图

[英]drawing in swing Java

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. 但是我有一个问题,选项是椭圆,它在类Help_Option扩展JComponent中绘制。 When I test this class Help_Option individually, it works fine. 当我分别测试此类Help_Option时,它可以正常工作。 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. 但是,当我将Help_Option对象添加到游戏面板中时,实际上是框架中的一个子面板,它只是在面板上显示一条线,而不画椭圆。

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. 注意:a是JFrame,我不会复制整个方法initialize(JFrame a)cos,因为它很长,而且我不认为错误来自那里。

   /******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: 这是Help_Option类:

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: 这是我用来测试Help_Option类的类:

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 这是因为您尚未为前几个JPanel设置值,并且它们返回的首选大小为0。尺寸为0,0

So you should add values to the JPanels there. 因此,您应该在此处向JPanels添加值。

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. 希望这会帮助您调整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. 您可能要为每个对象创建自定义的JPanels,以便可以轻松检查它们的大小是否正确。

public class VotingPanel extends JPanel {

    public VotingPanel(){

      //All your variables such as size and color schemes

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM