I tried to draw a trapezoid in Java. It worked out well. Now I'm trying to rotate my keystone without printing the code in the rotate () method. I was hoping I could do it. for example, call the rotate () method in my tester cluster and after calling the draw () method a rotated trapezoid will be printed. In general, I would like it to be. as
--***********--
--***********--
My second thought was that I would like to postpone one trapezoid. I would like to print a trapezoid in another drawing method (indentation).
Here is my code that I have found out so far
public class MyTrapezium {
private char backgroundChar;
private int bottomWidth;
private char foreground;
private int height;
private int margin = 5;
private int topWidth;
private int width = 30;
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;
}
public MyTrapezium(int topWidth, int bottomWidth, int margin) {
this.topWidth = topWidth;
this.bottomWidth = bottomWidth;
this.margin = margin;
}
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;
}
}
public void rotate() {
}
public void draw(int indentation) {
int i = 0;
while (i <= indentation) {
System.out.print(" ");
i++;
}
}
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) {
Trapezium t = new Trapezium(5, 11, '*', '-', 2);
t.draw();
t.rotate();
t.draw(15);
}
}
As you can see, I am afraid against the two methods rotate () and draw (...). Any advice is appreciated. Thank.
source
share