Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6992721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:40:17+00:00 2026-05-27T19:40:17+00:00

Building a lottery scratch-card app in Java. Have allot of it done, just need

  • 0

Building a lottery scratch-card app in Java. Have allot of it done, just need help with the scratching functionality.

Basically the program works like this:

  1. create image for the background (right now its just a white background, but later would obviously be an scratchcard image with lotto symbols)
  2. create the card_surface which is just a green layer than should reveal the image behind when scratched.
  3. onMouseDragged() I used a stroke to draw a line from current mouse co-ordinates to newer mouse-cordinates. I have tried setting the Alphacomposite on this stroke thinking it would reveal the image underneath. Unfortunately not though.

Appreciate any help…

 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);

  }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:40:17+00:00Added an answer on May 27, 2026 at 7:40 pm

    What you are trying to do is to reveal the underlying image when the user ‘scratches’ the top image, correct?

    You actually need two images here : First, the original image and secondly, a blank image with a gray background that goes over the first image, hiding it. When the user ‘scratches’ the top image, you need to draw with a transparent colour OVER the gray image, revealing the underlying image.

    So the steps are:

    Draw original image with blank over it
    When user ‘scratches’ draw with a transparent colour on the top image
    Repaint the images, with the top image now having transparent sections
    Repeat

    It sounds complicated, but I don’t think it will be too hard, you just need to keep the two images apart and paint one over the other using a buffer. See this post How to make a color transparent in a BufferedImage and save as PNG for information on working with transparent buffered images.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building an MVC 3 app with Razor and I have some information persisted in
Iam building a custom rating bar. With help of kozyr's article i have created
Building a tribunary tree in Java and my delete functionality doesn't seem to be
Building a simple asp.net web app just to test things out (will obviously refactor
Building an offline web app in VS2010. I have a master page with a
Building a Shopping Cart app. Some products have options, some don't. I visitor can
Building an inventory system. I have lots of products and each product has three
Building a Django app on a VPS. I am not very experienced with setting
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
Building a sample ASP.NET MVC app. Using the Membership API for authentication. For whatever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.