1.贪吃蛇是在b站上学习的时候码的代码 B站
2.图形编辑是极简化的代码 程序中的代码中有详细的注释
贪吃蛇
package snake
;
import javax
.swing
.*
;
public class StartGame {
public static void main(String arggs
[]) {
JFrame frame
= new JFrame();
frame
.setBounds(10,10,900,720);
frame
.setResizable(false);
frame
.setDefaultCloseOperation(WindowConstants
.EXIT_ON_CLOSE
);
frame
.add(new GamePanel());
frame
.setVisible(true);
}
}
package snake
;
import javax
.swing
.*
;
import java
.awt
.*
;
import java
.awt
.event
.ActionEvent
;
import java
.awt
.event
.ActionListener
;
import java
.awt
.event
.KeyEvent
;
import java
.awt
.event
.KeyListener
;
import java
.util
.Random
;
public class GamePanel extends JPanel implements KeyListener,ActionListener
{
int length
;
int snakeX
[] = new int[600];
int snakeY
[] = new int[500];
String direction
;
int foodX
;
int foodY
;
Random random
= new Random();
int score
;
boolean isStart
= false;
boolean isFail
= false;
Timer timer
= new Timer(100, this);
public GamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
timer
.start();
}
public void init() {
length
= 3;
snakeX
[0] = 100; snakeY
[0] = 100;
snakeX
[1] = 75; snakeY
[1] = 100;
snakeX
[2] = 50; snakeY
[2] = 100;
direction
= "right";
foodX
= 25 + 25*random
.nextInt(34);
foodY
= 75 + 25*random
.nextInt(24);
score
= 0;
}
@Override
protected void paintComponent(Graphics g
) {
super.paintComponent(g
);
this.setBackground(Color
.white
);
Data
.header
.paintIcon(this, g
, 25, 11);
g
.fillRect(25, 75, 850, 600);
g
.setColor(Color
.black
);
g
.setFont(new Font("微软雅黑",Font
.BOLD
,18));
g
.drawString("长度:"+length
, 750, 35);
g
.drawString("分数:"+score
, 750, 50);
Data
.food
.paintIcon(this, g
, foodX
, foodY
);
if(direction
.equals("right")) {
Data
.right
.paintIcon(this, g
, snakeX
[0], snakeY
[0]);
}else if(direction
.equals("left")) {
Data
.left
.paintIcon(this, g
, snakeX
[0], snakeY
[0]);
}else if(direction
.equals("up")) {
Data
.up
.paintIcon(this, g
, snakeX
[0], snakeY
[0]);
}else if(direction
.equals("down")) {
Data
.down
.paintIcon(this, g
, snakeX
[0], snakeY
[0]);
}
for (int i
= 1; i
< length
; i
++) {
Data
.body
.paintIcon(this, g
, snakeX
[i
], snakeY
[i
]);
}
if(isStart
== false) {
g
.setColor(Color
.white
);
g
.setFont(new Font("微软雅黑",Font
.BOLD
,40));
g
.drawString("按下空格开始游戏", 300, 300);
}
if(isFail
) {
g
.setColor(Color
.red
);
g
.setFont(new Font("微软雅黑",Font
.BOLD
,40));
g
.drawString("按下空格重新开始", 300, 300);
}
}
@Override
public void keyPressed(KeyEvent e
) {
int keyCode
= e
.getKeyCode();
if(keyCode
== KeyEvent
.VK_SPACE
) {
if(isFail
) {
isFail
= false;
init();
}else {
isStart
= !isStart
;
}
repaint();
}
if(keyCode
== KeyEvent
.VK_UP
) {
direction
= "up";
}else if(keyCode
== KeyEvent
.VK_DOWN
) {
direction
= "down";
}else if(keyCode
== KeyEvent
.VK_LEFT
) {
direction
= "left";
}else if(keyCode
== KeyEvent
.VK_RIGHT
) {
direction
= "right";
}
}
@Override
public void actionPerformed(ActionEvent e
) {
if(isStart
&& isFail
== false) {
if(snakeX
[0] == foodX
&& snakeY
[0] == foodY
) {
length
++;
score
= score
+ 10;
foodX
= 25 + 25*random
.nextInt(34);
foodY
= 75 + 25*random
.nextInt(24);
}
for(int i
= length
-1; i
>0;i
--) {
snakeX
[i
] = snakeX
[i
-1];
snakeY
[i
] = snakeY
[i
-1];
}
if(direction
.equals("right")) {
snakeX
[0] = snakeX
[0] + 25;
if(snakeX
[0]>850){snakeX
[0] = 25;}
}else if(direction
.equals("left")) {
snakeX
[0] = snakeX
[0] - 25;
if(snakeX
[0]<25){snakeX
[0] = 850;}
}else if(direction
.equals("up")) {
snakeY
[0] = snakeY
[0] - 25;
if(snakeY
[0]<75){snakeY
[0] = 650;}
}else if(direction
.equals("down")) {
snakeY
[0] = snakeY
[0] + 25;
if(snakeY
[0]>650){snakeY
[0] = 75;}
}
for(int i
= 1; i
<length
;i
++) {
if(snakeX
[0] == snakeX
[i
] && snakeY
[0] == snakeY
[i
]) {
isFail
= true;
}
}
repaint();
}
timer
.start();
}
public void keyTyped(KeyEvent e
) {}
public void keyReleased(KeyEvent e
) {}
}
package snake
;
import java
.net
.URL
;
import javax
.swing
.*
;
public class Data {
public static URL headerUrl
= Data
.class.getResource("statics/header.png");
public static ImageIcon header
= new ImageIcon(headerUrl
);
public static URL upUrl
= Data
.class.getResource("statics/up.png");
public static URL downUrl
= Data
.class.getResource("statics/down.png");
public static URL leftUrl
= Data
.class.getResource("statics/left.png");
public static URL rightUrl
= Data
.class.getResource("statics/right.png");
public static ImageIcon up
= new ImageIcon(upUrl
);
public static ImageIcon down
= new ImageIcon(downUrl
);
public static ImageIcon left
= new ImageIcon(leftUrl
);
public static ImageIcon right
= new ImageIcon(rightUrl
);
public static URL bodyUrl
= Data
.class.getResource("statics/body.png");
public static ImageIcon body
= new ImageIcon(bodyUrl
);
public static URL foodUrl
= Data
.class.getResource("statics/food.png");
public static ImageIcon food
= new ImageIcon(foodUrl
);
}
图形编辑(画板)
import java
.awt
.*
;
import java
.awt
.event
.*
;
import javax
.swing
.*
;
public class NewPaint extends JPanel{
private NewShape
[] shapeArray
= new NewShape[10000];
public static void main(String
[] args
) {
NewPaint p
= new NewPaint();
}
JFrame frame
= new JFrame();
int len
= 0;
PaintListener paintListener
= new PaintListener();
public NewPaint() {
frame
.setTitle("图形编辑软件");
frame
.setLocation(450, 150);
frame
.setSize(700, 600);
this.setPreferredSize(new Dimension(600, 500));
frame
.setLayout(new FlowLayout());
String
[] command
= { "直线", "画笔", "矩形","圆", "多边形", "文本","图片","清除"};
for (int i
= 0; i
< command
.length
; i
++) {
JButton button
= new JButton(command
[i
]);
button
.addActionListener(paintListener
);
frame
.add(button
);
}
Color
[] color
= { Color
.BLACK
, Color
.BLUE
, Color
.YELLOW
, Color
.RED
, Color
.GREEN
};
for (int i
= color
.length
- 1; i
>= 0; i
--) {
JButton button
= new JButton();
button
.setBackground(color
[i
]);
button
.setPreferredSize(new Dimension(20, 20));
button
.addActionListener(paintListener
);
frame
.add(button
);
}
frame
.add(this);
frame
.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
frame
.setVisible(true);
Graphics graphics1
= this.getGraphics();
this.addMouseListener(paintListener
);
this.addMouseMotionListener(paintListener
);
paintListener
.setGraphics(graphics1
);
paintListener
.setJPanel(this);
paintListener
.setShapeArray(shapeArray
);
}
public void paint(Graphics graphics
) {
super.paint(graphics
);
len
= paintListener
.get_len();
for(int i
=0;i
<len
;i
++) {
if(shapeArray
[i
]!=null
) {
shapeArray
[i
].repaint(graphics
);
}
else {
break;
}
}
}
}
import java
.awt
.event
.*
;
import javax
.swing
.ImageIcon
;
import javax
.swing
.JButton
;
import javax
.swing
.JFrame
;
import javax
.swing
.JPanel
;
import javax
.swing
.JScrollPane
;
import javax
.swing
.JTextArea
;
import java
.awt
.*
;
public class PaintListener extends JFrame implements ActionListener,MouseListener
,MouseMotionListener
{
private int index
=0;
private NewShape shapeArray
[];
private JPanel panel
;
private Graphics graphics
;
private String order
="";
int x0
=0,y0
=0,x1
=0,y1
=0,x2
=0,y2
=0,x3
=0,x4
=0,y3
=0,y4
=0;
int start_x
=0,start_y
=0;
public void setJPanel(JPanel panel
) {
this.panel
=panel
;
}
public void setGraphics(Graphics graphics
) {
this.graphics
= graphics
;
}
public void setShapeArray(NewShape shapeArray
[]) {
this.shapeArray
= shapeArray
;
}
public int get_len() {
return index
;
}
public void actionPerformed(ActionEvent e
)
{
x1
= 200; y1
= 200;
if (e
.getActionCommand()=="") {
JButton button
= (JButton
)e
.getSource();
graphics
.setColor(button
.getBackground());
}
else {
order
= e
.getActionCommand();
if("清除".equals(order
)) {
index
= 0;
panel
.repaint();
x4
= 0;
y4
= 0;
}
}
}
public void mouseDragged(MouseEvent e
) {
if(order
.equals("画笔")) {
x1
= x0
; y1
= y0
;
x0
= e
.getX();
y0
= e
.getY();
graphics
.drawLine(x1
,y1
,x0
,y0
);
shapeArray
[index
++] = new NewShape(x1
,y1
,x0
,y0
,order
);
}
}
public void mouseClicked(MouseEvent e
) {
if(order
.equals("多边形")) {
if(x4
==0&&y4
==0){
x4
= e
.getX();
y4
= e
.getY();
start_x
= x4
;
start_y
= y4
;
}
else {
x3
= x4
; y3
= y4
;
x4
= e
.getX();
y4
= e
.getY();
graphics
.drawLine(x3
, y3
, x4
, y4
);
shapeArray
[index
++] = new NewShape(x3
,y3
,x4
,y4
,order
);
}
if(e
.getClickCount()==2){
x4
=0; y4
=0;
graphics
.drawLine(start_x
, start_y
, e
.getX(), e
.getY());
shapeArray
[index
++] = new NewShape(start_x
,start_y
,e
.getX(),e
.getY(),order
);
}
}
if(order
.equals("文本")) {
x1
= e
.getX();
y1
= e
.getY();
String text
= "文本框";
Font font
= new Font("行楷", Font
.BOLD
+Font
.ITALIC
, 26);
graphics
.setFont(font
);
graphics
.drawString(text
, x1
, y1
);
shapeArray
[index
++] = new NewShape(x1
, y1
, text
);
}
if(order
.equals("图片")) {
x1
= e
.getX();
y1
= e
.getY();
ImageIcon icon
= new ImageIcon(
"G:\\eclipse\\workspace\\internetWork\\Work\\grapEdit\\picture1.jpg");
Image img
= icon
.getImage();
graphics
.drawImage(img
, x1
, y1
, null
);
shapeArray
[index
++] = new NewShape(x1
, y1
, img
);
}
}
public void mousePressed(MouseEvent e
) {
if(order
.equals("画笔")){
x0
= e
.getX();
y0
= e
.getY();
}
x1
= e
.getX();
y1
= e
.getY();
}
public void mouseReleased(MouseEvent e
) {
x2
= e
.getX();
y2
= e
.getY();
if("直线".equals(order
)){
graphics
.drawLine(x1
, y1
, x2
, y2
);
shapeArray
[index
++] = new NewShape(x1
,y1
,x2
,y2
,order
);
}
else if("矩形".equals(order
)) {
graphics
.drawRect(Math
.min(x1
, x2
), Math
.min(y1
, y2
), Math
.abs(x1
-x2
), Math
.abs(y1
-y2
));
shapeArray
[index
++] = new NewShape(x1
,y1
,x2
,y2
,order
);
}
else if("圆".equals(order
)) {
graphics
.drawOval(Math
.min(x1
, x2
), Math
.min(y1
, y2
), Math
.abs(x1
-x2
), Math
.abs(y1
-y2
));
shapeArray
[index
++] = new NewShape(x1
,y1
,x2
,y2
,order
);
}
}
public void mouseEntered(MouseEvent e
) {}
public void mouseExited(MouseEvent e
) {}
public void mouseMoved(MouseEvent e
) {}
}
import java
.awt
.Graphics
;
import java
.awt
.Image;
import javax
.swing
.ImageIcon
;
import javax
.swing
.JPanel
;
public class NewShape{
private int x1
,x2
,y1
,y2
;
private String name
;
private Image image
;
public NewShape(int x1
,int y1
,int x2
,int y2
,String name
){
this.x1
= x1
;
this.x2
= x2
;
this.y1
= y1
;
this.y2
= y2
;
this.name
= name
;
}
public NewShape(int x1
,int y1
, String name
){
this.x1
= x1
;
this.y1
= y1
;
this.name
= name
;
}
public NewShape(int x1
,int y1
, Image image
){
this.x1
= x1
;
this.y1
= y1
;
this.image
= image
;
}
public void repaint(Graphics g
) {
switch(name
) {
case "直线":
g
.drawLine(x1
, y1
, x2
, y2
);
break;
case "矩形":
g
.drawRect(Math
.min(x1
, x2
), Math
.min(y1
,y2
), Math
.abs(x1
-x2
), Math
.abs(y1
-y2
));
break;
case "圆形":
g
.drawOval(Math
.min(x1
, x2
), Math
.min(y1
,y2
), Math
.abs(x1
-x2
), Math
.abs(y1
-y2
));
break;
case "曲线":
g
.drawLine(x1
, y1
, x2
, y2
);
break;
case "多边形":
g
.drawLine(x1
, y1
, x2
, y2
);
break;
case "文本":
g
.drawString(name
, x1
, y1
);
break;
case "图片":
g
.drawImage(image
, x1
, y1
, null
);
break;
}
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-63300.html