java写一些简易图形程序(跟着b站黑马写的)

java写一些简易图形程序(跟着b站黑马写的),第1张

java写一些简易图形程序(跟着b站黑马写的)

用java awt跟着b站黑马程序员写的

简易d球游戏

d球游戏代码:
package myAwt.myDraw;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//d球游戏
public class PinBall {
    //创建窗口对象
    private frame frame = new frame("d球游戏");

    //桌面高度
    private final int TABLE_WIDTH = 300;
    //桌面宽度
    private final int TABLE_HEIGHT = 400;

    //球拍的高度和宽度
    private final int RACKET_WIDTH = 60;
    private final int RACKET_HEIGHT = 20;

    //小球的大小
    private final int BALL_SIZE = 16;

    //记录小球的坐标
    private int ballX = 120;
    private int ballY = 20;

    //记录小球在x,y方向上的移动速度
    private int speedX = 10;
    private int speedY = 5;

    //记录球拍的坐标
    private int racketX = 120;
    private final int racketY = 340;

    //记录当前游戏是否已经结束
    private boolean isOver = false;

    //声明一个定时器
    private Timer timer;

    //定义一个类,继承canvas,充当画布
    private class MyCanvas extends Canvas {
        @Override
        public void paint(Graphics g) {
            if (isOver) {
                //游戏结束
                g.setColor(Color.BLUE);

                g.setFont(new Font("Times", Font.BOLD, 30));

                g.drawString("GAME OVER!", 50, 200);
            } else {
                //游戏中

                //绘制小球
                g.setColor(Color.RED);
                g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);

                //绘制球拍
                g.setColor(Color.PINK);
                g.fillRect(racketX, racketY, RACKET_WIDTH, RACKET_HEIGHT);
            }
        }
    }

    //创建绘图区域
    MyCanvas drawArea = new MyCanvas();

    public void init() {
        //点叉号时关闭程序
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        //组装视图,游戏逻辑控制

        //完成球拍坐标的变化
        KeyListener listener = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //获取当前按下的键
                int keyCode = e.getKeyCode();

                if (keyCode == KeyEvent.VK_LEFT) {
                    //<-  向左移
                    if (racketX > 0) {
                        racketX -= 10;
                    }
                }

                if (keyCode == KeyEvent.VK_RIGHT) {
                    //->  向右移
                    if (racketX < TABLE_WIDTH - RACKET_WIDTH) {
                        racketX += 10;
                    }
                }
            }
        };

        //给frame和drawArea注册监听器
        frame.addKeyListener(listener);
        drawArea.addKeyListener(listener);

        //小球坐标的控制
        ActionListener task = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //根据边界范围,修正速度
                if (ballX <= 0 || ballX >= TABLE_WIDTH - BALL_SIZE) {
                    speedX = -speedX;
                }

                if (ballY <= 0 || (ballY > racketY - BALL_SIZE && ballX > racketX && ballX < racketX + RACKET_WIDTH)) {
                    speedY = -speedY;
                }

                if (ballY > racketY - BALL_SIZE && (ballX < racketX || ballX > racketX + RACKET_WIDTH)) {
                    //小球超出了球拍的范围,游戏结束

                    //停止计时器
                    timer.stop();

                    //修改游戏是否结束标记
                    isOver = true;

                    //重绘界面
                    drawArea.repaint();
                }

                //更新小球坐标,重绘界面
                ballX += speedX;
                ballY += speedY;

                //重绘界面
                drawArea.repaint();
            }
        };

        timer = new Timer(100, task);
        timer.start();

        drawArea.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));
        frame.add(drawArea);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new PinBall().init();
    }
}

简易绘图程序(鼠标右键选择颜色):
绘图程序代码:
package myAwt.myDraw;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class HandDrawDemo {
    private frame frame = new frame("简单手绘程序");

    //定义画图区的宽高
    private final int AREA_WIDTH = 500;
    private final int AREA_HEIGHT = 400;

    private PopupMenu colorMenu = new PopupMenu();
    private MenuItem redItem = new MenuItem("red");
    private MenuItem greenItem = new MenuItem("green");
    private MenuItem blueItem = new MenuItem("blue");

    private Color forceColor = Color.BLACK;

    BufferedImage image = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_RGB);

    Graphics g = image.getGraphics();

    private class MyCanvas extends Canvas {
        @Override
        public void paint(Graphics g) {
            g.drawImage(image, 0, 0, null);
        }
    }

    MyCanvas drawArea = new MyCanvas();

    //记录鼠标拖动过程中,上一次所处的坐标
    private int preX = -1;
    private int preY = -1;

    public void init() {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                switch (actionCommand) {
                    case "red":
                        forceColor = Color.RED;
                        break;
                    case "green":
                        forceColor = Color.GREEN;
                        break;
                    case "blue":
                        forceColor = Color.BLUE;
                        break;
                }
            }

        };

        redItem.addActionListener(listener);
        greenItem.addActionListener(listener);
        blueItem.addActionListener(listener);

        colorMenu.add(redItem);
        colorMenu.add(greenItem);
        colorMenu.add(blueItem);

        drawArea.add(colorMenu);

        drawArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                boolean popupTrigger = e.isPopupTrigger();
                if (popupTrigger) {
                    colorMenu.show(drawArea, e.getX(), e.getY());
                }

                //重置prex和prey
                preX = -1;
                preY = -1;
            }
        });

        //设置位图的背景为白色
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, AREA_WIDTH, AREA_HEIGHT);

        //监听鼠标的移动,完成线条绘制
        drawArea.addMouseMotionListener(new MouseMotionAdapter() {
            //当鼠标左键按下,并进行拖动,会被调用
            @Override
            public void mouseDragged(MouseEvent e) {
                if (preX > 0 && preY > 0) {
                    g.setColor(forceColor);

                    //画线条,要两组坐标,分别代表线条的起点和终点
                    g.drawLine(preX, preY, e.getX(), e.getY());

                }

                //修正prex和prey
                preX = e.getX();
                preY = e.getY();

                //重绘组件
                drawArea.repaint();
            }
        });

        drawArea.setPreferredSize(new Dimension(AREA_WIDTH, AREA_HEIGHT));
        frame.add(drawArea);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new HandDrawDemo().init();
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5606658.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存