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

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

问题描述

我正在尝试画一些线条.问题是关于颜色.例如.我有几行红色,然后我画了一行蓝色(或相反).有时,那些线条更多,对于最后一个是不透明的.

我尝试制作新颜色并使用 alpha 复合 0.7 设置颜色 - 对于那些更多的线条,我保留了一种颜色 - 不透明(alpha 1.0).一开始我画了更多的线,比最后一条线多.但是那条线覆盖"了那条线.有没有办法解决这个问题?

我在玻璃板上画了那条线.

<小时>

编辑:那个代码很健壮,所以很难发布,它是论文的一部分.原则是例如2色颜色基本颜色;颜色相似颜色;

比我有paint方法和2个hashmap作为属性-存储了一些点.我遍历这张地图,记住一点和他相似,所有其他都与graphics2D.drawLine(x1,y1,x2,y2) 然后改变颜色并用另一种颜色绘制最后一行.我也在修改中风,使其更重要.

我希望这足够了...

<小时>

edit2:我有一些 Point SimilarPoint 比一些强大的绘画方法,这里是图形修改迭代器迭代点列表的列表.

点相似=空;迭代器<点>第二个迭代器;graphics.setColor(colorOfSimilar);而(迭代器.hasNext()){点点 = iterator.next();如果 (point.equals(similarPoint)) {相似 = 相似点;} 别的 {secondIterator = secondMap.get(point).iterator();而 (secondIterator.hasNext()) {点 secondPoint = secondIterator.next();graphics2D.drawLine(point.getX(), point.getY(),secondPoint.getX(), secondPoint.getY());}}}如果(类似!= null){secondIterator = secondMap.get(similar);graphics2D.setColor(hooverColor);graphics2D.setStroke(new BasicStroke(2.5f));而 (secondIterator.hasNext()) {点 secondPoint = secondIterator.next();graphics2D.drawLine(similar.getX(),similar.getY(),secondPoint.getX(), secondPoint.getY());}graphics2D.setColor(colorOfSimilar);graphics2D.setStroke(new BasicStroke(1.0f));}

我在记事本中写的很抱歉有些错误(我认为括号等),但这是修改机制,围绕它是迭代和其他的其他方法,但这并不重要.中风的问题不存在,因为起初我没有中风.

感谢您的建议.

解决方案

结果取决于使用

import javax.swing.*;导入 java.awt.*;导入 java.awt.geom.*;/** @see http://stackoverflow.com/questions/7823631 */公共类 X 扩展 JPanel {私有静态最终 int SIZE = 300;私有静态最终 int INSET = 64;私有静态最终 AlphaComposite OVER_HALF =AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);私有布尔源;公共 X(布尔型 src){this.src = src;this.setBackground(Color.lightGray);}@覆盖公共维度 getPreferredSize() {返回新维度(大小,大小);}@覆盖公共无效paintComponent(图形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() - 插入,getHeight() - 插入);Line2D line2 = new Line2D.Double(getWidth() - INSET,插入,插入,getHeight() - 插入);g2.setStroke(new BasicStroke(64,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));g2.setComposite(OVER_HALF);g2.setColor(Color.red);g2.draw(line1);如果(源代码){g2.setComposite(AlphaComposite.Src);}g2.setColor(Color.blue);g2.draw(line2);}公共静态无效主(字符串 [] args){最终 JFrame 框架 = new JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLayout(new GridLayout(1, 0));frame.add(new X(false));frame.add(new X(true));框架.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}}

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). 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; Color similarColor;

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.

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.

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(). 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.

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天全站免登陆