How to create a pixel line in Java

I would like to draw a “pixelated” string in Java. Here is an example of what I mean by a "pixel" row:

Pixel line

This is my code that my code is trying to do. Suppose the line goes from (x1, y1)to (x2, y2)and that I want a 10 "block long line between them (I would call them pixels, but they will be displayed using a large number of pixels):

  • Create a rectangle with (x1, y1)both (0, 0)and with a width x2 - x1and height y2 - y1.
  • Calculate the hypotenuse of the triangle formed by this rectangle. Name it cOriginal.
  • Scan the rectangle so that the length of the hypotenuse is 10 longer.
  • 2D- . grid.
  • Bresenham Line , (0, 0), , true. , .
  • , . 2 , "" . :

    float widthScalingFactor = (float) widthOriginal/(float) newWidth; float heightScalingFactor = (float) heightOriginal/(float) newHeight;

( , ...) grid, grid[i][j] (is true), 10 (x1 + (i * widthScalingFactor), yOffset + (j * heightScalingFactor)).

:

public static void drawPixelatedLine(Graphics g, int x1, int y1, int x2, int y2) {

    int widthOriginal = x2 - x1;
    int heightOriginal = y2 - y1;

    if(widthOriginal <= 0 && heightOriginal <= 0){

        int temp = x2;
        x2 = x1;
        x1 = temp;

        temp = y2;
        y2 = y1;
        y1 = temp;

    }

    double cOriginal = Math.sqrt(widthOriginal * widthOriginal + heightOriginal * heightOriginal);

    double shrinkingFactor = 10d / cOriginal;

    int newWidth = (int) Math.round(shrinkingFactor * widthOriginal);
    int newHeight = (int) Math.round(shrinkingFactor * heightOriginal);

    newWidth = (newWidth <= 0 ? 1 : newWidth);
    newHeight = (newHeight <= 0 ? 1 : newHeight);

    boolean[][] grid = new boolean[newWidth][newHeight];

    int rescaledX1 = 0;
    int rescaledY1 = 0;

    int rescaledX2 = newWidth - 1;
    int rescaledY2 = newHeight - 1;

    int x = rescaledX1;
    int y = rescaledY1;

    int dx = Math.abs(rescaledX2 - rescaledX1);
    int dy = Math.abs(rescaledY2 - rescaledY1);

    int s1 = Utils.sign(rescaledX2 - rescaledX1);
    int s2 = Utils.sign(rescaledY2 - rescaledY1);

    boolean swap = false;

    if (dy > dx) {

        int temp = dx;
        dx = dy;
        dy = temp;

        swap = true;

    }

    int D = 2 * dy - dx;

    for (int i = 0; i < dx; i += 1) {

        grid[x][y] = true;

        while (D >= 0) {

            D = D - 2 * dx;

            if (swap) {

                x += s1;

            }

            else {

                y += s2;

            }

        }

        D = D + 2 * dy;

        if (swap) {

            y += s2;

        }

        else {

            x += s1;

        }

    }

    int xOffset = x1;
    int yOffset = y1;

    float widthScalingFactor = (float) widthOriginal / (float) newWidth;
    float heightScalingFactor = (float) heightOriginal / (float) newHeight;

    for(int i = 0; i < grid.length; i++){

        for(int j = 0; j < grid[0].length; j++){

            if(grid[i][j]){

                g.fillRect((int) (xOffset + (i * widthScalingFactor)), (int) (yOffset + (j * heightScalingFactor)), 10, 10);

            }

        }

    }

, - -, , , (, , , 10 10 , , , ~ 140 . , - -, . .

, - ( /). Example 1

, -, , , , . -, , y. Example 2

, , -, 2 , -. , , .

, "" ?

+4
1

, , (x, y).

, ( ) .

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class BresenhamBlocky {

    static class TestPanel extends JPanel {
        public TestPanel() {
            setPreferredSize(new Dimension(800, 800));
        }

        @Override
        protected void paintComponent(final Graphics g) {
            super.paintComponent(g);
            int w = getWidth();
            int h = getHeight();
            g.setColor(Color.GRAY);
            g.fillRect(0, 0, w, h);
            g.setColor(Color.BLUE);
            drawLine(g, w >> 1, h >> 1, targetX, targetY, 10);
        }
    }

    public static void main(String[] argv) {
        SwingUtilities.invokeLater(() -> { showTest(); });
    }

    static int targetX, targetY;

    static void showTest() {
        JFrame frame = new JFrame("Test");
        JComponent test = new TestPanel();
        test.setFocusable(true);
        test.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                targetX = e.getX();
                targetY = e.getY();
                e.getComponent().repaint();
            }
        });
        frame.setLayout(new BorderLayout());
        frame.add(test, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void drawLine(Graphics g, int x0, int y0, int x1, int y1, int blockSize) {
        int scaledX0 = x0 / blockSize;
        int scaledY0 = y0 / blockSize;
        int scaledX1 = x1 / blockSize;
        int scaledY1 = y1 / blockSize;
        int dx = scaledX1 - scaledX0;
        int dy = scaledY1 - scaledY0;
        int stepX = Integer.signum(dx);
        int stepY = Integer.signum(dy);
        dx = Math.abs(dx);
        dy = Math.abs(dy);
        int dx2 = dx << 1;
        int dy2 = dy << 1;
        int x = scaledX0;
        int y = scaledY0;
        int error;
        if (dx >= dy) {
            error = dy2 - dx;
            do {
                plot(g, x, y, blockSize);
                if (error > 0) {
                    y += stepY;
                    error -= dx2;
                }
                error += dy2;
                x += stepX;
            } while (x != scaledX1);
        } else {
            error = dx2 - dy;
            do {
                plot(g, x, y, blockSize);
                if (error > 0) {
                    x += stepX;
                    error -= dy2;
                }
                error += dx2;
                y += stepY;
            } while (y != scaledY1);
        }
    }

    static void plot(Graphics g, int x, int y, int blockSize) {
        int x0 = x * blockSize;
        int y0 = y * blockSize;
        int w = blockSize;
        int h = blockSize;
        g.fillRect(x0, y0, w, h);
    }

}

( , )

+3

Source: https://habr.com/ru/post/1678723/


All Articles