首页 > 解决方案 > paintComponent 覆盖像素

问题描述

所以我在我的 BufferedImage 中添加了一个网格,PaintComponent就像这样。它工作完美。

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    Color c=new Color(184, 184, 184, 255);
    g.drawImage(canvas, 0, 0, this);
    for ( int x = 0; x <= getWidth(); x += 10 ){
        for ( int y = 0; y <= getHeight(); y += 10 ){
            g.setColor(c);
            g.drawRect( x, y, 10, 10 );
        }
    }
}

但是,当我在这个函数中画出我的线/圆时:

public void drawPixels(Object val_x, Boolean highlight)
{
    String[] values = val_x.toString().replaceAll("[()]", "").split(",");
    if (highlight){
        canvas.setRGB(Integer.parseInt(values[0]), Integer.parseInt(values[1]), Color.RED.getRGB());
    }else{
        canvas.setRGB(Integer.parseInt(values[0]), Integer.parseInt(values[1]), c.getRGB());
    }
    repaint();
}

我看到我的一些像素被paintComponent.

有没有办法对我绘制的像素后面的网格进行 z 索引,或者可能先绘制网格,然后在其顶部绘制?

我的 GUI 看起来像这样,您可以在其中看到我的圆圈像素与网格交换:

在此处输入图像描述

如果要查看整个绘图 .java 文件

package DrawCanvas;

import com.sun.org.apache.xpath.internal.operations.Bool;

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

public class DrawCanvas extends JPanel {
    private                 BufferedImage canvas;
    private static          Graphics g2;
    final private           Color c         = Color.BLACK;
    private final static    Color def_bg    = new Color(108, 108, 108, 255);
    //private final static    Color def_bg    = new Color(80, 80, 80, 255);
    int                     width           = 1280;
    int                     height          = 720;
    public DrawCanvas() {
        canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        g2 = canvas.getGraphics();
        fillCanvas(def_bg);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(canvas.getWidth(), canvas.getHeight());
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        Color c=new Color(184, 184, 184, 255);
        g.drawImage(canvas, 0, 0, this);
        for ( int x = 0; x <= getWidth(); x += 10 ){
            for ( int y = 0; y <= getHeight(); y += 10 ){
                g.setColor(c);
                g.drawRect( x, y, 10, 10 );
            }
        }
    }

    public void fillCanvas(Color c) {
        int color = c.getRGB();
        for (int x = 0; x < canvas.getWidth(); x++) {
            for (int y = 0; y < canvas.getHeight(); y++) {
                canvas.setRGB(x, y, color);
            }
        }
        repaint();
    }


    // Implementation from Wikipedia: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases
    public void drawPixels(Object val_x, Boolean highlight)
    {
        String[] values = val_x.toString().replaceAll("[()]", "").split(",");
        if (highlight){
            canvas.setRGB(Integer.parseInt(values[0]), Integer.parseInt(values[1]), Color.RED.getRGB());
        }else{
            canvas.setRGB(Integer.parseInt(values[0]), Integer.parseInt(values[1]), c.getRGB());
        }
        repaint();
    }

    public void clearImage() {
        Graphics2D g2 = (Graphics2D) canvas.getGraphics();
        g2.setBackground(def_bg);
        g2.clearRect(0,0, (int)canvas.getWidth(), (int)canvas.getHeight());
        repaint();
    }
}

编辑 1 解决方案

package DrawCanvas;

import com.sun.org.apache.xpath.internal.operations.Bool;

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

public class DrawCanvas extends JPanel {
    private                 BufferedImage canvas;
    private static          Graphics g2;
    final private           Color c         = Color.BLACK;
    private final static    Color def_bg    = new Color(108, 108, 108, 255);
    private final static    Color grid      = new Color(149, 149, 149, 186);
    //private final static    Color def_bg    = new Color(80, 80, 80, 255);
    int                     width           = 1280;
    int                     height          = 720;
    public DrawCanvas() {
        canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        g2 = canvas.getGraphics();
        fillCanvas(def_bg);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(canvas.getWidth(), canvas.getHeight());
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(canvas, 0, 0, this);
    }

    public void fillCanvas(Color c) {
        int color = c.getRGB();
        for (int x = 0; x < canvas.getWidth(); x++) {
            for (int y = 0; y < canvas.getHeight(); y++) {
                canvas.setRGB(x, y, color);
            }
        }
        drawGrid();
        repaint();
    }


    // Implementation from Wikipedia: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases
    // Implementation from Wikipedia: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases
    public void drawPixels(Object val_x, Boolean highlight)
    {
        Graphics2D graph = canvas.createGraphics();
        String[] values = val_x.toString().replaceAll("[()]", "").split(",");
        if (highlight){
            graph.setColor(Color.RED);
            graph.drawOval(Integer.parseInt(values[0]), Integer.parseInt(values[1]), 1, 1);
        }else{
            graph.setColor(c);
            graph.drawOval(Integer.parseInt(values[0]), Integer.parseInt(values[1]), 1, 1);
        }
    }

    public void drawGrid(){
        Graphics2D graph = canvas.createGraphics();
        for (int x = 0; x < canvas.getWidth(); x += 10) {
            for (int y = 0; y < canvas.getHeight(); y += 10) {
                graph.setColor(grid);
                graph.drawRect( x, y, 10, 10 );
            }
        }
        repaint();
    }

    public void clearImage() {
        Graphics2D g2 = (Graphics2D) canvas.getGraphics();
        g2.setBackground(def_bg);
        g2.clearRect(0,0, (int)canvas.getWidth(), (int)canvas.getHeight());
        drawGrid();
        repaint();
    }

}

在此处输入图像描述

标签: java

解决方案


推荐阅读