Package painting

Source Code of painting.DrawingPane

package painting;

import algorithms.core.Algorithm;
import algorithms.factories.AlgorithmFactory;
import algorithms.factories.Factories;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import static algorithms.factories.Factories.AlgorithmName;
import static algorithms.factories.Factories.AlgorithmName.DEJIKSTRY_ALG;
import static algorithms.factories.Factories.AlgorithmName.WAVE_ALG;



public class DrawingPane extends JPanel {

    private List<List<ColorSquare>> colorSquareArr;
    private int panelWidth;
    private int panelHeight;
    private int pointNum = 0;
    private final int START_X = 0;
    private final int START_Y = 0;
    private Color color = Color.RED;
    private int squareWidth = 10;
    private int squareHeight = 10;
    private int squaresNumX;
    private int squaresNumY;
    private List<Point> pathCoordArr;
    private AlgorithmName currAlgorithm = WAVE_ALG;


    public DrawingPane(int initSquaresNumX, int initSquaresNumY) {
      colorSquareArr = new ArrayList<List<ColorSquare>>();
        squaresNumX = initSquaresNumX;
        squaresNumY = initSquaresNumY;
        panelWidth = squareWidth * squaresNumX;
        panelHeight = squareHeight * squaresNumY;

        setBorder(BorderFactory.createLineBorder(Color.black));

        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                revalidate();
                if (pathCoordArr != null) {
                    if (!pathCoordArr.isEmpty()) {
                        Color colorTmp = color;
                        makeEmptyColorCurrent();
                        for (Point point : pathCoordArr) {
                            repaintSquareByItsCoord(point.x, point.y);
                        }
                        color = colorTmp;
                        pathCoordArr.clear();
                    }
                }   
                repaintSquare(e.getX(),e.getY());
            }
        });

        addMouseMotionListener(new MouseAdapter(){
            public void mouseDragged(MouseEvent e){
                revalidate();
                repaintSquare(e.getX(),e.getY());
            }
        });

        paintEmptySquares();
    }

    private void repaintSquare(int x, int y){
        if ((x >= panelWidth) || (y >= panelHeight) ||
                (x < START_X) || (y < START_Y)) {
            return;
        }

        int x1 = (x / squareWidth) * squareWidth;
        int y1 = (y / squareHeight) * squareHeight;
        final int OFFSET = 1;

        // Repaint the square at the new location.
        if (!isPointsLimitExceeded() ||
                (color != Color.GREEN))
        {
            if ((color == Color.GREEN) &&
                    (colorSquareArr.get(y1 / squareHeight).get(x1 / squareWidth).getColor()!= Color.GREEN)) {
                pointNum++;
            }
            else if ((color != Color.GREEN) &&
                (colorSquareArr.get(y1 / squareHeight).get(x1 / squareWidth).getColor() == Color.GREEN)) {
              pointNum--;
            }
            colorSquareArr.get(y1 / squareHeight).get(x1 / squareWidth).setColor(color);
        repaint(colorSquareArr.get(y1 / squareHeight).get(x1 / squareWidth).getXPos(),
            colorSquareArr.get(y1 / squareHeight).get(x1 / squareWidth).getYPos(),
            colorSquareArr.get(y1 / squareHeight).get(x1 / squareWidth).getWidth() + OFFSET,
            colorSquareArr.get(y1 / squareHeight).get(x1 / squareWidth).getHeight() + OFFSET);
        }
    }

    private void repaintSquareByItsCoord(int x, int y){
        final int OFFSET = 1;

        // Repaint the square at the new location.

            colorSquareArr.get(y).get(x).setColor(color);
        repaint(colorSquareArr.get(y).get(x).getXPos(),
            colorSquareArr.get(y).get(x).getYPos(),
            colorSquareArr.get(y).get(x).getWidth() + OFFSET,
            colorSquareArr.get(y).get(x).getHeight() + OFFSET);
    }


    private void paintEmptySquares(){
      pointNum = 0;
        colorSquareArr.clear();
        for (int i = 0; i < squaresNumY; i++) {
            List<ColorSquare> colorSquareArrX = new ArrayList<ColorSquare>();
            for (int j = 0; j < squaresNumX; j++) {
                ColorSquare colorSquare = new ColorSquare();
                colorSquare.setXPos(j * squareWidth);
                colorSquare.setYPos(i * squareHeight);
                colorSquare.setXCoord(j);
                colorSquare.setYCoord(i);
                colorSquareArrX.add(colorSquare);
            }
            colorSquareArr.add(colorSquareArrX);
        }

        super.setPreferredSize(new Dimension(panelWidth, panelHeight));
    }

    public void setPaneSquaresNumX(int PaneSquaresNumX) {
        squaresNumX = PaneSquaresNumX;
        panelWidth = squareWidth * squaresNumX;
        paintEmptySquares();
        repaint();
    }

    public void setPaneSquaresNumY(int PaneSquaresNumY) {
       squaresNumY = PaneSquaresNumY;
       panelHeight = squareHeight * squaresNumY;
       paintEmptySquares();
       repaint();
    }

    public void makeBlockColorCurrent() {
        color = Color.RED;
    }

    public void makeEmptyColorCurrent() {
        color = Color.GRAY;
    }

    public void makePointColorCurrent() {
        color = Color.GREEN;
    }

    public boolean isPointsLimitExceeded() {
        final int POINT_SQUARES_LIMIT = 2;

        return (POINT_SQUARES_LIMIT <= pointNum) ? true : false;
    }
   
    public void pathDrawing() {
      pathCalculation();
        Color colorTmp = color;
        color = Color.BLUE;
        for (Point point : pathCoordArr)
        {
            repaintSquareByItsCoord((int)point.getX(), (int)point.getY());
        }
        color = colorTmp;
    }
   
    public void pathCalculation() {
        AlgorithmFactory algorithmFactory =
                Factories.createFactory(currAlgorithm);
        Algorithm algorithm =
                algorithmFactory.getAlgorithm(colorSquareArr);
        algorithm.calculate();
        pathCoordArr = algorithm.getPath();
    }

    public void makeWaveAlgCurrent() {
        currAlgorithm = WAVE_ALG;
    }

    public void makeDeikstryAlgCurrent() {
        currAlgorithm = DEJIKSTRY_ALG;
    }

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

        for (java.util.List<ColorSquare> squareArrX : colorSquareArr) {
            for (ColorSquare redSquare : squareArrX) {
                redSquare.paintSquare(g);
            }
        }
    }
}
TOP

Related Classes of painting.DrawingPane

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.