스크래치 및 승리 (Scratch and win)


문제 설명

스크래치 및 승리 (Scratch and win)

저는 고등학교 4학년에 컴퓨터 공학을 전공하는 학생입니다. CREATE A GRATTA AND WIN 프로젝트를 시작해야 합니다. 코드의 이 부분을 어떻게 수정합니까? card_surface 변수가 색상이 아닌 이미지인지 확인해야 하지만 어떻게 해야 하는지 모르겠습니다.

card_surface를 수정하려고 했습니다. 변수, 이미지를 할당하지만 이 경우에는 작동하지 않습니다.

그런 다음 그 자리를 대신할 다른 이미지 유형의 변수를 만들려고 했습니다. card_surface의 경우, 이 경우 이미지가 있지만 투명해지지 않습니다.

class DrawPad extends JComponent {
BufferedImage image;
BufferedImage card_surface;

Graphics2D graphics2D;
int currentX, currentY, oldX, oldY;
 public void clear() {
    image=null;          
    card_surface=null;
    repaint();
}


  public void paintComponent(Graphics g) {
    String imagePath;
    int numero= Main.numero;
    if (image == null) {

        image = new BufferedImage(500, 300, BufferedImage.TYPE_INT_ARGB);
        if(numero<=7) {
            imagePath = "HAI VINTO.PNG";
            File inFile = new File(imagePath);
            System.out.println("vinto");

            try{
                image = ImageIO.read(inFile);
            }catch(java.io.IOException e){System.out.println(e);}
        }
        else{
            imagePath = "HAI PERSO.PNG";
            File inFile = new File(imagePath);

            try{
                image = ImageIO.read(inFile);
            }catch(java.io.IOException e){System.out.println(e);}
        }

        graphics2D = image.createGraphics();

        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        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.black);
        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);
}

}


참조 솔루션

방법 1:

As already mentioned by MadProgrammer and Ludwig Schulze, a simplified and working version of your code is very helpful for all the enthusiastic people here at Stack Overflow who want to help you. In an attempt to help you to create a so‑called minimal reproducible example, I've taken part of your code (that hopefully contains your issue) and added a simple Main class (so we can execute the code). You could compare this code to your project and/or use this code as a starting point.

The Main class:

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class Main {
    public static void main(String[] arguments) {
        new Main().createGuiAndLaunch();
    }

    private void createGuiAndLaunch() {
        JFrame frame = new JFrame("Test");
        frame.setBounds(100, 100, 800, 600);

        frame.getContentPane().setBackground(Color.YELLOW);

        // Image from https://ya‑webdesign.com/transparent250_/52‑playing‑cards‑png.png
        String imagePath = "input/q60141219/52‑playing‑cards‑png‑transparent.png";
        frame.getContentPane().add(new SimpleDrawPad(imagePath));

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

A SimpleDrawPad class:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;

public class SimpleDrawPad extends JComponent {
    private BufferedImage image;

    public SimpleDrawPad(String imagePath) {
        try {
            image = ImageIO.read(new File(imagePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(image, 0, 0, null);
    }
}

The "playing‑card‑6‑clubs‑transparent.png" image used in the code:

playing card 6 clubs transparent

In the screenshot below, the yellow background color is visible, so the SimpleDrawPad component is transparent:

screenshot with transparent component

(by DelfaFreek de Bruijn)

참조 문서

  1. Scratch and win (CC BY‑SA 2.5/3.0/4.0)

#graphics #swing #java






관련 질문

Triangulate a quad with a hole in it using tessellation (Triangulate a quad with a hole in it using tessellation)

기존 이미지가 화면에 그려지지 않는 이유는 무엇입니까? (Why won't my existing image draw to the screen?)

barplot()에서 정보를 저장하고 그래프를 표시하지 않는 방법 (How to save information from barplot() and not displaying the graph)

Java 시간에 따라 사각형을 이동하는 방법 (java how to make a rectangle move by time)

큐브맵을 2D 텍스처로 투영 (Project cubemap to 2D texture)

OpenGL & Qt : 마우스 오버 시 레이블 표시 (OpenGL & Qt : display a label on the mouseover)

프리파스칼에서 기능적 그래픽을 얻으려면 어떻게 해야 합니까? (How do I get functional graphics from freepascal?)

스크래치 및 승리 (Scratch and win)

벡터 병합 알고리즘 도움말 (Help with algorithm for merging vectors)

WPF - ScreenSaver 그래픽 성능 향상 (WPF - ScreenSaver graphics performance improvements)

Java, 패널용 그래픽 개체 생성 및 편집? (Java, create and edit a Graphics Object for Panel?)

정점 셰이더의 out 변수는 어떻게 보간됩니까? (How are the out variables of vertex shader interpolated?)







코멘트