如何在Java中旋转、缩进和绘制三角形以打印梯形?

4

我尝试在Java中绘制梯形,结果很成功。现在我想旋转我的梯形,但不想在方法rotate()中打印代码。我希望我可以在测试类中调用方法rotate(),并在调用方法draw()后打印一个旋转的梯形。总之,我想要像这样的东西:

-----*****-----
----*******----
---*********---
--***********--
--***********--
---*********---
----*******----
-----*****-----

我的第二个想法是我想缩进单个梯形。我想在我的另一个绘制(缩进)方法中打印这个梯形。

     --***********--
     ---*********---
     ----*******----
     -----*****-----

这是我目前想出的代码:

public class MyTrapezium { 
    // background character
    private char backgroundChar;

    // number of characters at the bottom line of the trapezium.
    private int bottomWidth;

    // foreground character.
    private char foreground;

    // number of lines used to draw the trapezium
    private int height;

    // the size of the margin in chars.
    private int margin = 5;

    // number of characters at the top line of the trapezium
    private int topWidth;

    // maximal number of characters per line used to draw the trapezium
    private int width = 30;



    // Creates a trapezium without a margin with the given width at the top and bottom.     
    public Trapezium(int topWidth, int bottomWidth) {
        this.topWidth = topWidth;
        this.bottomWidth = bottomWidth;
    }


    public MyTrapezium(int topWidth, int bottomWidth, char foreground, char background, int margin) {
        this.topWidth = topWidth;
        this.bottomWidth = bottomWidth;
        this.foreground = foreground;
        this.background = background;
        this.margin = margin;
    }

    // Creates a trapezium with the given width at the top and bottom and the size of the margin.

    public MyTrapezium(int topWidth, int bottomWidth, int margin) {
        this.topWidth = topWidth;
        this.bottomWidth = bottomWidth;
        this.margin = margin;
    }


    //Methods
    // Draws the trapezium with no indentation.
    public void draw() {
        int tw = topWidth;
        int bw = bottomWidth;
        height = bottomWidth - topWidth;

        while (tw <= bw) {
            for (int i = 0; i < margin; i++) {
                System.out.print(background);
            }
            printChar(background, height / 2);
            printChar(foreground, tw);
            printChar(background, height / 2);
            for (int i = 0; i < margin; i++) {
                System.out.print(background);
            }
            height -= 2;
            tw += 2;
        }
    }

    // Rotates the triangle 180 degrees without printing it
    public void rotate() {

    }

    // Draws the triangle with a given indentation.
    public void draw(int indentation) {
        int i = 0;
        while (i <= indentation) {
            System.out.print(" ");
            i++;
        }
    }

    // Prints a 'run' of a given character.
    private void printChar(char character, int length) {
        for (int i = 0; i < length; i++) {
            System.out.print(character);
        }
    }    

}



public class MyTester {
   public static void main(String[] args) {
        /** Creates and draws a new trapezium with given values.
        * The trapezium is rotated and should be drawn with indentation 15.
        */
        Trapezium t = new Trapezium(5, 11, '*', '-', 2);

        t.draw();
        t.rotate();
        t.draw(15);

    }
}

如您所见,我在应用rotate()和draw(...)这两种方法时遇到了困难。欢迎提供任何建议。谢谢。


我对三角形、梯形和 myTrapezium 的使用有点困惑... 你可否请清理一下你的代码/问题? - Martin Frank
@MartinFrank - 我编辑了我的帖子,希望现在更容易理解了。 - user1437032
你只是旋转了180度,对吧? - Martin Frank
是的,所以我想,我只需要以某种方式切换参数。我的意思是,我可以在方法中打印它,但对我来说,这似乎不够简洁。 - user1437032
1个回答

1

好的,一种方法是创建一个缓冲区并打印它,缓冲区可以是String[],其中每个String代表要打印的一行...

private String[] buffer;

private void draw(){ //please rename 'draw' to 'print' this is confusiong else
    for (String line: buffer){
        System.out.println(line);
    }
}

public void create(int topWidth, int bottomWidth, char foreground, char background, int margin){
    //as done in your code
}

public void rotate(){
    String[] newBuffer = new String[buffer.length()];
    for(int i = 0; i < buffer.length(); i ++){
        newBuffer[buffer.length-1-i] = buffer[i];
    }
    buffer = newBuffer;
}

public void draw(int indent){
    String indentString = createIndent(indent);
    for(String line: buffer){
         System.out.println(indentString + line);
    }
}

private String createIndent(int indent){
    String str = "";
    for(int i = 0; i < indent; i ++){
       str = str + " ";
    }
    return str;
}

嗨,感谢您的帖子。我仍在学习Java,似乎还有很长的路要走 :-) - user1437032

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接