Java - 不透明颜色 [英] Java - opaque color

查看:142
本文介绍了Java - 不透明颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画一些线。问题是关于颜色。例如。我有几行红色,而我绘制一行蓝色(或反转)。有时,那些线更多,对于最后一个是不透明的。

i am trying to draw some lines. Problem is about colors. For example. I have several lines of red color, and than i draw one line of blue color (or reversed). And sometimes, that lines those is more, is opaque for that last one.

我试图用alpha复合0.7制作新的颜色和颜色 - 对于那些更多的线条,一个颜色我默认 - 不透明(alpha 1.0)。起初我画了更多的线,而不是最后一个。但是那条线覆盖那一条。有解决这个问题的解决方案吗?

I tried to make new color and set color with alpha composite 0.7 - for those more lines, and one color i left default - opaque (alpha 1.0). At first i draw more lines, and than last one. But that lines "overwrite" that one. Is there some solution to fix this problem?

我在玻璃板上绘制线条。

I draw that lines on glasspane.

> 编辑 :该代码是健壮的,因此很难发布,它是论文的一部分。
原理是2色例如
颜色basicColor;
颜色similarColor;

edit: that code is robust, so it is difficult to post it, and it is one part of thesis. principle is 2 color for example Color basicColor; Color similarColor;

比我有绘制方法和2哈希作为属性 - 一些点被存储。
i迭代这个地图,记住一点和他类似,所有其他连接
graphics2D.drawLine(x1,y1,x2,y2),然后更改颜色和最后一行用另一种颜色。我也修改中风,使它更重要。

than i have paint method and 2 hashmaps as attributes - some points are stored. i iterate over this map, remember that one point and similar to him, all other connect with graphics2D.drawLine(x1,y1,x2,y2) and than change color and paint last one line with another color. I am modifying stroke too, to make it more significant.

我希望已经足够了。

edit2:
i有一些Point similarPoint比一些强大的绘制方法,这里是图形修改
迭代器迭代点列表。

edit2: i have some Point similarPoint than some robust paint method and here is graphics modifying iterator iterate over list of points' lists.

Point similar = null;
Iterator<Point> secondIterator;
graphics.setColor(colorOfSimilar);
while (iterator.hasNext()) {
    Point point = iterator.next();
    if (point.equals(similarPoint)) {
        similar = similarPoint;
    } else {
        secondIterator = secondMap.get(point).iterator();
        while (secondIterator.hasNext()) {
            Point secondPoint = secondIterator.next();
            graphics2D.drawLine(point.getX(), point.getY(),
                secondPoint.getX(), secondPoint.getY());
        }
    }
}
if (similar != null) {
    secondIterator = secondMap.get(similar);
    graphics2D.setColor(hooverColor);
    graphics2D.setStroke(new BasicStroke(2.5f));
    while (secondIterator.hasNext()) {
        Point secondPoint = secondIterator.next();
        graphics2D.drawLine(similar.getX(), similar.getY(),
            secondPoint.getX(), secondPoint.getY());
    }
    graphics2D.setColor(colorOfSimilar);
    graphics2D.setStroke(new BasicStroke(1.0f));
}

我在记事本中写了一些,所以抱歉一些错误),但这是修改的机制,周围是其他方法iterate和其他,但它不重要。中风的问题不存在,因为一开始我没有中风。

i wrote it in notepad so sorry about some mistakes (i think brackets etc.), but this is mechanism of modifying, around that is other methods for iterate and other, but it is not important. Problem with stroke doesn´t exist, because at first i did it without stroke.

感谢任何想法。

推荐答案

结果取决于合成规则在图形上下文中使用 setComposite() 。此实用程序可能有助于了解各种模式。它还可以帮助您准备展示您描述的问题的 sscce

The result depends on which compositing rule is specified in the graphics context using setComposite(). This utility may be useful in understanding the various modes. It may also help you in preparing an sscce that exhibits the problem you describe.

附录:以下示例说明了如何使用 AlphaComposite.Src 模式。

Addendum: Here's an example that shows how one might use AlphaComposite.Src mode for this.

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

/** @see http://stackoverflow.com/questions/7823631 */
public class X extends JPanel {

    private static final int SIZE = 300;
    private static final int INSET = 64;
    private static final AlphaComposite OVER_HALF =
        AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
    private boolean src;

    public X(boolean src) {
        this.src = src;
        this.setBackground(Color.lightGray);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(SIZE, SIZE);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        Line2D line1 = new Line2D.Double(INSET, INSET,
            getWidth() - INSET, getHeight() - INSET);
        Line2D line2 = new Line2D.Double(getWidth() - INSET,
            INSET, INSET, getHeight() - INSET);
        g2.setStroke(new BasicStroke(64,
            BasicStroke.CAP_ROUND,
            BasicStroke.JOIN_BEVEL));
        g2.setComposite(OVER_HALF);
        g2.setColor(Color.red);
        g2.draw(line1);
        if (src) {
            g2.setComposite(AlphaComposite.Src);
        }
        g2.setColor(Color.blue);
        g2.draw(line2);
    }

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(1, 0));
        frame.add(new X(false));
        frame.add(new X(true));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

这篇关于Java - 不透明颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆