用Java構建樂透刮刮卡應用程序。完成它的分配,只需要抓取功能的幫助。Java刮刮卡演示
基本程序是這樣的:
- 創建圖像爲背景(現在它只是一個白色背景,但後來顯然是與樂透符號的刮刮卡的圖像)
- 創建card_surface這只是一個綠色的層,而不是在劃傷後顯露背後的圖像。
- onMouseDragged()我用筆畫從當前的鼠標座標繪製一條線到較新的鼠標座標。我試圖在這個筆畫上設置Alphacomposite,認爲它會揭示下面的圖像。不幸的是,不過。
感謝所有幫助...
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);
}
}
你需要告訴我們你的問題是什麼。說出你想做的事實際上並沒有太多的幫助。告訴我們什麼是行不通的,哪些(如果有)異常等等。 – 2011-12-29 08:19:19
我不知道從哪裏去。我嘗試使用Alphacomposite筆畫揭開下面的圖像。我還研究了剪下下面的圖像。代碼編譯和運行,所以沒有例外。我想我真的不知道做到這一點的最佳方式,所以也許有人可以提出一些建議? – aubreybourke 2011-12-29 08:31:49