Java Scratch Card演示

时间:2011-12-29 08:14:26

标签: java alpha-transparency

用Java构建彩票刮刮卡应用程序。已完成分配,只需要帮助处理划痕功能。

基本上程序的工作原理如下:

  1. 为背景创建图像(现在它只是一个白色背景,但后来显然是带有乐透符号的刮刮卡图像)
  2. 创建card_surface,它只是一个绿色层,而不是在划伤时显示背后的图像。
  3. onMouseDragged()我使用笔划从当前鼠标坐标绘制一条线到新的鼠标坐标。我试过在这个笔画上设置Alphacomposite,以为它会显示下面的图像。不幸的是,不过。
  4. 感谢任何帮助...

     import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    
    import java.awt.Stroke;
    import java.awt.BasicStroke;
    import java.awt.AlphaComposite;
    
    
    public class Main {
      public static void main(String[] args) {
        JFrame frame = new JFrame();
        final DrawPad drawPad = new DrawPad();
        frame.add(drawPad, BorderLayout.CENTER);
        JButton clearButton = new JButton("New Scratch-Card");
        clearButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            drawPad.clear();
          }
        });
        frame.add(clearButton, BorderLayout.SOUTH);
        frame.setSize(500, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    
    }
    class DrawPad extends JComponent {
      Image image;
      Image card_surface;
    
      Graphics2D graphics2D;
      int currentX, currentY, oldX, oldY;
    
    
    
      public DrawPad() {
        final Stroke stroke = new BasicStroke (17.0F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
        //image = new ImageIcon("iPhone-4-Pattern-Wallpaper-07.jpg").getImage();
        setDoubleBuffered(false);
        addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
            oldX = e.getX();
            oldY = e.getY();
          }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
          public void mouseDragged(MouseEvent e) {
            currentX = e.getX();
            currentY = e.getY();
            if (graphics2D != null){
          graphics2D.setStroke(stroke);
              graphics2D.setPaint(Color.GRAY);
              graphics2D.setComposite(makeComposite(0.5F));
              graphics2D.drawLine(oldX, oldY, currentX, currentY);
            repaint();
            oldX = currentX;
            oldY = currentY;
          }
    }
        });
      }
    private AlphaComposite makeComposite(float alpha) {
        int type = AlphaComposite.SRC_OVER;
        return(AlphaComposite.getInstance(type, alpha));
      }
     public void clear() {
        image=null;
        card_surface=null;
    
        repaint();
      }
      public void paintComponent(Graphics g) {
        if (image == null) {
    
          image = createImage(getSize().width, getSize().height);
          graphics2D = (Graphics2D) image.getGraphics();
          graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
              RenderingHints.VALUE_ANTIALIAS_ON);
    
    
    
        graphics2D.setPaint(Color.white);
            graphics2D.fillRect(0, 0, getSize().width, getSize().height);
            graphics2D.setPaint(Color.black);
    
        repaint();
    
        }
    
         if (card_surface == null) {
        card_surface = createImage(getSize().width, getSize().height);
            graphics2D = (Graphics2D) card_surface.getGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
          RenderingHints.VALUE_ANTIALIAS_ON);   
    
        graphics2D.setPaint(Color.green);
            graphics2D.fillRect(0, 0, getSize().width, getSize().height);
    
        repaint();
    }
    
    
        g.drawImage(image, 0, 0, null);
        g.drawImage(card_surface, 0, 0, null);
    
      }
    
    
    }
    

2 个答案:

答案 0 :(得分:4)

您要做的是在用户“刮擦”顶部图像时显示基础图像,是否正确?

这里实际上需要两个图像:第一个是原始图像,第二个是带有灰色背景的空白图像,它覆盖第一个图像,隐藏它。当用户“划伤”顶部图像时,您需要在灰色图像上绘制透明色,以显示底层图像。

所以步骤是:

在空白处绘制原始图像 当用户'划痕'在顶部图像上以透明颜色绘制时 重新绘制图像,顶部图像现在具有透明部分 重复

这听起来很复杂,但我认为它不会太难,你只需要将两个图像分开并使用缓冲区绘制一个在另一个上面。有关使用透明缓冲图像的信息,请参阅此文章How to make a color transparent in a BufferedImage and save as PNG

我希望这会有所帮助。

答案 1 :(得分:0)

确定设法让基础工作正常,

我使用了Ewald的链接,谢谢你的伴侣!

这是工作代码(只需将'iphone4'图像路径更改为您选择的图像):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.awt.Toolkit;
import java.io.File;
import javax.imageio.ImageIO;

import java.awt.Stroke;
import java.awt.BasicStroke;
import java.awt.AlphaComposite;


public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    final DrawPad drawPad = new DrawPad();
    frame.add(drawPad, BorderLayout.CENTER);
    JButton clearButton = new JButton("New Scratch-Card");
    clearButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        drawPad.clear();
      }
    });
    frame.add(clearButton, BorderLayout.SOUTH);
    frame.setSize(500, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

}
class DrawPad extends JComponent {
  BufferedImage image;
  BufferedImage card_surface;

  Graphics2D graphics2D;
  int currentX, currentY, oldX, oldY;



  public DrawPad() {
    final Stroke stroke = new BasicStroke (17.0F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);

    setDoubleBuffered(false);
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        oldX = e.getX();
        oldY = e.getY();
      }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e) {
        currentX = e.getX();
        currentY = e.getY();
        if (graphics2D != null){
      graphics2D.setStroke(stroke);
          graphics2D.setPaint(Color.GRAY);
          //graphics2D.setComposite(makeComposite(0.5F));
          graphics2D.drawLine(oldX, oldY, currentX, currentY);
        repaint();
        oldX = currentX;
        oldY = currentY;
      }
}
    });
  }
private AlphaComposite makeComposite(float alpha) {
    int type = AlphaComposite.SRC_OVER;
    return(AlphaComposite.getInstance(type, alpha));
  }
 private Image TransformColorToTransparency(BufferedImage image, Color c1, Color c2)
  {
    // Primitive test, just an example
    final int r1 = c1.getRed();
    final int g1 = c1.getGreen();
    final int b1 = c1.getBlue();
    final int r2 = c2.getRed();
    final int g2 = c2.getGreen();
    final int b2 = c2.getBlue();
    ImageFilter filter = new RGBImageFilter()
    {
      public final int filterRGB(int x, int y, int rgb)
      {
        int r = (rgb & 0xFF0000) >> 16;
        int g = (rgb & 0xFF00) >> 8;
        int b = rgb & 0xFF;
        if (r >= r1 && r <= r2 &&
            g >= g1 && g <= g2 &&
            b >= b1 && b <= b2)
        {
          // Set fully transparent but keep color
          return rgb & 0xFFFFFF;
        }
        return rgb;
      }
    };

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
      return Toolkit.getDefaultToolkit().createImage(ip);
  }
private BufferedImage ImageToBufferedImage(Image image2, int width, int height)
  {
    BufferedImage dest = new BufferedImage(
        width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = dest.createGraphics();
    //g2.drawImage(image, 0, 0, null);
    g2.drawImage(image2, 0, 0, null);
    //g2.dispose();
    return dest;
  }
 public void clear() {
    image=null;
    card_surface=null;

    repaint();
  }
  public void paintComponent(Graphics g) {



 if (image == null) {

    image = new BufferedImage(500, 300, BufferedImage.TYPE_INT_ARGB);
    String imagePath = "iPhone-4-Pattern-Wallpaper-07.jpg";
    File inFile = new File(imagePath);
    try{
    image = ImageIO.read(inFile);
}catch(java.io.IOException e){System.out.println(e);}


    graphics2D = image.createGraphics();

      //image = new ImageIcon().getImage("");
      //image = createImage(getSize().width, getSize().height);
      //graphics2D = (Graphics2D) image.getGraphics();
      graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
          RenderingHints.VALUE_ANTIALIAS_ON);



    //graphics2D.setPaint(Color.white);
       // graphics2D.fillRect(0, 0, getSize().width, getSize().height);
       // graphics2D.setPaint(Color.black);

    repaint();

    }

     if (card_surface == null) {
    card_surface = new BufferedImage(500, 300, BufferedImage.TYPE_INT_ARGB);
        graphics2D = (Graphics2D) card_surface.getGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON);   

    graphics2D.setPaint(Color.green);
        graphics2D.fillRect(0, 0, getSize().width, getSize().height);

    repaint();
}


    g.drawImage(image, 0, 0, null);
    g.drawImage(card_surface, 0, 0, null);
    Image transpImg2 = TransformColorToTransparency(card_surface, new Color(0, 50, 77), new Color(200, 200, 255));
    BufferedImage resultImage2 = ImageToBufferedImage(transpImg2, image.getWidth(), image.getHeight());
    g.drawImage(image, 0, 0, null);
    g.drawImage(resultImage2, 0, 0, null); 

   /* File outFile2 = new File("map_with_transparency2.png");
    try{
    ImageIO.write(resultImage2, "PNG", outFile2);
    }catch(Exception e){System.out.println(e);}
    //card_surface = (BufferedImage)transpImg2;
    */
  }


}