Java生成扑克牌
扑克牌的花色模板图片
然后我们通过代码生成
下面是代码
maker.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class maker {
public static RECT hot=new RECT(144,240,236,348);
public static RECT mh=new RECT(135,366,236,488);
public static RECT ht=new RECT(23,368,115,476);
public static RECT fk=new RECT(18,235,119,355);
public static RECT pai=new RECT(3,2,158,233);
public static RECT dw=new RECT(211,27,237,199);
public static RECT xw=new RECT(257,29,283,201);
public static RECT J[]=new RECT[] {new RECT(293,6,413,200),new RECT(417,7,536,199),new RECT(540,7,656,200),new RECT(780,210,898,401)};
public static RECT Q[]=new RECT[] {new RECT(660,7,779,200),new RECT(782,6,900,201),new RECT(294,209,412,400),new RECT(295,409,411,600)};
public static RECT K[]=new RECT[] {new RECT(416,209,536,402),new RECT(539,210,658,400),new RECT(660,211,778,400),new RECT(419,410,532,599)};
BufferedImage modimg;
public static int Width=350,Height=450;
public maker(String mainpath)
{
try {
modimg=ImageIO.read(new File(mainpath));
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public BufferedImage Make(int num)
{
int W=Width/4,H=Height/4;
BufferedImage out=new BufferedImage(Width,Height,BufferedImage.TYPE_INT_ARGB);
Graphics g=out.getGraphics();
g.drawImage(modimg, 0, 0, Width, Height, pai.left,pai.top, pai.right, pai.bottom, null);
g.setFont(new Font("微软雅黑", Font.BOLD, W));
RECT hua=null;
if(num==52)
hua=xw;
else if(num==53)
hua=dw;
else
{
int type=num/13;
switch(type)
{
case 0:
hua=hot;
g.setColor(Color.RED);
break;
case 1:
hua=fk;
g.setColor(Color.RED);
break;
case 2:
hua=ht;
g.setColor(Color.BLACK);
break;
case 3:
hua=mh;
g.setColor(Color.BLACK);
break;
}
int r=num%13;
if(r==10)
{
g.drawString("J", 20, H);
}
else if(r==11)
{
g.drawString("Q", 20, H);
}
else if(r==12)
{
g.drawString("K", 20, H);
}
else
{
if(r==0)
{
g.drawString("A", 20, H);
}
else
{
g.drawString(""+(r+1), 20, H);
}
}
int sw=Width/2;
int sh=Height-25;
g.drawImage(modimg, 20, H+20, 20+W-20, H+W-10, hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg, sw, Height-sw, Width-25,sh, hua.left, hua.top, hua.right, hua.bottom, null);
return out;
}
g.drawImage(modimg, 20, 20, W,(int)( Height*0.6f), hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg, Width-W, Height-(int)( Height*0.6f), Width-20, Height-20, hua.right, hua.bottom, hua.left, hua.top, null);
return out;
}
public BufferedImage Make(int num,int fuck)
{
BufferedImage out=new BufferedImage(Width,Height,BufferedImage.TYPE_INT_ARGB);
Graphics g=out.getGraphics();
g.drawImage(modimg, 0, 0, Width, Height, pai.left,pai.top, pai.right, pai.bottom, null);
g.setFont(new Font("微软雅黑", Font.BOLD, 36));
RECT hua=null;
if(num==52)
hua=xw;
else if(num==53)
hua=dw;
else
{
int type=num/13;
switch(type)
{
case 0:
hua=hot;
g.setColor(Color.RED);
break;
case 1:
hua=fk;
g.setColor(Color.RED);
break;
case 2:
hua=ht;
g.setColor(Color.BLACK);
break;
case 3:
hua=mh;
g.setColor(Color.BLACK);
break;
}
int r=num%13;
if(r==10)
{
g.drawString("J", 20, 40);
g.drawImage(out, Width-70, Height-45, Width-20, Height-4, 70, 45,20, 4, null);
g.drawImage(modimg, 50, 30, Width-50, Height-30, J[type].left, J[type].top, J[type].right, J[type].bottom, null);
}
else if(r==11)
{
g.drawString("Q", 20, 40);
g.drawImage(out, Width-70, Height-45, Width-20, Height-4, 70, 45,20, 4, null);
g.drawImage(modimg, 50, 30, Width-50, Height-30, Q[type].left, Q[type].top, Q[type].right, Q[type].bottom, null);
}
else if(r==12)
{
g.drawString("K", 20, 40);
g.drawImage(out, Width-70, Height-45, Width-20, Height-4, 70, 45,20, 4, null);
g.drawImage(modimg, 50, 30, Width-50, Height-30, K[type].left, K[type].top, K[type].right, K[type].bottom, null);
}
else
{
if(r==0)
{
g.drawString("A", 20, 40);
g.drawImage(out, Width-70, Height-45, Width-20, Height-4, 70, 45,20, 4, null);
int w=Width/6;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-w,sh-w,sw+w,sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
}
else
{
g.drawString(""+(r+1), 20, 40);
g.drawImage(out, Width-70, Height-45, Width-20, Height-4, 70, 45,20, 4, null);
switch(r)
{
case 1:
{
int w=Width/10;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-w,sh-(4*w),sw+w,sh-(2*w),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-w,sh+(2*w),sw+w,sh+(4*w),hua.left, hua.top, hua.right, hua.bottom, null);
}
break;
case 2:
{
int w=Width/10;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-w,sh-w,sw+w,sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-w,sh-(4*w),sw+w,sh-(2*w),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-w,sh+(2*w),sw+w,sh+(4*w),hua.left, hua.top, hua.right, hua.bottom, null);
}
break;
case 3:
{
int w=Width/10;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-(3*w),sh-(5*w)-(w/2),sw-w,sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-(5*w)-(w/2),sw+(3*w),sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh+(3*w)+(w/2),sw-w,sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh+(3*w)+(w/2),sw+(3*w),sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
}
break;
case 4:
{
int w=Width/10;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-w,sh-w,sw+w,sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-(5*w)-(w/2),sw-w,sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-(5*w)-(w/2),sw+(3*w),sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh+(3*w)+(w/2),sw-w,sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh+(3*w)+(w/2),sw+(3*w),sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
}
break;
case 5:
{
int w=Width/10;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-(3*w),sh-w,sw-w,sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-w,sw+(3*w),sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-(5*w)-(w/2),sw-w,sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-(5*w)-(w/2),sw+(3*w),sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh+(3*w)+(w/2),sw-w,sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh+(3*w)+(w/2),sw+(3*w),sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
}
break;
case 6:
{
int w=Width/10;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-w,sh-(3*w),sw+w,sh-w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-w,sw-w,sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-w,sw+(3*w),sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-(5*w)-(w/2),sw-w,sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-(5*w)-(w/2),sw+(3*w),sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh+(3*w)+(w/2),sw-w,sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh+(3*w)+(w/2),sw+(3*w),sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
}
break;
case 7:
{
int w=Width/10;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-w,sh-(3*w),sw+w,sh-w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-w,sh+w,sw+w,sh+(3*w),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-w,sw-w,sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-w,sw+(3*w),sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-(5*w)-(w/2),sw-w,sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-(5*w)-(w/2),sw+(3*w),sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh+(3*w)+(w/2),sw-w,sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh+(3*w)+(w/2),sw+(3*w),sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
}
break;
case 8:
{
int w=Width/10;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-w,sh-w,sw+w,sh+w,hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-(2*w+(w/2)),sw-w,sh-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-(2*w+(w/2)),sw+(3*w),sh-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh+(w/2),sw-w,sh+(2*w+(w/2)),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh+(w/2),sw+(3*w),sh+(2*w+(w/2)),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-(5*w)-(w/2),sw-w,sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-(5*w)-(w/2),sw+(3*w),sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh+(3*w)+(w/2),sw-w,sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh+(3*w)+(w/2),sw+(3*w),sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
}
break;
case 9:
{
int w=Width/10;
int sw=Width/2;
int sh=Height/2;
g.drawImage(modimg,sw-w,sh-(4*w),sw+w,sh-(2*w),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-w,sh+(2*w),sw+w,sh+(4*w),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-(2*w+(w/2)),sw-w,sh-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-(2*w+(w/2)),sw+(3*w),sh-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh+(w/2),sw-w,sh+(2*w+(w/2)),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh+(w/2),sw+(3*w),sh+(2*w+(w/2)),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh-(5*w)-(w/2),sw-w,sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh-(5*w)-(w/2),sw+(3*w),sh-(3*w)-(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw-(3*w),sh+(3*w)+(w/2),sw-w,sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg,sw+w,sh+(3*w)+(w/2),sw+(3*w),sh+(5*w)+(w/2),hua.left, hua.top, hua.right, hua.bottom, null);
}
break;
}
}
//g.drawImage(modimg, 47, 85, 107, 145, hua.left, hua.top, hua.right, hua.bottom, null);
}
g.drawImage(modimg, 25, 50, 50, 75, hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg, Width-50, Height-75, Width-25, Height-50, hua.right, hua.bottom, hua.left, hua.top, null);
return out;
}
g.drawImage(modimg, 10, 10, hua.width+10, hua.height-40, hua.left, hua.top, hua.right, hua.bottom, null);
g.drawImage(modimg, Width-hua.width-10, Height-hua.height+40, Width-10, Height-10, hua.right, hua.bottom, hua.left, hua.top, null);
return out;
}
}
RECT.java
public class RECT {
public int top,left,right,bottom;
public int width,height;
public RECT(int left,int top,int right,int bottom)
{
this.top=top;
this.bottom=bottom;
this.left=left;
this.right=right;
this.width=right-left;
this.height=bottom-top;
}
}
成果演示