Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java使用drawRect绘制楼梯,并使用inputField绘制更多楼梯_Java_For Loop_Drawrect - Fatal编程技术网

Java使用drawRect绘制楼梯,并使用inputField绘制更多楼梯

Java使用drawRect绘制楼梯,并使用inputField绘制更多楼梯,java,for-loop,drawrect,Java,For Loop,Drawrect,我是一个有Java经验的初学者,希望你能帮助我 for (int i = 0; i < 6; i++) { g.drawRect(x, y * i, width, height); } 我想从JTextfield中读取步骤数。但是如何使楼梯变为变量呢 Graphics g = textP.getGraphics(); int x = 5; int y = 20; int width = 10; int hei

我是一个有Java经验的初学者,希望你能帮助我

    for (int i = 0; i < 6; i++) {
        g.drawRect(x, y * i, width, height);
    }       
我想从JTextfield中读取步骤数。但是如何使楼梯变为变量呢

Graphics g = textP.getGraphics();

    int x = 5;
    int y = 20;
    int width = 10;
    int height = 10;
    for (int i = 0; i < 6; i++) {
        g.drawRect(x, y * i, width, height);
    }       
对于环1-同时给出6件的牵引

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

你可以这样做

    for (int i = 0; i < 6; i++) {
        g.drawRect(x, y * i, width, height);
    }       
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.
JTextField楼梯=新的JTextField(“输入楼梯编号”);
字符串noOfStairsStr=stairs.getText();
int noOfStairs=Integer.parseInt(noOfStairsStr);
...
for(inti=0;i
你期待这样的事情吗

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

图形环境是一件复杂的事情。您不再有固定字符宽度和高度的安全性,现在您需要开始处理更广泛的环境因素(例如必须绘制的区域的宽度和高度…)

    for (int i = 0; i < 6; i++) {
        g.drawRect(x, y * i, width, height);
    }       
首先,我建议你看看

    for (int i = 0; i < 6; i++) {
        g.drawRect(x, y * i, width, height);
    }       
涵盖基本知识

    for (int i = 0; i < 6; i++) {
        g.drawRect(x, y * i, width, height);
    }       
为了画台阶,我们需要知道(至少)三件事

    for (int i = 0; i < 6; i++) {
        g.drawRect(x, y * i, width, height);
    }       
  • 要绘制的步骤数
  • 每一步的宽度
  • 每一步的高度
  • 此示例计算台阶的宽度和高度,作为其所绘制容器的宽度和高度的一个因子

        for (int i = 0; i < 6; i++) {
            g.drawRect(x, y * i, width, height);
        }       
    
    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();
            }
        }
    }
    

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

    是的,这是我尝试的,但它将输出1行矩形。它必须按多行矩形(请参阅我添加的输出)。是的,这是我尝试的,但它将输出1行矩形。它必须按多行矩形(请参阅我添加的输出)。