JScrollPanel内部的Java2D图形效果不佳

时间:2014-02-07 17:01:30

标签: java swing rendering jscrollpane java-2d

我是荷兰计算机科学专业的学生,​​我正在完成一项任务,我必须绘制几种形状。为了使这些形状看起来更好,我设置渲染提示抗锯齿。但现在我的问题是,当我使用我的JScrollPanel滚动到屏幕上没有形状的地方然后向后滚动时,似乎抗锯齿和透明度无法正常工作。

在下面链接的图像中,我已滚动到形状的一半,然后向后滚动。

Link to image

我真的不知道问题所在的位置,所以我在下面的课程中提供了完整的代码。

You can download the full eclipse project folder here.

我想提前感谢你,

Milan v.Dijck

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")

public class AgendaPanel extends JPanel{
    private final int WIDTH = 24; // width in hours
    private final Color BUTTON_TOP_GRADIENT = new Color(250, 250, 250);
    private final Color BUTTON_BOTTOM_GRADIENT = new Color(210, 210, 210);

    private ArrayList<Line> lines = new ArrayList<Line>();
    private ArrayList<Shape> shadowBoxes = new ArrayList<Shape>();
    private ArrayList<Shape> itemBoxes = new ArrayList<Shape>();
    private ArrayList<Image2D> images = new ArrayList<Image2D>();
    private ArrayList<String2D> names = new ArrayList<String2D>();

    public AgendaPanel(){
        setPreferredSize(new Dimension(WIDTH*100, 300));

        drawGrid();
    }

    public void drawGrid(){
        for(int i=100; i < WIDTH*100; i+=100){
            lines.add(new Line(i,0,i,300,Color.GRAY));
        }

        for(int i=50; i < WIDTH*100; i+=100){
            lines.add(new Line(i,0,i,300, Color.LIGHT_GRAY));
        }

        // change to the amount of podia
        for(int i=75; i < 300; i+=75){
            lines.add(new Line(0, i, WIDTH*100, i, Color.black));
        }

        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        RenderingHints qualityHints = new RenderingHints(
                  RenderingHints.KEY_ANTIALIASING,
                  RenderingHints.VALUE_ANTIALIAS_ON );

        qualityHints.put(
                  RenderingHints.KEY_RENDERING,
                  RenderingHints.VALUE_RENDER_QUALITY );

        g2.setRenderingHints( qualityHints );

        // Add agenda items to the draw queue
        addAgendaItem("logo.png", "Minus Militia", 2, 2, 170);

        //draw time grid
        for(Line line : lines){
            g2.setColor(line.color);
            g2.drawLine(line.x1, line.y1, line.x2, line.y2);
        }

        //Shape roundRect = new RoundRectangle2D.Double(2, 2, 170, 71, 20, 20);

        //Paint all shadow shapes as the first layer over the grid with a transparacy of 30%
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.3f));

        for (Shape shadow : shadowBoxes){
            paintBorderShadow(g2,4,shadow);
        }
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1f));

        //Paint all boxes as second layer over the shadows
        g2.setStroke(new BasicStroke());
        g2.setPaint(new GradientPaint(  new Point(0, 0),
                BUTTON_TOP_GRADIENT,
                new Point(0, 71),
                BUTTON_BOTTOM_GRADIENT));

        for (Shape box : itemBoxes){
            g2.fill(box);
        }

        // Paint as third layer all the images over the boxes
        for (Image2D image : images){
            g2.drawImage(image.getImage() , image.getX(), image.getY(), null);
        }

        g2.setColor(Color.black);

        //Paint all names as fourth and last layer
        for (String2D name : names){
            g2.drawString(name.getText(), name.getX(), name.getY());
        }

    }

    private void addAgendaItem(String imageFile,String name, int x, int y, int length){

        shadowBoxes.add(new RoundRectangle2D.Double(x+3, y+3, length-3, 68, 20, 20));
        itemBoxes.add(new RoundRectangle2D.Double(x, y, length, 71, 20, 20));

        BufferedImage in = null;

        try {
            in = ImageIO.read(new File("logo.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        images.add(new Image2D(x+8, y+8 ,drawPicture(scaleImage(in, 55, 55,Color.black),20)));

        names.add(new String2D(x+73,y+38, name));

    }

    private BufferedImage drawPicture(BufferedImage image, int cornerRadius){
            int w = image.getWidth();
            int h = image.getHeight();
            BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

            Graphics2D g2 = output.createGraphics();

            g2.setComposite(AlphaComposite.Src);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.WHITE);
            g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));

            g2.setComposite(AlphaComposite.SrcAtop);
            g2.drawImage(image, 0, 0, null);

            g2.dispose();

            return output;
    }

    public BufferedImage scaleImage(BufferedImage img, int width, int height, Color background) {
        int imgWidth = img.getWidth();
        int imgHeight = img.getHeight();

        if (imgWidth*height < imgHeight*width) {
            width = imgWidth*height/imgHeight;
        } else {
            height = imgHeight*width/imgWidth;
        }
        BufferedImage output = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = output.createGraphics();
        try {
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g.setBackground(background);
            g.clearRect(0, 0, width, height);
            g.drawImage(img, 0, 0, width, height, null);
        } finally {
            g.dispose();
        }
        return output;
    }

    private void paintBorderShadow(Graphics2D g2, int shadowWidth, Shape shape) {

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        int sw = shadowWidth*2;
        for (int i=sw; i >= 2; i-=2) {
            float pct = (float)(sw - i) / (sw - 1);
            g2.setColor(getMixedColor(Color.DARK_GRAY, pct,
                                      Color.DARK_GRAY, 1.0f-pct));
            g2.setStroke(new BasicStroke(i));
            g2.draw(shape);
        }
    }

    private static Color getMixedColor(Color c1, float pct1, Color c2, float pct2) {
        float[] clr1 = c1.getComponents(null);
        float[] clr2 = c2.getComponents(null);
        for (int i = 0; i < clr1.length; i++) {
            clr1[i] = (clr1[i] * pct1) + (clr2[i] * pct2);
        }
        return new Color(clr1[0], clr1[1], clr1[2], clr1[3]);
    }

}

Image2D,String2D和Line类:

public class String2D {
    private int x = 0;
    private int y = 0;
    private String text = "";

    public String2D(int x, int y, String text){
        this.x = x;
        this.y = y;
        this.text = text;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

import java.awt.image.BufferedImage;


public class Image2D {
    private int x = 0;
    private int y = 0;
    private BufferedImage image = null;

    public Image2D(int x, int y, BufferedImage image){
        this.x = x;
        this.y = y;
        this.image = image;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }
}
import java.awt.*;

public class Line{
    public int x1;
    public int x2;
    public int y1;
    public int y2;
    public Color color;

    public Line(int x1, int y1, int x2, int y2, Color color){
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
        this.color = color;
    }
}

2 个答案:

答案 0 :(得分:0)

在个人尤里卡时刻之后,我发现了我的问题。我正在调整大小并给每个重绘的图像圆角这改变了g2对象以某种方式禁用我的渲染。我已将此代码放入构造函数中,现在每个都运行顺畅。

以下代码是问题所在:

    private BufferedImage drawPicture(BufferedImage image, int cornerRadius){
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2 = output.createGraphics();

    g2.setComposite(AlphaComposite.Src);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.WHITE);
    g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));

    g2.setComposite(AlphaComposite.SrcAtop);
    g2.drawImage(image, 0, 0, null);

    g2.dispose();

    return output;
    }

    public BufferedImage scaleImage(BufferedImage img, int width, int height, Color background) {
int imgWidth = img.getWidth();
int imgHeight = img.getHeight();

if (imgWidth*height < imgHeight*width) {
    width = imgWidth*height/imgHeight;
} else {
    height = imgHeight*width/imgWidth;
}
BufferedImage output = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_RGB);
Graphics2D g = output.createGraphics();
try {
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setBackground(background);
    g.clearRect(0, 0, width, height);
    g.drawImage(img, 0, 0, width, height, null);
} finally {
    g.dispose();
}
return output;
}

我在paintcomponent例程中调用了它:

addAgendaItem("logo.png", "Minus Militia", 2, 2, 170);

这是哪种方法:

private void addAgendaItem(String imageFile,String name, int x, int y, int length){
shadowBoxes.add(new RoundRectangle2D.Double(x+3, y+3, length-3, 68, 20, 20));
itemBoxes.add(new RoundRectangle2D.Double(x, y, length, 71, 20, 20));

BufferedImage in = null;

try {
    in = ImageIO.read(new File("logo.png"));
} catch (IOException e) {
    e.printStackTrace();
}

images.add(new Image2D(x+8, y+8 ,drawPicture(scaleImage(in, 55, 55,Color.black),20)));

names.add(new String2D(x+73,y+38, name));

}

这会调用上述两种方法(每次重绘)导致我的表现和绘图问题。

答案 1 :(得分:0)

这是一种可行的方法,它有时可能会提高CPU效率(以及更多的内存消耗),但这是解决问题的方法。

问题的存在是因为您将作为参数提供的Graphics对象的状态更改为“paintComponent”方法。这是一个常见的错误,会导致各种奇怪的图形错误。

您不应该更改对象状态的原因是因为此对象由其父组件传递给您的组件,该组件从其父组件获取它,依此类推。链中的每个组件,直到拥有窗口,都使用相同的Graphics对象来绘制自身。同一个Graphics对象也将自动传递给组件的任何子对象。这是Swing如何工作的基础(关于Swing如何与AWT相比甚至SWT的讨论完全是另一个主题)。

你应该做的是保持国家。这可以通过以下两种方式之一完成:

  1. 在方法返回之前(即方法中的最后几行代码),将您更改的所有内容设置回原来的样式(在您的情况下是渲染提示等)。如果您只对Graphics对象的状态进行了一些更改,这是一个很好的方法。
  2. 使用'create'方法在方法开头创建Graphics对象的副本,使用它代替原始对象进行绘制,并在方法结束时使用'dispose'去除它方法