【问题标题】:Java graphics JAppletJava 图形 JApplet
【发布时间】:2012-04-03 06:48:30
【问题描述】:

我目前正在开发一个针对学校的计划。我们必须制作一个Applet,我选择了JApplet。出于某种原因,我用来显示特定字符​​串的面板将不会显示。这里可能是什么问题?请指出我正确的方向。另外,我看了一些教程,有些人建议我有“开始”、“停止”和“破坏”方法,有些没有。这些方法是否对我的 JPanel 不显示图形有影响?

谢谢

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Shape extends JApplet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    // making the radiobuttons for the shape choices 
    JRadioButton squareButton = new JRadioButton("Square",true);
    JRadioButton ovalButton = new JRadioButton("Oval",false);
    JRadioButton rectangleButton = new JRadioButton("Rectangle",false);
    JRadioButton triangleButton = new JRadioButton("Triangle",false);

    // making radiobuttons for the color choices
    JRadioButton redButton = new JRadioButton("Red",true);
    JRadioButton blueButton = new JRadioButton("Blue",false);
    JRadioButton greenButton = new JRadioButton("Green",false);
    JRadioButton yellowButton = new JRadioButton("Yellow",false);

    // making buttons draw and animate
    JButton drawButton = new JButton("Draw!");
    JButton animateButton = new JButton("Animate!");

    // making JTextFields for length and width
    JTextField lengthField = new JTextField("Enter a length",15);
    JTextField widthField = new JTextField("Enter a width",15);

    // making JPanel, in which the radiobuttons will go01
    JPanel shapePanel = new JPanel();
    JPanel colorPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JPanel textPanel = new JPanel();
    drawPanel dPanel;

    ButtonGroup shapeGroup = new ButtonGroup();
    ButtonGroup colorGroup = new ButtonGroup();

    // variables that will dictates the shape, size and color
    int length = 200;
    int width = 200;
    Color color = Color.RED;
    String shape = "square";

    public void init() {
        setLayout(new FlowLayout()); // setting layout for the applet
        setSize(680,480);

        // setting the layout for the shapePanel - gridlayout 2 rows, 2 cols and space of 5
        shapePanel.setLayout(new GridLayout(2,2,5,5));

        // adding a border to the shapePanel to indicate to the user what it does "titledBorder"
        shapePanel.setBorder(BorderFactory.createTitledBorder("Choose a shape"));

        // setting layout for the color panel - gridlayout 2 rows, 2 cols and space of 5
        colorPanel.setLayout(new GridLayout(2,2,5,5));

        // adding a border to the  colorPanel to indicate to the user what it does "titledBorder"
        colorPanel.setBorder(BorderFactory.createTitledBorder("Choose a color"));

        // setting the layout for the buttonPanel - gridlayout 1 row, 2 cols and space of 5
        buttonPanel.setLayout(new GridLayout(1,2,5,5));

        // adding a color border
        buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red, 2));

        // setting the layout of the textField - gridlayout 1 row, 2 cols and space of 5
        textPanel.setLayout(new GridLayout(1,2,5,5));

        // adding some attributes for lengthField and widthField
        lengthField.setFont(new Font("Arial",Font.PLAIN,12));
        lengthField.setForeground(new Color(150,150,150));

        widthField.setFont(new Font("Arial",Font.PLAIN,12));
        widthField.setForeground(new Color(150,150,150));

        // using shapegroup to organize the JRadioButtons
        shapeGroup.add(squareButton);
        shapeGroup.add(ovalButton);
        shapeGroup.add(rectangleButton);
        shapeGroup.add(triangleButton);

        // using colorgroup to organize the color radiobuttons
        colorGroup.add(redButton);
        colorGroup.add(blueButton);
        colorGroup.add(greenButton);
        colorGroup.add(yellowButton);

        // add the shape buttons to the panel so they appear in a square form 
        shapePanel.add(squareButton);
        shapePanel.add(ovalButton);
        shapePanel.add(rectangleButton);
        shapePanel.add(triangleButton);

        // adding color buttons to the color panel
        colorPanel.add(redButton);
        colorPanel.add(blueButton);
        colorPanel.add(greenButton);
        colorPanel.add(yellowButton);

        // adding jbuttons
        buttonPanel.add(drawButton);
        buttonPanel.add(animateButton);

        // adding textfields to the textPanel
        textPanel.add(lengthField);
        textPanel.add(widthField);

        dPanel  = new drawPanel();

        // adding panels to the applet
        add(shapePanel);
        add(colorPanel);
        add(buttonPanel);
        add(textPanel);
        add(dPanel);

        // adding focus listener to lengthField and widthField
        lengthField.addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent e) {
                lengthField.setText("");
                lengthField.setForeground(Color.black);
            }

            public void focusLost(FocusEvent e) {}

        });

        widthField.addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent e) {
                widthField.setText("");
                widthField.setForeground(Color.black);
            }

            public void focusLost(FocusEvent e) {}

        });

        drawButton.addActionListener(new drawListener());

    }

    // when the person presses paint, this will be executed to paint the specific shape, color with the width and length
    class drawListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            int mylength = 5; 
            int mywidth = 5;

            try {
                mylength = Integer.parseInt(lengthField.getText());;
                mywidth = Integer.parseInt(widthField.getText());;
            }catch(Exception ex) {
                JOptionPane.showMessageDialog(null,""+ex,"",JOptionPane.ERROR_MESSAGE);
            }

            if((mylength > 200 || mylength < 5)) {
                JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200", 
                        "Invalid length message", JOptionPane.ERROR_MESSAGE);
            }else if((mywidth > 200 || mywidth < 5)) {
                JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200", 
                        "Invalid width message", JOptionPane.ERROR_MESSAGE);
            }else {
                length = mylength;
                width = mywidth;

                // checking which color button is selected
                if(redButton.isSelected()) {
                    color = Color.RED;
                }else if(blueButton.isSelected()) {
                    color = Color.BLUE;
                }else if(greenButton.isSelected()) {
                    color = Color.GREEN;
                }else if(yellowButton.isSelected()) {
                    color = Color.YELLOW;
                }

                // checking which shape has been selected
                if(rectangleButton.isSelected()) {
                    shape = "rectangle";
                }else if(triangleButton.isSelected()) {
                    shape = "triangle";
                }else if(ovalButton.isSelected()) {
                    shape = "oval";
                }else if(squareButton.isSelected()) {
                    shape = "square";
                }

                //System.out.printf("%3d %3d %s %s \n",length,width,shape,color);

            }

        }
    }

    // This will be used to do the painting
    class drawPanel extends JPanel {
        private static final long serialVersionUID = 1L;

        //Paint Method
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.black);

            g2.drawString("My awesome string", 200, 200);
        }

    }

}

【问题讨论】:

    标签: java swing graphics japplet


    【解决方案1】:

    没有组件的JPanel 的默认大小为 0x0。试试这个来源:

    // <applet code=Shape width=640 height=480></applet>
    import java.awt.*;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Shape extends JApplet {
    
        /**
         *
         */
        private static final long serialVersionUID = 1L;
    
        // making the radiobuttons for the shape choices
        JRadioButton squareButton = new JRadioButton("Square",true);
        JRadioButton ovalButton = new JRadioButton("Oval",false);
        JRadioButton rectangleButton = new JRadioButton("Rectangle",false);
        JRadioButton triangleButton = new JRadioButton("Triangle",false);
    
        // making radiobuttons for the color choices
        JRadioButton redButton = new JRadioButton("Red",true);
        JRadioButton blueButton = new JRadioButton("Blue",false);
        JRadioButton greenButton = new JRadioButton("Green",false);
        JRadioButton yellowButton = new JRadioButton("Yellow",false);
    
        // making buttons draw and animate
        JButton drawButton = new JButton("Draw!");
        JButton animateButton = new JButton("Animate!");
    
        // making JTextFields for length and width
        JTextField lengthField = new JTextField("Enter a length",15);
        JTextField widthField = new JTextField("Enter a width",15);
    
        // making JPanel, in which the radiobuttons will go01
        JPanel shapePanel = new JPanel();
        JPanel colorPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
        JPanel textPanel = new JPanel();
        drawPanel dPanel;
    
        ButtonGroup shapeGroup = new ButtonGroup();
        ButtonGroup colorGroup = new ButtonGroup();
    
        // variables that will dictates the shape, size and color
        int length = 200;
        int width = 200;
        Color color = Color.RED;
        String shape = "square";
    
        public void init() {
            setLayout(new FlowLayout()); // setting layout for the applet
            // This is set by HTML!
            //setSize(680,480);
    
            // setting the layout for the shapePanel - gridlayout 2 rows, 2 cols and space of 5
            shapePanel.setLayout(new GridLayout(2,2,5,5));
    
            // adding a border to the shapePanel to indicate to the user what it does "titledBorder"
            shapePanel.setBorder(BorderFactory.createTitledBorder("Choose a shape"));
    
            // setting layout for the color panel - gridlayout 2 rows, 2 cols and space of 5
            colorPanel.setLayout(new GridLayout(2,2,5,5));
    
            // adding a border to the  colorPanel to indicate to the user what it does "titledBorder"
            colorPanel.setBorder(BorderFactory.createTitledBorder("Choose a color"));
    
            // setting the layout for the buttonPanel - gridlayout 1 row, 2 cols and space of 5
            buttonPanel.setLayout(new GridLayout(1,2,5,5));
    
            // adding a color border
            buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red, 2));
    
            // setting the layout of the textField - gridlayout 1 row, 2 cols and space of 5
            textPanel.setLayout(new GridLayout(1,2,5,5));
    
            // adding some attributes for lengthField and widthField
            lengthField.setFont(new Font("Arial",Font.PLAIN,12));
            lengthField.setForeground(new Color(150,150,150));
    
            widthField.setFont(new Font("Arial",Font.PLAIN,12));
            widthField.setForeground(new Color(150,150,150));
    
            // using shapegroup to organize the JRadioButtons
            shapeGroup.add(squareButton);
            shapeGroup.add(ovalButton);
            shapeGroup.add(rectangleButton);
            shapeGroup.add(triangleButton);
    
            // using colorgroup to organize the color radiobuttons
            colorGroup.add(redButton);
            colorGroup.add(blueButton);
            colorGroup.add(greenButton);
            colorGroup.add(yellowButton);
    
            // add the shape buttons to the panel so they appear in a square form
            shapePanel.add(squareButton);
            shapePanel.add(ovalButton);
            shapePanel.add(rectangleButton);
            shapePanel.add(triangleButton);
    
            // adding color buttons to the color panel
            colorPanel.add(redButton);
            colorPanel.add(blueButton);
            colorPanel.add(greenButton);
            colorPanel.add(yellowButton);
    
            // adding jbuttons
            buttonPanel.add(drawButton);
            buttonPanel.add(animateButton);
    
            // adding textfields to the textPanel
            textPanel.add(lengthField);
            textPanel.add(widthField);
    
            dPanel  = new drawPanel();
            dPanel.setPreferredSize(new Dimension(500,300));
    
            // adding panels to the applet
            add(shapePanel);
            add(colorPanel);
            add(buttonPanel);
            add(textPanel);
            add(dPanel);
    
            // adding focus listener to lengthField and widthField
            lengthField.addFocusListener(new FocusListener() {
    
                public void focusGained(FocusEvent e) {
                    lengthField.setText("");
                    lengthField.setForeground(Color.black);
                }
    
                public void focusLost(FocusEvent e) {}
    
            });
    
            widthField.addFocusListener(new FocusListener() {
    
                public void focusGained(FocusEvent e) {
                    widthField.setText("");
                    widthField.setForeground(Color.black);
                }
    
                public void focusLost(FocusEvent e) {}
    
            });
    
            drawButton.addActionListener(new drawListener());
    
        }
    
        // when the person presses paint, this will be executed to paint the specific shape, color with the width and length
        class drawListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
    
                int mylength = 5;
                int mywidth = 5;
    
                try {
                    mylength = Integer.parseInt(lengthField.getText());;
                    mywidth = Integer.parseInt(widthField.getText());;
                }catch(Exception ex) {
                    JOptionPane.showMessageDialog(null,""+ex,"",JOptionPane.ERROR_MESSAGE);
                }
    
                if((mylength > 200 || mylength < 5)) {
                    JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200",
                            "Invalid length message", JOptionPane.ERROR_MESSAGE);
                }else if((mywidth > 200 || mywidth < 5)) {
                    JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200",
                            "Invalid width message", JOptionPane.ERROR_MESSAGE);
                }else {
                    length = mylength;
                    width = mywidth;
    
                    // checking which color button is selected
                    if(redButton.isSelected()) {
                        color = Color.RED;
                    }else if(blueButton.isSelected()) {
                        color = Color.BLUE;
                    }else if(greenButton.isSelected()) {
                        color = Color.GREEN;
                    }else if(yellowButton.isSelected()) {
                        color = Color.YELLOW;
                    }
    
                    // checking which shape has been selected
                    if(rectangleButton.isSelected()) {
                        shape = "rectangle";
                    }else if(triangleButton.isSelected()) {
                        shape = "triangle";
                    }else if(ovalButton.isSelected()) {
                        shape = "oval";
                    }else if(squareButton.isSelected()) {
                        shape = "square";
                    }
    
                    //System.out.printf("%3d %3d %s %s \n",length,width,shape,color);
    
                }
    
            }
        }
    
        // This will be used to do the painting
        class drawPanel extends JPanel {
            private static final long serialVersionUID = 1L;
    
            //Paint Method
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.setColor(Color.black);
    
                g2.drawString("My awesome string", 200, 200);
            }
    
        }
    
    }
    

    【讨论】:

    • 你先生,是救生员。非常感谢。我想知道您是否可以删除此代码,因为这是一个项目,我不想让别人复制它。再次感谢您。
    • “我想知道您是否可以删除此代码,” 我可以,但不会。这不是 SO 的工作方式。如果您需要私人解决方案,请聘请私人导师。
    • @user681159:所以您实际上是在说您可以借用此代码,但其他人不能这样做?那是怎么回事?你比其他可以从中受益的人更好或更重要吗?或者是您想为您的作业提交此代码的原因,就像您编写它一样。不管怎样,这很臭。
    • @user681159:我已经回滚了您的问题,以便显示您的原始代码。让我们为您的教练保持光明正大。
    • “你可以继续下去。” 哦,谢谢你好心的先生。“说得好听点就行了,不用“大喊大叫” “。” 谁在大喊大叫? “天哪。” O_o.如果您打算从 SO 中获得最好的结果,您可能想要处理“过度发展的自我权利意识”。但是,嘿,你的电话。无论哪种方式,我的鼻子都没有皮肤..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    相关资源
    最近更新 更多