我刚接触Java,对绘制梯形有疑问。 目前,我不确定从哪里开始。 您有什么想法,我可以从哪里开始,要照顾什么? 我不想从一些灵感中获得完成的代码,因为此刻我真的不知道
/*
* Trapezium.java -
*/
/**
* Represents a isosceles trapezium, that can be 'drawn' to the standard
* output stream. A trapezium is drawn with a foreground and background colour.
* The fore- and background colours are represented by two (different) characters.
* Each trapezium consists of a number of individual lines. Each line contains an
* odd number of foreground characters to be able to retain symmetry.
*/
public class Trapezium {
// Attributes/ Fields
/**
* The character representation of the default background.
*/
private static final char DEFAULT_BACKGROUND = 32;
/**
* The character representation of the default foreground.
*/
private static final char DEFAULT_FOREGROUND = 42;
/**
* The character representation of the indentation.
*/
private static final char INDENTATION = 32;
/**
* The number of lines used to draw the triangle itself.
*/
private int height;
/**
* The maximal number of characters per line used to draw the
* trapezium itself.
*/
private int width;
/**
* The size of the margin in characters.
*/
private int margin;
/**
* The foreground character.
*/
private char foreground;
/**
* The background character.
*/
private char background;
/**
* The number of characters at the top line of
* the trapezium.
*/
private int topWidth;
/**
* The number of characters at the bottom line of the trapezium.
*/
private int bottomWidth;
// Constructor
/**
* Creates a trapezium accepting a top and bottom width,
* a fore- and a background character as well as a value
* for the margin.
*
* @param topWidth - the width at the top of the trapezium as a non-negative number.
* @param bottomWidth - the width at the bottom of the trapezium as a non-negative number.
* @param foreground - the character used to draw the trapezium's foreground
* @param background - the character used to draw the trapezium's background
* @param margin - the non-negative number of characters to be used as margin at the left
* and right of the trapezium
*
*/
public Trapezium(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.
* Default values are used for the foreground and background.
*
* @param topWidth - the width at the top of the trapezium as a non-negative odd number.
* @param bottomWidth - the width at the bottom of the trapezium as a non-negative odd number.
* @param margin - the non-negative number of characters to be used as margin at the left
* and right of the trapezium
*
*/
public Trapezium(int topWidth,
int bottomWidth,
int margin) {
this.topWidth = topWidth;
this.bottomWidth = bottomWidth;
this.margin = margin;
}
/**
* Creates a trapezium without a margin with the given width at the top and bottom.
* Default values are used for the foreground and background.
*
* @param topWidth - the width at the top of the trapezium as a non-negative odd number.
* @param bottomWidth - the width at the bottom of the trapezium as a non-negative odd number.
*/
public Trapezium(int topWidth,
int bottomWidth) {
this.topWidth = topWidth;
this.bottomWidth = bottomWidth;
}
// Methods
/**
* Draws the triangle with no indentation.
* This method calls the more general 'draw' method.
*/
public void draw() {
// while
}
/**
* Draws the triangle with the given indentation.
*
* @param indentation - the indentation at which the triangle is drawn
*
*/
public void draw(int indentation) {
;
//while
}
/**
* Rotates the triangle 180 degrees.
*
*/
public void rotate() {
;
}
/**
* Prints a 'run' of a given character.
*
* @param character - the character to be repeated.
* @param length - the number of characters to print.
*
*/
private void printCharacterRun(char character, int length) {
;
}
/**
* Adds a newline to the output.
*
*/
private void printNewLine() {
}
}
测试仪在另一个班级
trapezium = new Trapezium(5, 11, '*', '-', 2); // create a new Trapezium
trapezium.draw();
trapezium.rotate();
trapezium.draw(15);