【问题标题】:Java draw stairs using drawRect and using inputField for more stairsJava 使用 drawRect 绘制楼梯并使用 inputField 绘制更多楼梯
【发布时间】:2013-10-01 08:07:49
【问题描述】:

我是一名 Java 初学者,希望您能帮助我。

我想从 JTextfield 中读取步数。但是如何让楼梯可变呢?

Graphics g = textP.getGraphics();

    int x = 5;
    int y = 20;
    int width = 10;
    int height = 10;

对于循环 1 - 同时给出 6 个 drawRect

    for (int i = 0; i < 6; i++) {
        g.drawRect(x, y * i, width, height);
    }       

对于循环 2 - 同时给出 5 个 drawRect

    for (int i = 1; i < 6; i++) {
        g.drawRect(x + 15, y * i, width, height);
    }   

对于循环 3 - 同时给出 4 个 drawRect

    for (int i = 2; i < 6; i++) {
        g.drawRect(x + 30, y * i, width, height);
    }   

对于循环 4 - 同时给出 3 个 drawRect

    for (int i = 3; i < 6; i++) {
        g.drawRect(x + 45, y * i, width, height);
    }   

对于循环 5 - 同时给出 2 个 drawRect

    for (int i = 4; i < 6; i++) {
        g.drawRect(x + 60, y * i, width, height);
    }   

对于循环 6 - 同时给出 1 个 drawRect

    for (int i = 5; i < 6; i++) {
        g.drawRect(x + 75, y * i, width, height);
    }   

输出(必须是 rect ipv *):

*
**
***
****
*****
******

【问题讨论】:

    标签: java for-loop drawrect


    【解决方案1】:

    你可以这样做。

    JTextField stairs = new JTextField("Enter no. of Stairs");
    String noOfStairsStr = stairs.getText();
    int noOfStairs = Integer.parseInt(noOfStairsStr);
    ...
    for (int i = 0; i < noOfStairs; i++) { // Use that in the for loop.
    

    【讨论】:

    • 是的,这就是我尝试的方法,但它会输出 1 行矩形。它必须由许多行矩形组成(请参阅我添加的输出)。
    【解决方案2】:

    你是否期待这样的事情

    for (int i = 0,k=15; i < 6; i++) {
                g.drawRect( ( x+(i*k) ), y * i, width, height);
    } 
    

    【讨论】:

    • 是的,这就是我尝试的方法,但它会输出 1 行矩形。它必须由许多行矩形组成(请参阅我添加的输出)。
    【解决方案3】:

    图形环境是复杂的东西。不再有固定字符宽度和高度的安全性,现在你需要开始处理更广泛的环境因素(比如你必须绘制的区域的宽度和高度......)

    要开始,我建议你看看

    涵盖基础知识。

    为了绘制步骤,我们需要知道(至少)三件事。

    1. 要绘制的步数...
    2. 每一步的宽度
    3. 每一步的高度。

    此示例将步骤的宽度和高度计算为它正在绘制的容器的宽度和高度的一个因素。

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class SimpleSteps {
    
        public static void main(String[] args) {
            new SimpleSteps();
        }
    
        public SimpleSteps() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JTextField steps;
            private StairPane stairPane;
    
            public TestPane() {
                setLayout(new BorderLayout());
                JPanel options = new JPanel();
                steps = new JTextField(10);
                JButton create = new JButton("Create");
                stairPane = new StairPane();
    
                options.add(new JLabel("Step count: "));
                options.add(steps);
                options.add(create);
    
                add(stairPane);
                add(options, BorderLayout.SOUTH);
    
                create.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String value = steps.getText();
                        try {
                            int stepCount = Integer.parseInt(value);
                            stairPane.setStepCount(stepCount);
                        } catch (NumberFormatException exp) {
                            JOptionPane.showMessageDialog(TestPane.this, value + " is not a valid step count", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                });
            }
    
        }
    
        public class StairPane extends JPanel {
    
            private int stepCount = 0;
    
            public StairPane() {
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (stepCount > 0) {
                    Graphics g2d = (Graphics2D) g.create();
                    int stepHeight = getHeight() / stepCount;
                    int stepWidth = getWidth() / stepCount;
                    for (int step = stepCount; step != 0; step--) {
                        int width = stepWidth * step;
                        int y = (stepHeight * step) - stepHeight;
                        g2d.fillRect(0, y, width, stepHeight);                       
                    }
                    g2d.dispose();
                }
            }
    
            private void setStepCount(int stepCount) {
                this.stepCount = stepCount;
                repaint();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      相关资源
      最近更新 更多