I tried to draw a trapezium in Java. That worked out well. Now I'm trying to rotate my trapezium without printing the code in the method rotate(). I was hoping that I could do sth. like call the method rotate() in my testerclass and after calling method draw(), a rotated trapezium would be printed. All in all I'd like to have sth. like
-----*****-----
----*******----
---*********---
--***********--
--***********--
---*********---
----*******----
-----*****-----
My second thought was that I'd like to indent a single trapezium. I'd like to print the trapezium in my other draw(indentation) method.
--***********--
---*********---
----*******----
-----*****-----
Here is my code I figured out so far
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);
}
}
As you can see I'm struggling against the two methods rotate() and draw(...). Any advice is welcome. Thank you.
ok, one approach would be to create a buffer and print it then... a buffer would be a String[] where each String represents a line to be printed...
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.