문제 설명
스크래치 및 승리 (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:
In the screenshot below, the yellow background color is visible, so the SimpleDrawPad
component is transparent:
(by Delfa、Freek de Bruijn)