初步完成功能
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_9" default="true" project-jdk-name="9" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="16" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
124
.idea/uiDesigner.xml
Normal file
@ -0,0 +1,124 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
Before Width: | Height: | Size: 6.9 KiB |
19
src/ChickenObject.java
Normal file
@ -0,0 +1,19 @@
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public abstract class ChickenObject {
|
||||
protected BufferedImage image;
|
||||
protected BufferedImage[] images;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected int x;
|
||||
protected int y;
|
||||
protected BufferedImage[] imagesEgg;
|
||||
protected BufferedImage[] imagesEggCracked;
|
||||
protected BufferedImage[] imagesChickenBroken;
|
||||
protected BufferedImage[] imagesChicken;
|
||||
public abstract void step();
|
||||
public abstract boolean outOfBounds();
|
||||
}
|
@ -1,11 +1,25 @@
|
||||
import java.awt.FlowLayout;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.JButton;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class Demo {
|
||||
public static void main(String[] args) {
|
||||
int a=1;
|
||||
boolean b= a(a);
|
||||
System.out.println(b);
|
||||
}
|
||||
public static boolean a(int a){
|
||||
return a==1;
|
||||
int x =Demo1.broken2.getHeight();
|
||||
int x1 =Demo1.broken1.getHeight();
|
||||
int y = Demo1.broken2.getWidth();
|
||||
int y1 = Demo1.broken1.getWidth();
|
||||
System.out.println("X:"+x+"_Y:"+y);
|
||||
System.out.println("X1:"+x1+"_Y1:"+y1);
|
||||
}
|
||||
}
|
||||
|
||||
|
22
src/Demo1.java
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Demo1 {
|
||||
public static BufferedImage broken2;
|
||||
public static BufferedImage broken1;
|
||||
public static int width = 50;
|
||||
public static int height = 80;
|
||||
static {
|
||||
try {
|
||||
broken2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
|
||||
broken2 = ImageIO.read(Demo1.class.getResource("broken2.png"));
|
||||
broken1 = ImageIO.read(Demo1.class.getResource("broken1.png"));
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
66
src/EggChicken.java
Normal file
@ -0,0 +1,66 @@
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.Serializable;
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class EggChicken extends ChickenObject implements Serializable {
|
||||
int index;
|
||||
/**
|
||||
* state = 0;鸡蛋
|
||||
* state = 1;壳裂开
|
||||
* state = 2;小鸡冒出头
|
||||
* state = 3;小鸡出来了
|
||||
*/
|
||||
private int state;
|
||||
Random random = new Random();
|
||||
|
||||
public EggChicken() {
|
||||
image = Fanning.egg0;
|
||||
width = image.getWidth();
|
||||
height = image.getHeight();
|
||||
imagesEgg = new BufferedImage[]{Fanning.egg0, Fanning.egg1,Fanning.egg2, Fanning.egg3,Fanning.egg4};
|
||||
imagesEggCracked = new BufferedImage[]{Fanning.eggCracked0, Fanning.eggCracked1,Fanning.eggCracked2, Fanning.eggCracked3,Fanning.eggCracked4};
|
||||
imagesChickenBroken = new BufferedImage[]{Fanning.broken0, Fanning.broken1,Fanning.broken2};
|
||||
imagesChicken = new BufferedImage[]{Fanning.chicken0,Fanning.chicken1};
|
||||
index = 0;
|
||||
state = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void step() {
|
||||
index++;
|
||||
if (state == 0) {
|
||||
if (index > 100) {
|
||||
state = 1;
|
||||
}
|
||||
image = imagesEgg[index++/25 % imagesEgg.length];
|
||||
}else if (state == 1) {
|
||||
if (index > 200) {
|
||||
state = 2;
|
||||
}
|
||||
image = imagesEggCracked[index++/50 % imagesEggCracked.length];
|
||||
}else if(state == 2){
|
||||
if (index > 300) {
|
||||
state = 3;
|
||||
}
|
||||
image = imagesChickenBroken[index++/75 % imagesChickenBroken.length];
|
||||
}else if(state == 3) {
|
||||
image = imagesChicken[index++/100 % imagesChicken.length];
|
||||
if(index%25 == 0) {
|
||||
this.x += random.nextInt(50) - 25;
|
||||
this.y += random.nextInt(25) - 13;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void appear(int x,int y){
|
||||
this.x = x;
|
||||
this.y = y+this.height/2;
|
||||
}
|
||||
@Override
|
||||
public boolean outOfBounds() {
|
||||
return this.x<0||this.x>Fanning.WIDTH||this.y<0||this.y>Fanning.HEIGHT;
|
||||
}
|
||||
}
|
240
src/Fanning.java
Normal file
@ -0,0 +1,240 @@
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class Fanning extends JPanel {
|
||||
|
||||
public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
|
||||
public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
|
||||
|
||||
public static BufferedImage background;
|
||||
public static BufferedImage start;
|
||||
public static BufferedImage pause;
|
||||
public static BufferedImage hen0;
|
||||
public static BufferedImage hen1;
|
||||
public static BufferedImage egg0;
|
||||
public static BufferedImage egg1;
|
||||
public static BufferedImage egg2;
|
||||
public static BufferedImage egg3;
|
||||
public static BufferedImage egg4;
|
||||
public static BufferedImage eggCracked0;
|
||||
public static BufferedImage eggCracked1;
|
||||
public static BufferedImage eggCracked2;
|
||||
public static BufferedImage eggCracked3;
|
||||
public static BufferedImage eggCracked4;
|
||||
public static BufferedImage chicken0;
|
||||
public static BufferedImage chicken1;
|
||||
public static BufferedImage broken0;
|
||||
public static BufferedImage broken1;
|
||||
public static BufferedImage broken2;
|
||||
|
||||
|
||||
|
||||
|
||||
static {
|
||||
try {
|
||||
background = ImageIO.read(Fanning.class.getResource("background.png"));
|
||||
// background = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
start = ImageIO.read(Fanning.class.getResource("start.png"));
|
||||
//start = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
pause = ImageIO.read(Fanning.class.getResource("pause.png"));
|
||||
// pause = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
hen0 = ImageIO.read(Fanning.class.getResource("hen0.png"));
|
||||
//hen0 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
hen1 = ImageIO.read(Fanning.class.getResource("hen1.png"));
|
||||
//hen1 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
egg0 = ImageIO.read(Fanning.class.getResource("egg0.png"));
|
||||
// egg0 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
egg1 = ImageIO.read(Fanning.class.getResource("egg1.png"));
|
||||
//egg1 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
egg2 = ImageIO.read(Fanning.class.getResource("egg2.png"));
|
||||
// egg2 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
egg3 = ImageIO.read(Fanning.class.getResource("egg3.png"));
|
||||
//egg3 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
egg4 = ImageIO.read(Fanning.class.getResource("egg4.png"));
|
||||
// egg4 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
eggCracked0 = ImageIO.read(Fanning.class.getResource("eggCracked0.png"));
|
||||
//eggCracked0 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
eggCracked1 = ImageIO.read(Fanning.class.getResource("eggCracked1.png"));
|
||||
//eggCracked1 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
eggCracked2 = ImageIO.read(Fanning.class.getResource("eggCracked2.png"));
|
||||
// eggCracked2 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
eggCracked3 = ImageIO.read(Fanning.class.getResource("eggCracked3.png"));
|
||||
//eggCracked3 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
eggCracked4 = ImageIO.read(Fanning.class.getResource("eggCracked4.png"));
|
||||
//eggCracked4 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
chicken0 = ImageIO.read(Fanning.class.getResource("chicken0.png"));
|
||||
//chicken0 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
chicken1 = ImageIO.read(Fanning.class.getResource("chicken1.png"));
|
||||
//chicken1 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
broken0 = ImageIO.read(Fanning.class.getResource("broken0.png"));
|
||||
// broken0 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
broken1 = ImageIO.read(Fanning.class.getResource("broken1.png"));
|
||||
//broken1 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
broken2 = ImageIO.read(Fanning.class.getResource("broken2.png"));
|
||||
// broken2 = new BufferedImage(WIDTH/20, HEIGHT/50,BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static final int START = 0;
|
||||
public static final int RUNNING = 1;
|
||||
public static final int PAUSE= 2;
|
||||
public static final int GAME_OVER = 3;
|
||||
private int state = START;
|
||||
|
||||
private Hen hen = new Hen();
|
||||
|
||||
private ArrayList <EggChicken> eggChickensOBJ = new ArrayList<>();
|
||||
|
||||
public void action() {
|
||||
|
||||
MouseAdapter l = new MouseAdapter() {
|
||||
|
||||
// 重写鼠标移动
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
if(state==RUNNING){
|
||||
int x = e.getX();
|
||||
int y = e.getY();
|
||||
hen.moveTo(x, y);
|
||||
|
||||
}
|
||||
}
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
switch(state){
|
||||
case START:
|
||||
state = RUNNING;
|
||||
break;
|
||||
case GAME_OVER:
|
||||
hen = new Hen();
|
||||
state = START;
|
||||
break;
|
||||
case RUNNING:
|
||||
int x = e.getX();
|
||||
int y = e.getY();
|
||||
EggChicken eggChicken = new EggChicken();
|
||||
eggChicken.appear(x,y);
|
||||
eggChickensOBJ.add(eggChicken);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
public void mouseExited(MouseEvent e) {
|
||||
if(state==RUNNING)
|
||||
state = PAUSE;
|
||||
}
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
if(state==PAUSE)
|
||||
state=RUNNING;
|
||||
}
|
||||
};
|
||||
|
||||
this.addMouseListener(l);// 处理鼠标操作事件
|
||||
this.addMouseMotionListener(l);// 处理鼠标移动事件
|
||||
|
||||
int interval = 10;// 时间间隔,以毫秒为单位
|
||||
int interval1 = 10;// 时间间隔,以毫秒为单位
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
public void run() {
|
||||
if(state==RUNNING){
|
||||
stepAction();
|
||||
outOfBoundsAction();
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
}, interval, interval1);// 定时日程
|
||||
|
||||
}
|
||||
|
||||
public void outOfBoundsAction(){
|
||||
for (int i = eggChickensOBJ.size()-1;i>=0;i--) {
|
||||
if (eggChickensOBJ.get(i).outOfBounds()) {
|
||||
eggChickensOBJ.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void stepAction(){
|
||||
hen.step();
|
||||
for (int i=0;i<eggChickensOBJ.size();i++) {
|
||||
eggChickensOBJ.get(i).step();
|
||||
}
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.drawImage(background, 0, 0, null);
|
||||
paintHen(g);
|
||||
paintEgg(g);
|
||||
paintState(g);
|
||||
|
||||
}
|
||||
|
||||
public void paintHen(Graphics g){
|
||||
g.drawImage(hen.image,hen.x,hen.y,null);
|
||||
}
|
||||
|
||||
public void paintEgg(Graphics g){
|
||||
for (EggChicken c:eggChickensOBJ) {
|
||||
g.drawImage(c.image, c.x, c.y, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 画状态
|
||||
* @param g
|
||||
*/
|
||||
public void paintState(Graphics g) {
|
||||
switch(state){
|
||||
case START:
|
||||
g.drawImage(start, 0, 0, null);
|
||||
break;
|
||||
case PAUSE:
|
||||
g.drawImage(pause, 0, 0,null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("FunningChicken");
|
||||
Fanning game = new Fanning();
|
||||
frame.add(game);
|
||||
frame.setUndecorated(true);
|
||||
frame.setSize(WIDTH,HEIGHT);
|
||||
frame.setAlwaysOnTop(true);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);// 1.c窗口可见,2.自动调paint
|
||||
|
||||
game.action();
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class Funning extends JPanel {
|
||||
public static final int WIDTH = 400;
|
||||
public static final int HEIGHT = 654;
|
||||
|
||||
public static BufferedImage background;
|
||||
/*
|
||||
public static BufferedImage start;
|
||||
public static BufferedImage pause;
|
||||
public static BufferedImage gameover;
|
||||
public static BufferedImage airplane;
|
||||
public static BufferedImage bee;
|
||||
public static BufferedImage bullet;
|
||||
public static BufferedImage hero0;
|
||||
public static BufferedImage hero1;
|
||||
public static BufferedImage bigplane;
|
||||
*/
|
||||
|
||||
|
||||
static {
|
||||
try {
|
||||
background = ImageIO.read(Funning.class.getResource("background.jpeg"));
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("FunningChicken");
|
||||
Funning game = new Funning();
|
||||
frame.add(game);
|
||||
|
||||
frame.setSize(WIDTH, HEIGHT);
|
||||
frame.setAlwaysOnTop(true);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);// 1.c窗口可见,2.自动调paint
|
||||
|
||||
|
||||
}
|
||||
}
|
29
src/Hen.java
Normal file
@ -0,0 +1,29 @@
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Hen extends ChickenObject{
|
||||
|
||||
private int index;
|
||||
|
||||
public Hen(){
|
||||
image = Fanning.hen0;
|
||||
width = image.getWidth();
|
||||
height = image.getHeight();
|
||||
images = new BufferedImage[]{Fanning.hen0,Fanning.hen1};
|
||||
index = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void step() {
|
||||
image = images[index++/50 % images.length];
|
||||
}
|
||||
|
||||
public void moveTo(int x,int y){
|
||||
this.x = x - this.width/2;
|
||||
this.y = y - this.height/2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean outOfBounds() {
|
||||
return false;
|
||||
}
|
||||
}
|
23
src/Utils.java
Normal file
@ -0,0 +1,23 @@
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
|
||||
public class Utils {
|
||||
public static <T> List<T> deepCopy(List<T> src) {
|
||||
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
||||
List<T> copy_list=null;
|
||||
try {
|
||||
ObjectOutputStream out = new ObjectOutputStream(byteOut);
|
||||
out.writeObject(src);
|
||||
|
||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
|
||||
ObjectInputStream in = new ObjectInputStream(byteIn);
|
||||
copy_list = (List<T>) in.readObject();
|
||||
}catch ( IOException|ClassNotFoundException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return copy_list;
|
||||
}
|
||||
|
||||
|
||||
}
|
Before Width: | Height: | Size: 6.9 KiB |
BIN
src/background.png
Normal file
After Width: | Height: | Size: 302 KiB |
BIN
src/broken0.png
Normal file
After Width: | Height: | Size: 9.0 KiB |
BIN
src/broken1.png
Normal file
After Width: | Height: | Size: 6.6 KiB |
BIN
src/broken2.png
Normal file
After Width: | Height: | Size: 6.6 KiB |
BIN
src/chicken0.png
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
src/chicken1.png
Normal file
After Width: | Height: | Size: 4.8 KiB |
BIN
src/egg0.png
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
src/egg1.png
Normal file
After Width: | Height: | Size: 5.4 KiB |
BIN
src/egg2.png
Normal file
After Width: | Height: | Size: 5.6 KiB |
BIN
src/egg3.png
Normal file
After Width: | Height: | Size: 5.5 KiB |
BIN
src/egg4.png
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
src/eggCracked0.png
Normal file
After Width: | Height: | Size: 7.0 KiB |
BIN
src/eggCracked1.png
Normal file
After Width: | Height: | Size: 7.1 KiB |
BIN
src/eggCracked2.png
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
src/eggCracked3.png
Normal file
After Width: | Height: | Size: 6.9 KiB |
BIN
src/eggCracked4.png
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
src/hen0.png
Normal file
After Width: | Height: | Size: 5.4 KiB |
BIN
src/hen1.png
Normal file
After Width: | Height: | Size: 5.4 KiB |
BIN
src/pause.png
Normal file
After Width: | Height: | Size: 88 KiB |
BIN
src/start.png
Normal file
After Width: | Height: | Size: 17 KiB |