Ardoise.java 3.89 KB
package tp2.ArdoiseMagique;

import javax.swing.*;
import java.awt.*;

public class Ardoise extends JPanel {
        private final int defaultWidth = 1000;
        private final int defaultHeight = 600;
        protected static final int BLACK = 1;
        protected static final int WHITE = 0;

        private int oldx = -1;
        private int oldy = -1;

        private final int[][] pixelArray = new int[defaultWidth][defaultHeight];

        public Ardoise() {
            super();
            this.setSize(defaultWidth, defaultHeight);

            wipe();
        }

        public void wipe() {
            for (int i = 0; i < defaultWidth; ++i){
                for (int j = 0; j < defaultHeight; ++j){
                    pixelArray[i][j] = WHITE;
                }
            }

            repaint();
        }

        public void setPixel(int x, int y, int color){
            if (x < 0 || y < 0 || x >= defaultWidth || y >= defaultHeight){
                return;
            }

            if (oldx == -1 || oldy == -1){
                oldx = x;
                oldy = y;
            }

            plotLine(oldx, oldy, x, y);

            oldx = x;
            oldy = y;

            pixelArray[x][y] = color;
            repaint();
        }

        public void plotLineLow(int x0, int y0, int x1, int y1){
            int dx = x1 - x0;
            int dy = y1 - y0;
            int yi = 1;
            if (dy < 0) {
                yi = -1;
                dy = -dy;
            }
            int D = (2 * dy) - dx;
            int ay = y0;

            for (int ax = x0; ax < x1; ++ax) {
                pixelArray[ax][ay] = BLACK;
                if (D > 0) {
                    ay = ay + yi;
                    D = D + (2 * (dy - dx));
                } else D = D + 2 * dy;
            }
        }

        public void plotLineHigh(int x0, int y0, int x1, int y1){
            int dx = x1 - x0;
            int dy = y1 - y0;
            int xi = 1;
            if (dx < 0) {
                xi = -1;
                dx = -dx;
            }
            int D = (2 * dx) - dy;
            int ax = x0;

            for (int ay = y0; ay < y1; ++ay) {
                pixelArray[ax][ay] = BLACK;
                if (D > 0) {
                    ax = ax + xi;
                    D = D + (2 * (dx - dy));
                } else D = D + 2 * dx;
            }
        }

        public void plotLine(int x0, int y0, int x1, int y1) {
            if (Math.abs(y1 - y0) < Math.abs(x1 - x0)) {
                if (x0 > x1) {
                    plotLineLow(x1, y1, x0, y0);
                } else {
                    plotLineLow(x0, y0, x1, y1);
                }
            } else {
                if (y0 > y1) {
                    plotLineHigh(x1, y1, x0, y0);
                } else {
                    plotLineHigh(x0, y0, x1, y1);
                }
            }
        }

        public void resetOld() {
            oldx = -1;
            oldy = -1;
        }

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);

            g.setColor(Color.WHITE);
            for (int i = 0; i < defaultWidth; ++i){
                for (int j = 0; j < defaultHeight; ++j){
                    /*
                        since most of the image will be white and few pixels will be black,
                        we set the color to white and when we encounter a black pixel,
                        we temporarily change the color to black, paint the pixel and switch
                        it back to white.
                    */
                    if (pixelArray[i][j] == BLACK){
                        g.setColor(Color.BLACK);
                        g.drawLine(i, j, i, j);
                        g.setColor(Color.WHITE);
                    } else if (pixelArray[i][j] == WHITE){
                        g.drawLine(i, j, i, j);
                    }
                }
            }
        }
    }