Java - opaque color
Asked Answered
J

1

3

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.

Jactitation answered 19/10, 2011 at 15:18 Comment(2)
Could you post an sscce please?Hormone
In response to your edit, please note that an SSCCE only demonstrates the problem. It can be a very dumbed-down version of your original code, or a piece of code constructed specifically for the purpose of demonstration. By making the SSCCE, you will also discover if Stroke is part of the problem.Hormone
A
10

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.

enter image description here

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);
    }
}
Anticlockwise answered 19/10, 2011 at 15:54 Comment(8)
I try set composite to use src over composite, and make common lines color as 0.5 alpha, and that other line 1.0 alpha, but i get some idea. Maybe use common lines as destination and that last one as src over.... i am going to try it.Jactitation
I will be grateful for preparing some sscce. Method is paintLines(Graphics2D graphics), which is called in overriden methon paint(Graphics g) with some other methods. I change code a bit, because of optimalization (map take long time to compute and manipulate with so many objects...) In paintLines method at first draw few lines and than use some rule to change composite and draw last one over them. I tried it to get graphics.createGraphics() to copy graphics into 2 graphics (srcGraphics, dstGraphics) and than i modified it, but it doesnt work totally. Please, simple sscce to understand problemJactitation
You should edit your question to include your sscce that exhibits the problem you describe. Please read the link on how to narrow the focus of your example.Anticlockwise
I apologize for the typographical error that may have mislead you.Anticlockwise
I apologize for my stupidity, but it doesn't work in my case. I had tried it make similar to your sscce (which is useful, but not for me unfortunately). Several lines are "overdrawn" very nice, but other are not properly drawn. Here are 2 screenshots example imgupload.sk/viewer.php?file=03152286739135047006.bmp , imgupload.sk/viewer.php?file=74513883868686193032.bmp .Jactitation
It looks as if you are going to have to render the top-most line (cyan?) in a BufferedImage that has been filled with AlphaComposite.Clear. Then copy it using Src mode. See also this example.Anticlockwise
Nope, i have that 2 scroll panes, and this lines are painted in glasspane of whole application (mainFrame). I don't know, how it completly works - i think, that BufferedImage drawn in scroll pane has another graphics and it doesn't affect glasspane graphic. Am i wrong? I tried rendering too - no change. Application has over 5k lines of code, paint methods about 400, so i don't want to fix it with method trial and error anymore, cos last three days i am only trying to fix this - last bug (i hope).Jactitation
After you compose the line off-screen, you draw in onto your graphics context in paintComponent().Anticlockwise

© 2022 - 2024 — McMap. All rights reserved.