gpt4 book ai didi

java子图显示

转载 作者:行者123 更新时间:2023-11-30 09:20:14 25 4
gpt4 key购买 nike

我用纸牌写了一个游戏。而且我能够用 String 输出。但是,我想将 String 的显示更改为图像。我能够使用 JFrame 和 GridLayout 在 2*2 图像中显示四张卡片。

我的问题是我有一个 .png 的牌组,我怎样才能相应地获得单张牌?

卡片

package Chapter7;

public class Card {
Suit suit;
Rank rank;

public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}

public Rank getRank() {
return this.rank;
}

public Suit getSuit() {
return this.suit;
}

public String toString() {
return this.rank + " of " + this.suit;
}

public enum Suit {
CLUBS(1), SPADE(2), HEARTS(3), DIAMONDS(4);

int value;

Suit(int v) {
this.value = v;
}

int getSuit() {
return this.value;
}
}

public enum Rank {
ACE(1), DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13);

int rank;

Rank(int num) {
this.rank = num;
}

int getNumber() {
return this.rank;
}
}
}

Deck

package Chapter7;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Chapter7.Card.Rank;
import Chapter7.Card.Suit;

public class Deck {
List <Card> deck = new ArrayList <Card> ();

Deck() {
for(Suit s : Suit.values()) {
for(Rank r : Rank.values()) {
deck.add(new Card(s, r));
}
}
// System.out.println("A new deck created!!!");
// for(Card c : this.deck) {
// System.out.println(c.toString());
// }
}

public void shuffle(){
this.deck.clear();
Deck ret = new Deck();
for(int i = 0; i < 52; i++) {
int j = (int) (Math.random() * (52-i));
deck.add(ret.deck.remove(j));
}
// System.out.println("After shuffle!!!");
// for(Card c : this.deck) {
// System.out.println(c.toString());
// }
}

}

package Chapter7;

import java.io.IOException;



public class Game {

public static void main(String args[]) throws IOException {

Deck deck = new Deck();
deck.shuffle();
int aGame = 4;
for(int i = 0; i < 52; i++) {
if(aGame == 1) {
System.out.println(deck.deck.get(i).toString());
System.out.println("Start!!!");
while (true) {
char c = (char) System.in.read();
if (c == '\n') {
break;
}
}

aGame = 4;
// if(i == 51) {
// deck.shuffle();
// i = -1;
// }
}
else if(aGame == 4) {
System.out.println("Calculate based on the following four cards!");
System.out.println(deck.deck.get(i).toString());
aGame --;
}
else {
System.out.println(deck.deck.get(i).toString());
aGame --;
}
}
}



}

LoadImageApp

在 2*2 GridLayout 中加载图像的类。

package Chapter7;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/**
* This class demonstrates how to load an Image from an external file
*/
public class LoadImageApp extends Component {

BufferedImage img;

public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}

public LoadImageApp(String s) {
try {
img = ImageIO.read(new File(s));
} catch (IOException e) {
}

}

public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}

public static void main(String[] args) {

JFrame f = new JFrame("Load Image Sample");
GridLayout grid = new GridLayout(2, 2);
f.setLayout(grid);

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

f.add(new LoadImageApp("27.jpg"));
f.add(new LoadImageApp("hw6.jpg"));
f.add(new LoadImageApp("27.jpg"));
f.add(new LoadImageApp("hw6.jpg"));
f.pack();
f.setVisible(true);
}
}

我应该如何改变这两个类来显示?

带有纸牌的 .png 在这里。

http://math.hws.edu/javanotes/c13/s1.html

最佳答案

I have a .png for the deck of cards, how can I get individual cards accordingly?

使用 subImage(..) 方法,详见 this answer .

那是将一张图片分成 9 个子图片。 4 个用于按钮(带有用于按下状态的备用“红色边框”图标),5 个用于标签。

关于java子图显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17517703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com