[Java] Chương trình hiển thị bộ bài 52 quân bài [GUI]

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

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

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]

Categories

AI (13) AI programming (1) ASP (1) Android (31) App Honeygain (4) Assembly (17) Biểu diễn thuật toán (1) Bubble-Sort (1) Bài giảng (2) Bài giảng lập trình C và Cpp (21) Bài viết hay (108) Bản đồ tư duy (1) C Plus Plus (103) C/C++ (16) CDSL phân tán (1) CSS (2) Cơ sở dữ liệu (11) Danh ngôn lập trình (1) Datamining (4) Genetic Algorithm (1) Giáo trình (2) Giải thuật tiến hóa - thuật toán di truyền (2) Google App Engine (2) Góc học tập (34) HTML (1) Hướng dẫn kiếm tiền online tại nhà (6) Hướng dẫn sử dụng Emu8086 (1) Học lập trình (131) Học lập trình C và CPP qua ví dụ (17) Java (54) Java Căn bản (6) JavaScript (5) Kỹ năng đọc hiệu quả (1) Kỹ thuật lập trình (16) Kỹ thuật đồ họa máy tính (10) Lý thuyết Cơ sở dữ liệu (2) Lý thuyết đồ thị (11) Lập trình Cơ sở dữ liệu (2) Lập trình Python (2) Lập trình căn bản (8) Lập trình hướng đối tượng với Java (7) Lập trình mobile (7) Lập trình mạng (6) Lập trình nhúng (1) Lập trình trí tuệ nhân tạo (2) ML (1) MMO (6) MS Access (1) Machine learning (2) Mạng máy tính (1) Mẹo tìm kiếm trên Google (1) Nghiên cứu khoa học (3) Ngôn ngữ lập trình (2) Những cuốn sách hay mà bạn nên đọc khi còn trẻ (1) Pascal (3) Phương pháp tính toán tối ưu (2) Phương pháp tối ưu (2) Quản lý dự án CNTT (1) SEO (1) SQL (5) Swift (9) Sách hay (4) Thiết kế Web (2) Thuật toán (51) Thuật toán Sắp Xếp -Sort (9) Thuật toán Tìm kiếm - Search (5) Thuật toán di truyền (4) Thực hành Android (2) Tin học văn phòng (5) Tiện ích máy tính (3) Toán rời rạc (13) Treo máy kiếm tiền (3) Trí tuệ nhân tạo (18) Tài liệu tham khảo (4) Tìm hiểu Blockchain (2) Tự học Android (3) Tự học Android qua ví dụ (1) Tự học JavaScript (1) Tự học lập trình (7) Tự học lập trình Android (17) Tự học lập trình C và CPP (14) Tự học lập trình java qua các ví dụ (8) XML (1) blockchain (2) bài giảng quản lý dự án CNTT (1) bài tập java (3) bài tập lập trình (4) cấu trúc dữ liệu giải thuật (15) hướng dẫn viết báo (1) học lập trình Java (11) học máy (5) hợp ngữ (8) lập trình viên (3) phưng pháp đơn hình (2) thuật toán AI (2) tài liệu CNTT miễn phí (3) tính toán tối ưu (1) tự học lập trình iOS (8) tự học lập trình python (1) ví dụ Assembly (1) Đại số gia tử và ứng dụng (1) Đồ họa (4)