Yêu cầu: Hiển thị bộ bài 52 quân bài.
Ý tưởng: Lấy một lớp từ JPanel và ghi đè lên phương thức paintComponent. Để hiển thị được 52 quân bài, trước tiên bạn phải vẻ và hiển thị được 1 quân bài.
/******************************************************************************
* File : CardPanel.java (version 3)
* Description :
* Display a shuffled deck of playing card.
* Tested with : JDK 1.6 under Windows XP and MAC osx 10.6.6
******************************************************************************/
import java.awt.image.BufferedImage;
import java.awt.Font;
class CardPanel extends javax.swing.JPanel
{
String[] deck={
"SA","S2","S3","S4","S5","S6","S7","S8","S9","ST","SJ","SQ","SK", // spade
"HA","H2","H3","H4","H5","H6","H7","H8","H9","HT","HJ","HQ","HK", // heart
"CA","C2","C3","C4","C5","C6","C7","C8","C9","CT","CJ","CQ","CK", // club
"DA","D2","D3","D4","D5","D6","D7","D8","D9","DT","DJ","DQ","DK" // diamond
};
private BufferedImage[] cardImage=createAllImage();
public static CardPanel getInstance()
{
CardPanel panel=new CardPanel();
panel.setBackground(new java.awt.Color(255,255,128));
panel.setPreferredSize(new java.awt.Dimension(800,400));
return panel;
}
Ý tưởng: Lấy một lớp từ JPanel và ghi đè lên phương thức paintComponent. Để hiển thị được 52 quân bài, trước tiên bạn phải vẻ và hiển thị được 1 quân bài.
/******************************************************************************
* File : CardPanel.java (version 3)
* Description :
* Display a shuffled deck of playing card.
* Tested with : JDK 1.6 under Windows XP and MAC osx 10.6.6
******************************************************************************/
import java.awt.image.BufferedImage;
import java.awt.Font;
class CardPanel extends javax.swing.JPanel
{
String[] deck={
"SA","S2","S3","S4","S5","S6","S7","S8","S9","ST","SJ","SQ","SK", // spade
"HA","H2","H3","H4","H5","H6","H7","H8","H9","HT","HJ","HQ","HK", // heart
"CA","C2","C3","C4","C5","C6","C7","C8","C9","CT","CJ","CQ","CK", // club
"DA","D2","D3","D4","D5","D6","D7","D8","D9","DT","DJ","DQ","DK" // diamond
};
private BufferedImage[] cardImage=createAllImage();
public static CardPanel getInstance()
{
CardPanel panel=new CardPanel();
panel.setBackground(new java.awt.Color(255,255,128));
panel.setPreferredSize(new java.awt.Dimension(800,400));
return panel;
}
public BufferedImage[] createAllImage()
{
BufferedImage[] imageArray = new BufferedImage[52];
for (int i=0;i<52;i++)
{
imageArray[i]=createImage(deck[i]);
}
return imageArray;
}
// sample : createImage("S3");
public BufferedImage createImage(String card)
{
// create a card image.
int cardWidth=60, cardHeight=80;
BufferedImage image=new BufferedImage(cardWidth, cardHeight, BufferedImage.TYPE_INT_ARGB);
// get a graphics object of the image for drawing.
java.awt.Graphics2D gr = (java.awt.Graphics2D) image.getGraphics();
// draw a white playing card
gr.setColor(java.awt.Color.WHITE);
gr.fillRect(0,0,cardWidth,cardHeight);
// with black border
gr.setColor(java.awt.Color.BLACK);
gr.drawRect(0,0,cardWidth-1,cardHeight-1);
// draw the "three of Spade"
Font font=new Font("Dialog",Font.PLAIN, 28);
gr.setFont(font);
String prefix=card.substring(0,1); // first character
String postfix=card.substring(1,2); // second character
String suit="";
java.awt.Color color=java.awt.Color.BLACK;
if (prefix.equals("S"))
{
suit="\u2660"; // unicode for the "spade" character
}
else if (prefix.equals("H"))
{
suit="\u2665"; // unicode for the "heart" character
color=java.awt.Color.RED;
}
else if (prefix.equals("C"))
{
suit="\u2663";
}
else if (prefix.equals("D"))
{
suit="\u2666"; // unicode for the "diamond" character
color=java.awt.Color.RED;
}
String point=postfix;
int x=5;
if (postfix.equals("T"))
{
x=1;
point="10"; // special handling for "ten"
}
gr.setColor(color);
gr.drawString(suit+point,x,45);
return image;
}
// override the paint method
public void paintComponent(java.awt.Graphics graphics)
{
super.paintComponent(graphics); // paint background
// get panel dimemsion
int w=getWidth();
int h=getHeight();
// create a Graphics2D object for drawing shape
java.awt.Graphics2D gr=(java.awt.Graphics2D) graphics;
int y=0;
int x=0;
for (int i=0;i<52;i++)
{
gr.drawImage(cardImage[i],x,y,this); // draw a card
x+=61;
if ((i+1)%13==0) {y+=81; x=0;}
}
}
public static void main(String[] args) throws Exception
{
// create frame
javax.swing.JFrame frame=new javax.swing.JFrame();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setTitle("Macteki CardPanel");
// create graphics panel and add it to the frame
CardPanel panel=CardPanel.getInstance();
frame.add(panel); // panel size is 800x400, see getInstance()
frame.pack(); // this will correctly set the size of frame
// so that it is big enough to hold the panel
frame.setVisible(true);
}
}
{
BufferedImage[] imageArray = new BufferedImage[52];
for (int i=0;i<52;i++)
{
imageArray[i]=createImage(deck[i]);
}
return imageArray;
}
// sample : createImage("S3");
public BufferedImage createImage(String card)
{
// create a card image.
int cardWidth=60, cardHeight=80;
BufferedImage image=new BufferedImage(cardWidth, cardHeight, BufferedImage.TYPE_INT_ARGB);
// get a graphics object of the image for drawing.
java.awt.Graphics2D gr = (java.awt.Graphics2D) image.getGraphics();
// draw a white playing card
gr.setColor(java.awt.Color.WHITE);
gr.fillRect(0,0,cardWidth,cardHeight);
// with black border
gr.setColor(java.awt.Color.BLACK);
gr.drawRect(0,0,cardWidth-1,cardHeight-1);
// draw the "three of Spade"
Font font=new Font("Dialog",Font.PLAIN, 28);
gr.setFont(font);
String prefix=card.substring(0,1); // first character
String postfix=card.substring(1,2); // second character
String suit="";
java.awt.Color color=java.awt.Color.BLACK;
if (prefix.equals("S"))
{
suit="\u2660"; // unicode for the "spade" character
}
else if (prefix.equals("H"))
{
suit="\u2665"; // unicode for the "heart" character
color=java.awt.Color.RED;
}
else if (prefix.equals("C"))
{
suit="\u2663";
}
else if (prefix.equals("D"))
{
suit="\u2666"; // unicode for the "diamond" character
color=java.awt.Color.RED;
}
String point=postfix;
int x=5;
if (postfix.equals("T"))
{
x=1;
point="10"; // special handling for "ten"
}
gr.setColor(color);
gr.drawString(suit+point,x,45);
return image;
}
// override the paint method
public void paintComponent(java.awt.Graphics graphics)
{
super.paintComponent(graphics); // paint background
// get panel dimemsion
int w=getWidth();
int h=getHeight();
// create a Graphics2D object for drawing shape
java.awt.Graphics2D gr=(java.awt.Graphics2D) graphics;
int y=0;
int x=0;
for (int i=0;i<52;i++)
{
gr.drawImage(cardImage[i],x,y,this); // draw a card
x+=61;
if ((i+1)%13==0) {y+=81; x=0;}
}
}
public static void main(String[] args) throws Exception
{
// create frame
javax.swing.JFrame frame=new javax.swing.JFrame();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setTitle("Macteki CardPanel");
// create graphics panel and add it to the frame
CardPanel panel=CardPanel.getInstance();
frame.add(panel); // panel size is 800x400, see getInstance()
frame.pack(); // this will correctly set the size of frame
// so that it is big enough to hold the panel
frame.setVisible(true);
}
}
Một số tài liệu và khoá học bổ ích dành cho bạn:
# Tài liệu: Lập trình hướng đối tượng JAVA core dành cho người mới bắt đầu học lập trình [Click để xem]
# Khoá học online: Lập trình Java trong 4 tuần [Click để xem]