Home > other >  JPanel draw BufferedImage
JPanel draw BufferedImage

Time:12-20

I have a problem drawing a BufferedImage, it's hard to describe, so you can see it in the video: https://www.youtube.com/watch?v=9K-QTKHcOYA

Here's my code:

In the JPanel

@Override
public void mouseMoved(MouseEvent e) {
    if(Tool.selectedTool != null)
        Tool.selectedTool.onPaintingAreaMouseMoved(e.getX(),e.getY(),z);
}

@Override
public void onPaintingAreaMouseMoved(int x, int y, int z) {
    if(clicks == 1){
        curve.controlPoints[1] = new Point(x, y, z);
        PaintingArea.paintingArea.refresh();
    }
}

Because of an error, I decided not to post the code of my curve class here, but I checked it and there was no mistake.

In the JPanel class

public void refresh(){
    axis = new int[width * height];
    for (int i = 0; i < axis.length; i  )
        axis[i] = background;
    for(int i = 0; i < Line.lines.size(); i  ) {
        Point[] points = Line.lines.get(i).getPoints();
        for(int m = 0; m < points.length; m  )
            axis[points[m].x   width*points[m].y] = 0;
    }
    repaint();
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    setSize(width,height);
    BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB);
    image.setRGB(0,0, width, height, axis,0, 1);
    g.drawImage(image,0,0,image.getWidth(),image.getHeight(),null);
}

Can anyone tell me what I am doing wrong?

CodePudding user response:

here is the code of the Line.getPoints() method:

@Override
public Point[] getPoints() {
    List<Point> points = new ArrayList<Point>();
    int[] coefficients = getBinomialCoefficients(controlPoints);
    Point last = controlPoints[0];
    for(int i = 0; i <= PaintingArea.percent; i  ){
        double t = (double)i/(double)PaintingArea.percent;
        Point point = new Point(0,0,0);
        for (int m = 0; m < controlPoints.length; m  )
            point = new Point(
                    (int)(point.x   (double)controlPoints[m].x * Math.pow(t, m)
                            * Math.pow(1 - t, controlPoints.length - 1 - m) * (double)coefficients[m]),
                    (int)(point.y   (double)controlPoints[m].y * Math.pow(t, m)
                            * Math.pow(1 - t, controlPoints.length - 1 - m) * (double)coefficients[m]),
                    (int)(point.z   (double)controlPoints[m].z * Math.pow(t, m)
                            * Math.pow(1 - t, controlPoints.length - 1 - m) * (double)coefficients[m]));
        if(Math.sqrt((double)(point.x - last.x)*(double)(point.x - last.x)   (double)(point.y - last.y)*(double)(point.y - last.y)
              (double)(point.z - last.z)*(double)(point.z - last.z)) > 1)
            addPointsBetween(point,last,points);
        points.add(point);
        last = point;
    }
    Point[] array = new Point[points.size()];
    for (int i = 0; i < array.length; i  )
        array[i] = points.get(i);
    return array;
}

void addPointsBetween(Point point, Point last, List<Point> points){
    int distance = (int)Math.sqrt((double)(point.x - last.x)*(double)(point.x - last.x)
              (double)(point.y - last.y)*(double)(point.y - last.y)
              (double)(point.z - last.z)*(double)(point.z - last.z));
    double atan = Math.atan((double)(point.y - last.y) / Math.sqrt((double)(point.x - last.x)*(double)(point.x - last.x)
              (double)(point.z - last.z)*(double)(point.z - last.z))),
        atan2 = Math.atan((double)(point.z - last.z) / (double)(point.x - last.x));
    for (int i = 0; i < distance; i  )
        points.add(new Point((int)((double)last.x   (double)i * Math.cos(atan) * Math.cos(atan2)),
                (int)((double)last.y   (double)i * Math.sin(atan)),
                (int)((double)last.z   (double)i * Math.cos(atan) * Math.sin(atan2))));
}

public int[] getBinomialCoefficients(Point[] controlPoints){
    int n = controlPoints.length - 1;
    int[] coefficients = new int[n   1];
    coefficients[0] = 1;
    for (int i = 1; i < n   1; i  )
        coefficients[i] = (int)((double)coefficients[i - 1] * ((double)(n - (i - 1)) / (double)i));
    return coefficients;
}

CodePudding user response:

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(menubar, BorderLayout.NORTH);
    panel.add(toolbar, BorderLayout.WEST);
    panel.add(new PaintingArea(), BorderLayout.CENTER);

    add(panel);
    pack();
    setSize(800,700);
    setVisible(true);
  • Related