JPanel绘制曲线出现像素拉伸锯齿化的问题排查与解决
问题:自定义像素绘制的曲线在JPanel中出现锯齿化
我尝试在宽高为800的JPanel中绘制平滑曲线,但部分像素出现拉伸,导致曲线呈现锯齿化效果。调试时打印的像素数组显示正常,生成的BufferedImage本身也无拉伸问题,但在JPanel中绘制时该现象出现。我发现grf.drawRoundRect(50, 50, 400, 400, 200, 200)可实现平滑效果,但希望通过像素数组实现该功能。
相关代码如下:
public void draw(){ Graphics grf = screen.get_graphics(); int width = 400; int height = 400; int scale = 1; BufferedImage red_fill = BufferedImageFactory.fill(width, height, Color.RED); Matrixi2d square_mask = new Soft_Square().get_filled_in_mask(width, height); BufferedImage hue_soft_square = square_mask.mask_image(red_fill); grf.drawImage(hue_soft_square, 50, 50, null); }
public BufferedImage mask_image(BufferedImage input){ //BufferedImage result = new BufferedImage(width + edge*2, height + edge*2, BufferedImage.TYPE_INT_ARGB); BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); //for(int x = edge, mask_x = 0; x < width; x++, mask_x++){ //for(int y = edge, mask_y = 0; y < height; y++, mask_y++){ for(int x = 0, mask_x = 0; x < width; x++, mask_x++){ for(int y = 0, mask_y = 0; y < height; y++, mask_y++){ if(get(mask_x, mask_y) != 0){ int mask_alpha = get(mask_x, mask_y); int color = input.getRGB(x, y); int red = (0xFF & ( color >> 16)); int alpha = (0xFF & (color >> 24)); int blue = (0xFF & (color >> 0 )); int green = (0xFF & (color >> 8 )); //System.out.println("mask value: "+get(mask_x, mask_y)); //System.out.println("initial alpha: " + alpha); alpha = (int)(alpha * mask_alpha / 255.0); int sum = ((alpha & 0xff) << 24 | (red & 0xff) << 16 | (blue & 0xff) << 8 | (green & 0xff)); //System.out.println("alpha: " + alpha); //System.out.println("mask alpha: " + mask_alpha); result.setRGB(x, y, sum); //System.out.println("setting result: "+sum); } } } return result; }
最终通过启用绘图时的抗锯齿渲染解决了该问题。
内容的提问来源于stack exchange,提问作者Tyler
相关产品推荐
相关产品推荐

