Java - 橡胶alpha功能

时间:2014-02-01 23:50:47

标签: java graphics alpha graphics2d

我目前正在制作一个绘画程序,目前我正在实施一个橡胶工具(一个使用alpha颜色的版本)。问题是,我无法通过mouseDragged事件绘制不可见的线条,因为它们只会在之前的绘画之上...

我已经考虑过bImg上的setRGB,但它需要得到mouseDragged的支持,并且还要设置行变量的粗细。有人有任何想法吗?

编辑:为什么要投票?

2 个答案:

答案 0 :(得分:2)

downwote可能与以下事实有关: 你试图获得什么,以及你已经尝试过的

我假设你想在mouseDragged的图像上画线。这些行应该具有“橡皮”/“橡皮擦”的效果,删除一些以前绘制的像素,或者(当你说你想要一个alpha值时),可能不会删除它们,但只是让他们更苍白。使用setRGB手动更改像素值不是一种选择,因为此橡胶效果应具有某个厚度,您可以通过在调用graphics.setStroke(new BasicStroke(thickness))之前设置graphics.draw(rubberLine)来选择。

如果这一切都正确,那么您可能需要使用http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#setComposite%28java.awt.Composite%29设置合适的合成。特别是,您必须设置AlphaComposite(http://docs.oracle.com/javase/6/docs/api/java/awt/AlphaComposite.html)。您可以尝试使用不同的“橡皮效果”,具体取决于擦除像素的alpha值,以及您为“橡胶颜色”设置的Alpha值。

编辑:一个例子,......也许你应该更清楚地描述问题

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SimplePaint
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        PaintPanel paintPanel = new PaintPanel();

        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(
            createControlPanel(paintPanel), BorderLayout.NORTH);
        f.getContentPane().add(paintPanel, BorderLayout.CENTER);

        f.setSize(500,500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    static JPanel createControlPanel(final PaintPanel paintPanel)
    {
        JPanel controlPanel = new JPanel(new GridLayout(0,2));
        final JLabel label = new JLabel("Paint");
        final JSlider slider = new JSlider(0, 255, 0);
        slider.addChangeListener(new ChangeListener()
        {

            @Override
            public void stateChanged(ChangeEvent e)
            {
                int value = slider.getValue();
                if (value <= 0)
                {
                    label.setText("Paint");
                }
                else
                {
                    label.setText("Rubber alpha "+value);
                }
                paintPanel.setRubber(value);
            }
        });
        controlPanel.add(label);
        controlPanel.add(slider);
        return controlPanel;
    }
}



class PaintPanel extends JPanel implements MouseMotionListener
{
    private BufferedImage image;
    private Point previousPoint = new Point();
    private int rubberAlpha = -1;

    PaintPanel()
    {
        addMouseMotionListener(this);
    }

    void setRubber(int alpha)
    {
        this.rubberAlpha = alpha;
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int w = getWidth();
        int h = getHeight();
        if (image == null)
        {
            image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics imageG = image.getGraphics();
            imageG.setColor(Color.WHITE);
            imageG.fillRect(0, 0, w, h);
            imageG.dispose();
        }
        else if (image.getWidth() != w || image.getHeight() != h)
        {
            BufferedImage newImage = 
                new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics imageG = newImage.getGraphics();
            imageG.drawImage(image, 0, 0, null);
            imageG.dispose();
            image = newImage;
        }
        g.drawImage(image, 0, 0, null);
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
        if (image == null)
        {
            return;
        }

        Graphics2D g = (Graphics2D)image.getGraphics();
        g.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);

        if (rubberAlpha > 0)
        {
            g.setColor(new Color(255,255,255,rubberAlpha));
            Line2D line = new Line2D.Double(previousPoint, e.getPoint());
            g.setStroke(new BasicStroke(20, 
                BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            g.draw(line);
        }
        else
        {
            g.setColor(new Color(255,0,0,255));
            Line2D line = new Line2D.Double(previousPoint, e.getPoint());
            g.setStroke(new BasicStroke(20, 
                BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            g.draw(line);
        }

        g.dispose();
        repaint();

        previousPoint = e.getPoint();
    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
        previousPoint = e.getPoint();
    }
}

答案 1 :(得分:1)

您可以将图片转换为writeableraster,并使用getpixel()和setpixel()方法在特定条件下转换用户鼠标指针下方的颜色。