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

时间:2013-10-01 08:07:49

标签: java for-loop drawrect

我是初学者的Java经验,所以我希望你能帮助我。

我想从JTextfield中读取步数。但你怎么能让楼梯变化呢?

Graphics g = textP.getGraphics();

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

对于循环1 - whil给出drawRect为6件

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

对于循环2 - whil给出5件的drawRect

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

对于循环3 - whil给出4件的drawRect

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

对于循环4 - whil给出3件的drawRect

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

对于循环5 - whil给出2件的drawRect

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

对于循环6 - whil给出1件的drawRect

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

输出(必须通过rect ipv *):

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

3 个答案:

答案 0 :(得分:0)

你可以这样做。

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 :(得分:0)

你期待这样的事吗

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

答案 2 :(得分:0)

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

要开始,我建议你看看

涵盖基础知识。

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

  1. 要绘制的步骤数......
  2. 每个步骤的宽度
  3. 每一步的高度。
  4. 此示例计算步骤的宽度和高度,作为其绘制的容器的宽度和高度的因子。

    enter image description here enter image description here enter image description here

    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();
            }
        }
    }