Java Code Examples for com.googlecode.lanterna.graphics.TextGraphics#drawTriangle()

The following examples show how to use com.googlecode.lanterna.graphics.TextGraphics#drawTriangle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TerminalTextGraphicsTest.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    Terminal terminal = new TestTerminalFactory(args).createTerminal();
    TextGraphics textGraphics = terminal.newTextGraphics();
    if((args.length > 0 && args[0].equals("--square")) ||
            (args.length > 1 && args[1].equals("--square"))) {
        textGraphics = new DoublePrintingTextGraphics(textGraphics);
    }
    textGraphics.setForegroundColor(TextColor.ANSI.BLUE);
    textGraphics.putString(3, 3, "Hello World!");
    textGraphics.setForegroundColor(TextColor.ANSI.CYAN);
    TerminalPosition lineStart = new TerminalPosition(3 + "Hello World!".length(), 3);
    textGraphics.drawLine(lineStart, lineStart.withRelativeColumn(2).withRelativeRow(6), Symbols.BLOCK_SOLID);
    textGraphics.setForegroundColor(TextColor.ANSI.RED);
    textGraphics.drawRectangle(lineStart.withRelativeColumn(2).withRelativeRow(6), new TerminalSize(5, 3), Symbols.BULLET);
    textGraphics.setForegroundColor(TextColor.ANSI.MAGENTA);
    TerminalPosition triangleStart = lineStart.withRelativeColumn(7).withRelativeRow(9);
    textGraphics.drawTriangle(
            triangleStart,
            triangleStart.withColumn(0).withRelativeRow(-1),
            triangleStart.withColumn(5).withRelativeRow(3),
            Symbols.SPADES);
    textGraphics.setForegroundColor(TextColor.ANSI.YELLOW);
    textGraphics.fillRectangle(new TerminalPosition(30, 1), new TerminalSize(8, 5), Symbols.DIAMOND);
    textGraphics.setForegroundColor(TextColor.ANSI.GREEN);
    triangleStart = new TerminalPosition(30, 6);
    textGraphics.fillTriangle(
            triangleStart,
            triangleStart.withRelativeRow(5).withRelativeColumn(-2),
            triangleStart.withRelativeRow(5).withRelativeColumn(4),
            Symbols.CLUB);

    terminal.resetColorAndSGR();
    terminal.flush();

    Thread.sleep(4000);

    if(terminal instanceof Window) {
        ((Window)terminal).dispose();
    }
}
 
Example 2
Source File: ScreenTriangleTest.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    boolean useAnsiColors = false;
    boolean useFilled = false;
    boolean slow = false;
    boolean rotating = false;
    boolean square = false;
    for(String arg : args) {
        if(arg.equals("--ansi-colors")) {
            useAnsiColors = true;
        }
        if(arg.equals("--filled")) {
            useFilled = true;
        }
        if(arg.equals("--slow")) {
            slow = true;
        }
        if(arg.equals("--rotating")) {
            rotating = true;
        }
        if(arg.equals("--square")) {
            square = true;
        }
    }
    Screen screen = new TestTerminalFactory(args).createScreen();
    screen.startScreen();

    TextGraphics graphics = new ScreenTextGraphics(screen);
    if(square) {
        graphics = new DoublePrintingTextGraphics(graphics);
    }
    Random random = new Random();

    TextColor color = null;
    double rad = 0.0;
    while(true) {
        KeyStroke keyStroke = screen.pollInput();
        if(keyStroke != null &&
                (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF)) {
            break;
        }
        screen.doResizeIfNecessary();
        TerminalSize size = graphics.getSize();
        if(useAnsiColors) {
            if(color == null || !rotating) {
                color = TextColor.ANSI.values()[random.nextInt(TextColor.ANSI.values().length)];
            }
        }
        else {
            if(color == null || !rotating) {
                //Draw a rectangle in random indexed color
                color = new TextColor.Indexed(random.nextInt(256));
            }
        }

        TerminalPosition p1;
        TerminalPosition p2;
        TerminalPosition p3;
        if(rotating) {
            screen.clear();
            double triangleSize = 15.0;
            int x0 = (size.getColumns() / 2) + (int) (Math.cos(rad) * triangleSize);
            int y0 = (size.getRows() / 2) + (int) (Math.sin(rad) * triangleSize);
            int x1 = (size.getColumns() / 2) + (int) (Math.cos(rad + oneThirdOf2PI) * triangleSize);
            int y1 = (size.getRows() / 2) + (int) (Math.sin(rad + oneThirdOf2PI) * triangleSize);
            int x2 = (size.getColumns() / 2) + (int) (Math.cos(rad + twoThirdsOf2PI) * triangleSize);
            int y2 = (size.getRows() / 2) + (int) (Math.sin(rad + twoThirdsOf2PI) * triangleSize);
            p1 = new TerminalPosition(x0, y0);
            p2 = new TerminalPosition(x1, y1);
            p3 = new TerminalPosition(x2, y2);
            rad += Math.PI / 90.0;
        }
        else {
            p1 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows()));
            p2 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows()));
            p3 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows()));
        }

        graphics.setBackgroundColor(color);
        if(useFilled) {
            graphics.fillTriangle(p1, p2, p3, ' ');
        }
        else {
            graphics.drawTriangle(p1, p2, p3, ' ');
        }
        screen.refresh(Screen.RefreshType.DELTA);
        if(slow) {
            Thread.sleep(500);
        }
    }
    screen.stopScreen();
}