BufferedImage bi = new BufferedImage(huatan.hb.getWidth(), huatan.hb.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();huatan.hb.paint(g2d);ImageIO.write(bi, “PNG”, new File(“frame.png”));
详细代码
package huatan;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class huatan extends JFrame {
static huaman hb = null;
public static void main(String[] args) throws IOException {
huatan yhh = new huatan();
}
public huatan() throws IOException {
hb = new huaman();
this.add(hb);
this.addKeyListener(hb);
this.setSize(800, 800);
this.setLocation(300, 280);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
System.out.println(
"1,add flowerbed 2,remove flowerbed 3,add flower to flowerbed 4, remove flower from flowerbed 5save and exit ");
Thread c = new Thread(hb);
c.start();
}
}
class huaman extends JPanel implements KeyListener, Runnable {
Vector<blossomfu> vector = new Vector<blossomfu>();
StringBuffer str = new StringBuffer();
blossomfu bloss = new blossomfu();
boolean addFlowerbed = false;
boolean removeFlowerbed = false;
boolean addFlowerToFlowerbed = false, addFlowerToFlowerbed1 = false, addFlowerToFlowerbed2 = false;
boolean removeFlowerFromFlowerbed = false;
boolean get1 = false;
boolean get2 = false;
boolean get3 = false;
boolean get4 = false;
boolean a = true;
boolean addhua = false;
int xx1 = 120, yy1 = 120, xx2 = 120, yy2 = 120, xx3 = 120, yy3 = 120, xx4 = 120, yy4 = 120;
public void paint(Graphics g) {
super.paint(g);
Image tp = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/grass1.png"));
g.drawImage(tp, 0, 0, 800, 800, this);
if (get1) {
add1(g, xx1, yy1);
}
if (get2) {
add2(g, xx2, yy2);
}
if (get3) {
add3(g, xx3, yy3);
}
if (get4) {
add4(g, xx4, yy4);
}
addz(g);
}
public void addz(Graphics g) {
int x1 = 5;
int y1 = 5;
int x2 = 5;
int y2 = 5;
int x3 = 705;
int y3 = 5;
int x4 = 705;
int y4 = 5;
int x5 = 5;
int y5 = 705;
int x6 = 5;
int y6 = 705;
int x7 = 705;
int y7 = 705;
int x8 = 705;
int y8 = 705;
int z = 0;
int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
boolean b1 = true;
boolean b2 = true;
boolean b3 = true;
boolean b4 = true;
boolean b5 = true;
boolean b6 = true;
boolean b7 = true;
boolean b8 = true;
int s1 = 15, s2 = 15, s3 = 15, s4 = 15, s5 = 15, s6 = 15, s7 = 15, s8 = 15;
for (blossomfu blos : vector) {
// System.out.println(blos.toString());
// Image tp =
// Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(blos.url));
// g.drawImage(tp, 0, 0, 800, 800, this);
// System.out.println();
z++;
if (blos.huatan == 1) {
if (blos.zh == 1) {
Image tp = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(blos.url));
g.drawImage(tp, x1, y1, 20, 100, this);
g.setColor(Color.RED);
g.setFont(new Font("幼圆", Font.BOLD, 10));
g.drawString(String.valueOf(z), x1 + 10, y1 + 10);
x1 += 20;
a1++;
if (a1 >= s1) {
y1 += 100;
s1 += 15;
x1 = 5;
}
}
if (blos.zh == 2) {
Image tp = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(blos.url));
g.drawImage(tp, x2, y2, 100, 20, this);
g.setColor(Color.RED);
g.setFont(new Font("幼圆", Font.BOLD, 10));
g.drawString(String.valueOf(z), x2 + 10, y2 + 10);
y2 += 20;
a2++;
if (a2 >= s2) {
x2 += 100;
s2 += 15;
y2 = 5;
}
}
}
if (blos.huatan == 2) {
if (blos.zh == 1) {
Image tp = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(blos.url));
g.drawImage(tp, x3, y3, 20, 100, this);
g.setColor(Color.RED);
g.setFont(new Font("幼圆", Font.BOLD, 10));
g.drawString(String.valueOf(z), x3 + 10, y3 + 10);
a3++;
x3 += 20;
if (a3 >= s3) {
y3 += 100;
s3 += 15;
x3 = 705;
}
}
if (blos.zh == 2) {
Image tp = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(blos.url));
g.drawImage(tp, x4, y4, 100, 20, this);
g.setColor(Color.RED);
g.setFont(new Font("幼圆", Font.BOLD, 10));
g.drawString(String.valueOf(z), x4 + 10, y4 + 10);
a4++;
y4 += 20;
if (a4 >= s4) {
x4 += 100;
s4 += 15;
y4 = 5;
}
}
}
if (blos.huatan == 3) {
if (blos.zh == 1) {
Image tp = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(blos.url));
g.drawImage(tp, x5, y5, 20, 100, this);
g.setColor(Color.RED);
g.setFont(new Font("幼圆", Font.BOLD, 10));
g.drawString(String.valueOf(z), x5 + 10, y5 + 10);
a5++;
x5 += 20;
if (a5 >= s5) {
y5 += 100;
s5 += 15;
x5 = 5;
}
}
if (blos.zh == 2) {
Image tp = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(blos.url));
g.drawImage(tp, x6, y6, 100, 20, this);
g.setColor(Color.RED);
g.setFont(new Font("幼圆", Font.BOLD, 10));
g.drawString(String.valueOf(z), x6 + 10, y6 + 10);
a6++;
y6 += 20;
if (a6 >= s6) {
x6 += 100;
s6 += 15;
y6 = 705;
}
}
}
if (blos.huatan == 4) {
if (blos.zh == 1) {
Image tp = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(blos.url));
g.drawImage(tp, x7, y7, 20, 100, this);
g.setColor(Color.RED);
g.setFont(new Font("幼圆", Font.BOLD, 10));
g.drawString(String.valueOf(z), x7 + 10, y7 + 10);
a7++;
x7 += 20;
if (a7 >= s7) {
y7 += 100;
s7 += 15;
x7 = 705;
}
}
if (blos.zh == 2) {
Image tp = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(blos.url));
g.drawImage(tp, x8, y8, 100, 20, this);
g.setColor(Color.RED);
g.setFont(new Font("幼圆", Font.BOLD, 10));
g.drawString(String.valueOf(z), x8 + 10, y8 + 10);
a8++;
y8 += 20;
if (a8 >= s8) {
x8 += 100;
s8 += 15;
y8 = 705;
}
}
}
}
}
public void add1(Graphics g, int xx, int yy) {
Image tp1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/未标题-1.png"));
g.drawImage(tp1, 0, 0, xx, yy, this);
}
public void add2(Graphics g, int xx, int yy) {
Image tp2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/未标题-2.png"));
g.drawImage(tp2, 700, 0, xx, yy, this);
}
public void add3(Graphics g, int xx, int yy) {
Image tp3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/未标题-3.png"));
g.drawImage(tp3, 0, 700, xx, yy, this);
}
public void add4(Graphics g, int xx, int yy) {
Image tp4 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/未标题-4.png"));
g.drawImage(tp4, 700, 700, xx, yy, this);
}
@Override
public void run() {
int s1 = 15, s2 = 15, s3 = 15, s4 = 15, s5 = 15, s6 = 15, s7 = 15, s8 = 15;
while (true) {
// int xx1 = 120, yy1 = 120, xx2 = 120, yy2 = 120, xx3 = 120, yy3 = 120, xx4 =
// 120, yy4 = 120;
// System.out.println("线程启动");
try {
Thread.sleep(500);
int in1 = 0, in2 = 0, in3 = 0, in4 = 0, in5 = 0, in6 = 0, in7 = 0, in8 = 0;// 用于判断是否增长
for (blossomfu blossomfu : vector) {
if (blossomfu.huatan == 1 && blossomfu.zh == 1) {
in1++;
xx1 =xx1>=( in1 > 4 ? ((in1 - 4) * 20 + 120)>=300?310:(in1 - 4) * 20 + 120: 120)?xx1:in1 > 4 ? ((in1 - 4) * 20 + 120)>=300?310:(in1 - 4) * 20 + 120: 120;
if ( in1 > s1) {
s1+=15;
if(yy1>(in1/15*100+105)) {
}else {
yy1+=100;
}
}
} else if (blossomfu.huatan == 1 && blossomfu.zh == 2) {
in2++;
yy1 =yy1>=(in2 > 4 ? ((in2 - 4) * 20 + 120)>=300?310:((in2 - 4) * 20 + 120) : 120)?yy1:in2 > 4 ? ((in2 - 4) * 20 + 120)>=300?310:((in2 - 4) * 20 + 120) : 120;
if ( in2 > s2) {
s2+=15;
if(xx1>(in2/15*100+105)) {
}else {
xx1+=100;
}
}
} else if (blossomfu.huatan == 2 && blossomfu.zh == 1) {
in3++;
xx2 =xx2>=(in3 > 4 ? ((in3 - 4) * 20 + 120)>=300?310:((in3 - 4) * 20 + 120) : 120)?xx2:in3 > 4 ? ((in3 - 4) * 20 + 120)>=300?310:((in3 - 4) * 20 + 120) : 120;
if ( in3 > s3) {
s3+=15;
if(yy2>(in3/15*100+105)) {
}else {
yy2+=100;
}
}
} else if (blossomfu.huatan == 2 && blossomfu.zh == 2) {
in4++;
yy2 =yy2>=(in4 > 4 ? ((in4 - 4) * 20 + 120)>=300?310:((in4 - 4) * 20 + 120) : 120)?yy2:in4 > 4 ? ((in4 - 4) * 20 + 120)>=300?310:((in4 - 4) * 20 + 120) : 120;
if ( in4 > s4) {
s4+=15;
if(xx2>(in4/15*100+105)) {
}else {
xx2+=100;
}
}
} else if (blossomfu.huatan == 3 && blossomfu.zh == 1) {
in5++;
xx3 =xx3>=(in5 > 4 ? ((in5 - 4) * 20 + 120)>=300?310:((in5 - 4) * 20 + 120) : 120)?xx3:in5 > 4 ? ((in5 - 4) * 20 + 120)>=300?310:((in5 - 4) * 20 + 120) : 120;
if ( in5 > s5) {
s5+=15;
if(yy3>(in5/15*100+105)) {
}else {
yy3+=100;
}
}
} else if (blossomfu.huatan == 3 && blossomfu.zh == 2) {
in6++;
yy3 =yy3>=(in6 > 4 ? ((in6 - 4) * 20 + 120)>=300?310:((in6 - 4) * 20 + 120) : 120)?yy3:in6 > 4 ? ((in6 - 4) * 20 + 120)>=300?310:((in6 - 4) * 20 + 120) : 120;
if ( in6 > s6) {
s6+=15;
if(xx3>(in6/15*100+105)) {
}else {
xx3+=100;
}
}
} else if (blossomfu.huatan == 4 && blossomfu.zh == 1) {
in7++;
xx4 =xx4>=(in7 > 4 ? ((in7 - 4) * 20 + 120)>=300?310:((in7 - 4) * 20 + 120) : 120)?xx4:in7 > 4 ? ((in7 - 4) * 20 + 120)>=300?310:((in7 - 4) * 20 + 120) : 120;
if ( in7 > s7) {
s7+=15;
if(yy4>(in7/15*100+105)) {
}else {
yy4+=100;
}
}
} else if (blossomfu.huatan == 4 && blossomfu.zh == 2) {
in8++;
yy4 =yy4>=(in8 > 4 ? ((in8 - 4) * 20 + 120)>=300?310:((in8 - 4) * 20 + 120) : 120)?yy4:in8 > 4 ? ((in8 - 4) * 20 + 120)>=300?310:((in8 - 4) * 20 + 120) : 120;
if ( in8 > s8) {
s8+=15;
if(xx4>(in8/15*100+105)) {
}else {
xx4+=100;
}
}
}
}
this.repaint();
BufferedImage bi = new BufferedImage(huatan.hb.getWidth(), huatan.hb.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();huatan.hb.paint(g2d);ImageIO.write(bi, "PNG", new File("frame.png"));
} catch (Exception e) {
// TODO: handle exception
}
}
}
@Override
public void keyTyped(KeyEvent e) {
}
StringBuffer str1 = new StringBuffer();
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyChar());
int judeg = 0;// 判断
if (addFlowerbed) {
switch (String.valueOf(e.getKeyChar())) {
case "1":
if (get1) {
System.out.println("花坛已存在");
} else {
System.out.println("添加花坛成功");
get1 = true;
}
break;
case "2":
if (get2) {
System.out.println("花坛已 存在");
} else {
System.out.println("添加花坛成功");
get2 = true;
}
break;
case "3":
if (get3) {
System.out.println("花坛已存在");
} else {
System.out.println("添加花坛成功");
get3 = true;
}
break;
case "4":
if (get4) {
System.out.println("花坛已存在");
} else {
System.out.println("添加花坛成功");
get4 = true;
}
break;
case "5":
System.out.println(
"1,add flowerbed 2,remove flowerbed 3,add flower to flowerbed 4, remove flower from flowerbed 5save and exit ");
judeg = 1;
addFlowerbed = false;
a = true;
break;
}
}
if (a && judeg == 0) {
switch (String.valueOf(e.getKeyChar())) {
case "1":
System.out.println("请选择你要添加的花坛位置:1左上 2右上 3左下 4右下 5退出");
addFlowerbed = true;
a = false;
judeg = 1;
break;
case "2":
System.out.println("请选择你要删除的花坛位置:1左上 2右上 3左下 4右下 5退出");
removeFlowerbed = true;
a = false;
judeg = 1;
break;
case "3":
//
System.out.println("请选择你要种花的花坛:1左上 2右上 3左下 4右下 5退出");
addFlowerToFlowerbed = true;
a = false;
judeg = 1;
break;
case "4":
removeFlowerFromFlowerbed = true;
System.out.println("请选择你要删除的花的序号,回家键确认输入,f退出");
a = false;
judeg = 1;
break;
case "5":
// 这里做保存数据
String excelName="F:\\aaa.xls";//相对路径
List<String> titles =new ArrayList<String>();
titles.add("1");
List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
Map<String, Object> map1=new HashMap<String, Object>();
map1.put("1", 123);
//excel.exportExcelToFile(titles, list, excelName, "huatan");
System.exit(-1);
break;
default:
}
}
// 花坛 //花 //序列
if (removeFlowerbed && judeg == 0) {
switch (String.valueOf(e.getKeyChar())) {
case "1":
if (!get1) {
System.out.println("花坛已存在");
} else {
System.out.println("删除花坛成功");
get1 = false;
}
break;
case "2":
if (!get2) {
System.out.println("花坛已存在");
} else {
System.out.println("删除花坛成功");
get2 = false;
}
break;
case "3":
if (!get3) {
System.out.println("花坛已存在");
} else {
System.out.println("删除花坛成功");
get3 = false;
}
break;
case "4":
if (!get4) {
System.out.println("花坛已存在");
} else {
System.out.println("删除花坛成功");
get4 = false;
}
break;
case "5":
System.out.println(
"1,add flowerbed 2,remove flowerbed 3,add flower to flowerbed 4, remove flower from flowerbed 5save and exit ");
judeg = 1;
removeFlowerbed = false;
a = true;
break;
}
}
if (addFlowerToFlowerbed && judeg == 0) {
switch (String.valueOf(e.getKeyChar())) {
case "1":
if (!get1) {
System.out.println("您选择的花坛不存在");
} else {
System.out.println("你选择了左上花坛");
bloss.returnStr();
bloss.huatan = 1;
addFlowerToFlowerbed1 = true;
addFlowerToFlowerbed = false;
judeg = 1;
}
break;
case "2":
if (!get2) {
System.out.println("您选择的花坛不存在");
} else {
System.out.println("你选择了右上花坛");
bloss.returnStr();
bloss.huatan = 2;
addFlowerToFlowerbed1 = true;
addFlowerToFlowerbed = false;
judeg = 1;
}
break;
case "3":
if (!get3) {
System.out.println("您选择的花坛不存在");
} else {
System.out.println("你选择了左下花坛");
bloss.returnStr();
bloss.huatan = 3;
addFlowerToFlowerbed1 = true;
addFlowerToFlowerbed = false;
judeg = 1;
}
break;
case "4":
if (!get4) {
System.out.println("您选择的花坛不存在");
} else {
bloss.huatan = 4;
System.out.println("你选择了右下花坛");
bloss.returnStr();
addFlowerToFlowerbed1 = true;
addFlowerToFlowerbed = false;
judeg = 1;
}
break;
case "5":
System.out.println(
"1,add flowerbed 2,remove flowerbed 3,add flower to flowerbed 4, remove flower from flowerbed 5save and exit ");
judeg = 1;
addFlowerToFlowerbed = false;
a = true;
break;
}
}
if (addFlowerToFlowerbed1 && judeg == 0) {
switch (String.valueOf(e.getKeyChar())) {
case "1":
System.out.println("你选择黄花");
addFlowerToFlowerbed2 = true;
addFlowerToFlowerbed1 = false;
bloss.url = bloss.ju().get(0).getUrl();
System.out.println("请选择 方法 1横 2纵 3退出");
judeg = 1;
break;
case "2":
System.out.println("你选择白花");
addFlowerToFlowerbed2 = true;
addFlowerToFlowerbed1 = false;
bloss.url = bloss.ju().get(1).getUrl();
System.out.println("请选择 方法 1横 2纵 3退出");
judeg = 1;
break;
case "3":
System.out.println("你选择蓝花");
addFlowerToFlowerbed2 = true;
addFlowerToFlowerbed1 = false;
bloss.url = bloss.ju().get(2).getUrl();
System.out.println("请选择 方法 1横 2纵 3退出");
judeg = 1;
break;
case "4":
System.out.println("你选择红花 ");
addFlowerToFlowerbed2 = true;
addFlowerToFlowerbed1 = false;
bloss.url = bloss.ju().get(3).getUrl();
System.out.println("请选择 方法 1横 2纵 3退出");
judeg = 1;
break;
case "i":
judeg = 1;
System.out.println(
"1,add flowerbed 2,remove flowerbed 3,add flower to flowerbed 4, remove flower from flowerbed 5save and exit ");
addFlowerToFlowerbed1 = false;
bloss.url = null;
a = true;
break;
}
}
if (addFlowerToFlowerbed2 && judeg == 0) {
switch (String.valueOf(e.getKeyChar())) {
case "1":
System.out.println("你选择了纵向");
bloss.zh = 1;
vector.add(bloss);
String url = bloss.url;
int huatan = bloss.huatan;
bloss = new blossomfu();
bloss.url = url;
bloss.huatan = huatan;
// System.out.println(vector);
judeg = 1;
break;
case "2":
System.out.println("你选择了横向");
bloss.zh = 2;
vector.add(bloss);
String url1 = bloss.url;
int huatan2 = bloss.huatan;
bloss = new blossomfu();
bloss.url = url1;
bloss.huatan = huatan2;
// System.out.println(vector);
judeg = 1;
break;
case "3":
judeg = 1;
System.out.println(
"1,add flowerbed 2,remove flowerbed 3,add flower to flowerbed 4, remove flower from flowerbed 5save and exit ");
addFlowerToFlowerbed2 = false;
bloss = new blossomfu();
a = true;
break;
}
}
if (removeFlowerFromFlowerbed && judeg == 0) {
if (e.getKeyText(e.getKeyCode()) == "Enter") {
if (!str1.toString().equals("")) {
if (Integer.parseInt(str1.toString()) <= vector.size() && Integer.parseInt(str1.toString()) > 0) {
vector.remove((Integer.parseInt(str1.toString()) - 1));
System.out.println("删除成功");
} else {
System.out.println("没有此花列");
}
str1 = new StringBuffer();
} else {
System.out.println("没有此花列");
}
} else if (String.valueOf(e.getKeyChar()).equals("1") || String.valueOf(e.getKeyChar()).equals("2")
|| String.valueOf(e.getKeyChar()).equals("3") || String.valueOf(e.getKeyChar()).equals("4")
|| String.valueOf(e.getKeyChar()).equals("5") || String.valueOf(e.getKeyChar()).equals("6")
|| String.valueOf(e.getKeyChar()).equals("7") || String.valueOf(e.getKeyChar()).equals("8")
|| String.valueOf(e.getKeyChar()).equals("9") || String.valueOf(e.getKeyChar()).equals("0")) {
str1.append(String.valueOf(e.getKeyChar()));// 获取
} else if (String.valueOf(e.getKeyChar()).equals("f")) {
removeFlowerFromFlowerbed = false;
judeg = 1;
System.out.println(
"1,add flowerbed 2,remove flowerbed 3,add flower to flowerbed 4, remove flower from flowerbed 5save and exit ");
a = true;
}
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
class blossomfu {
Map<String, Object> map = new HashMap<String, Object>();
List<blossom> ju = new ArrayList<blossom>();
public int x;
public int y;
public int huatan;// 花坛
public int zh;// 纵横 1是纵 2是横着
public String url;// 路径
blossomfu() {
ju.add(new blossom("蓝花", "/1.png"));
ju.add(new blossom("黄花", "/4.png"));
ju.add(new blossom("白花", "/3.png"));
ju.add(new blossom("红花", "/2.png"));
}
List<blossom> ju() {
return ju;
}
void returnStr() {
System.out.println("请选择你要插入的花");
int i = 1;
for (blossom som : ju) {
i++;
System.out.print(i + ": " + som.getColour() + " ");
}
System.out.print("i 退出");
}
@Override
public String toString() {
return "blossomfu [map=" + map + ", ju=" + ju + ", x=" + x + ", y=" + y + ", huatan=" + huatan + ", zh=" + zh
+ ", url=" + url + ", in=" + in + "]";
}
int in;
}
class blossom {
private String colour;// 颜色
private String url;// 路径
public blossom(String colour, String url) {
super();
this.colour = colour;
this.url = url;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public blossom() {
}
private int x = 10;// 宽
private int y = 100;// 高
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}