简体   繁体   English

Java - 不透明的颜色

[英]Java - opaque color

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. 有时,那些更多的线条对于最后一个线条是不透明的。

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). 我尝试制作新的颜色并使用alpha复合材料设置颜色0.7 - 对于那些更多的线条,我保留一种颜色默认 - 不透明(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. 我在玻璃窗上画出那条线。


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; 原理是2色,例如Color basicColor; Color similarColor; 颜色相似颜色;

than i have paint method and 2 hashmaps as attributes - some points are stored. 比我有绘画方法和2个哈希图作为属性 - 存储了一些点。 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. 我迭代这个地图,记住一点和他相似,所有其他点连接graphics2D.drawLine(x1,y1,x2,y2),然后改变颜色并用另一种颜色绘制最后一行。 I am modifying stroke too, to make it more significant. 我也在修改中风,使其更显着。

I hope it will be enough... 我希望它足够......


edit2: i have some Point similarPoint than some robust paint method and here is graphics modifying iterator iterate over list of points' lists. edit2:我有一些Point similarPoint而不是一些强大的paint方法,这里是图形修改迭代器迭代点列表的列表。

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));
}

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. 中风问题不存在,因为起初我没有中风。

Thanks for any idea. 谢谢你的想法。

The result depends on which compositing rule is specified in the graphics context using setComposite() . 结果取决于使用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. 它还可以帮助您准备一个展示您描述的问题的sscce

Addendum: Here's an example that shows how one might use AlphaComposite.Src mode for this. 附录:这是一个示例,显示了如何使用AlphaComposite.Src模式。

在此输入图像描述

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM