0

我是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 *):

*
**
***
****
*****
******
4

3 回答 3

0

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

要开始,我建议你看看

涵盖基础知识。

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

  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();
        }
    }
}
于 2013-10-01T09:57:17.650 回答
0

你期待这样的事情吗

for (int i = 0,k=15; i < 6; i++) {
            g.drawRect( ( x+(i*k) ), y * i, width, height);
} 
于 2013-10-01T08:11:43.123 回答
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.
于 2013-10-01T08:10:57.123 回答