Java-橡胶α功能



我当前正在制作涂料程序,并且目前正在实现橡胶工具(一种可与Alpha Colors一起使用的版本)。问题是,我无法通过慕斯事件绘制看不见的线条

我已经在BIMG上考虑了setRGB,但是需要由鼠标来支撑,并使线变量的厚度也是如此。有人有任何想法吗?

编辑:为什么要投票?

downwote可能与您尝试实现的事实并不完全清楚您已经尝试过它。

我假设您想在mouseDragged中绘制图像。这些线应具有"橡胶"/"橡胶"的效果,删除一些先前绘制的像素,或者(当您说要具有alpha值时),也许不是 emoving ,但是只是使它们更苍白。用setRGB手动更改像素值不是一个选项,因为此橡胶效应应该具有一定的厚度,您可能会通过在调用graphics.draw(rubberLine)之前设置graphics.setStroke(new BasicStroke(thickness))选择。

.awt.com posite%29。特别是,您将必须设置Alphacomposite(http://docs.oracle.com/javase/6/docs/api/java/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();
    }
}

您可以将图像转换为writeableraster,并使用getPixel()和setPixel()方法在某些条件下将颜色转换为颜色。

相关内容

  • 没有找到相关文章

最新更新