Save the bee with java code source

Save the bee with java code source

In this tutorail you will learn how to create the game named Save the bee with java.

Playing games is so fanny but develop the games with java is sweet and enjoyable.

Let’s start:

This is the finale interface of this game:

Source Code: Monetize your website traffic with yX Media

package com.javatmz.javaapp.bee;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
 
public class BeeFly extends JFrame {
 
private static final int D_W = 1000;
private static final int D_H = 700;
DrawPanel drawPanel = new DrawPanel();
 
int with = 1600;
int height = 768;
 
// int previousY = 0;
int unit = 10;
 
List<Line> lines = new ArrayList<Line>();
boolean down = false;
boolean keyPress = false;
 
int x = 100, y = 100, h = 100;
double dx = 0, dy = 0;
 
public BeeFly() {
 
    add(drawPanel);
    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
 
    // lines = new ArrayList<Line>();
    for (int i = 0; i < 10; i++) {
        Line line = new Line();
        Random rx = new Random();
        line.x = rx.nextInt(600) + 20;
 
        Random ry = new Random();
        line.y = i * 50;
 
 
        lines.add(line);
    }
 
}
 
private class DrawPanel extends JPanel {
    public DrawPanel() {
        InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = getActionMap();
 
        String VK_LEFT = "VK_LEFT";
        KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
        inputMap.put(W, VK_LEFT);
        actionMap.put(VK_LEFT, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                keyPress=true;
                x -= 1;
                dy -= 2;
                drawPanel.repaint();
                keyPress=false;
 
            }
        });
 
        String VK_RIGHT = "VK_RIGHT";
        KeyStroke WVK_RIGHT = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
        inputMap.put(WVK_RIGHT, VK_RIGHT);
        actionMap.put(VK_RIGHT, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                keyPress=true;
                x += 1;
                dy -= 2;
                drawPanel.repaint();
                keyPress=false;
 
            }
        });
 
        ActionListener listener = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                if (!keyPress) {
                    dy += 0.1;
                    y += dy;
                    if (y > 650)
                        dy = -2;
 
                }
                if (y < h) {
                    for (Line line : lines) {
                        y = h;
                        line.y = line.y - (int) dy;
                        if (line.y > 700) {
                            Random rw = new Random();
                            line.y = 80;
                            line.x = rw.nextInt(600) + 20;
                        }
                    }
                }
 
                /*
                 * for (Line line : lines) { if ((x > line.x && x < line.x +
                 * line.with) && (y >= line.y && y <= line.y + 10 && dy >
                 * 0)) { dy = -10; } }
                 */
 
                drawPanel.repaint();
 
            }
        };
        Timer timer = new Timer(10, listener);
 
        timer.start();
 
    }
 
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
 
        drawValuesSteps(g);
        drawValues(g);
    }
 
    private void drawValuesSteps(Graphics g) {
 
        for (Line line : lines) {
 
            Graphics stepBorder = (Graphics) g;
            stepBorder.setColor(Color.black);
            // stepBorder.s
            stepBorder.drawOval(line.x, line.y, 25, 25);
            Graphics step = (Graphics) g;
            step.setColor(Color.RED);
            step.fillOval(line.x, line.y, 25, 25);
        }
    }
 
    private void drawValues(Graphics g) {
 
        int currentX = x;
        int currentY = y;
 
        Graphics yellowg = (Graphics) g;
        yellowg.setColor(Color.yellow);
        yellowg.fillRect(currentX - unit, currentY - 4 * unit, unit, 3 * unit);
 
        yellowg.fillRect(currentX + unit, currentY - 5 * unit, 5 * unit, 5 * unit);
 
        Graphics blackg = (Graphics) g;
        blackg.setColor(Color.black);
 
        // 1
        int i = 1;
        blackg.fillRect(currentX, currentY, unit * 5, unit);
        blackg.fillRect(currentX - unit, currentY - i * unit, unit, unit);
        // step.fillRect(currentX+5*unit, currentY-i*unit, unit, unit);
        blackg.fillRect(currentX, currentY - (5 + i) * unit, unit, 6 * unit);
        blackg.fillRect(currentX + 5 * unit, currentY - i * unit, unit, unit);
        i++;
        blackg.fillRect(currentX - 2 * unit, currentY - (i + 2) * unit, unit, 3 * unit);
        blackg.fillRect(currentX + 6 * unit, currentY - (i + 2) * unit, unit, 3 * unit);
        i++;
        blackg.fillRect(currentX - 4 * unit, currentY - i * unit, 3 * unit, unit);
 
        blackg.fillRect(currentX + 4 * unit, currentY - i * unit, unit, unit);// tiT
        i++;
        i++;
        blackg.fillRect(currentX - unit, currentY - i * unit, unit, unit);
        blackg.fillRect(currentX + 5 * unit, currentY - i * unit, unit, unit);
        i++;
        blackg.fillRect(currentX, currentY - i * unit, unit * 5, unit);
 
        Graphics blueg = (Graphics) g;
        blueg.setColor(new Color(135, 206, 235));
 
        blueg.fillRect(currentX - unit, currentY - (i + 2) * unit, 5 * unit, 2 * unit);
 
        blackg.setColor(Color.black);
        blackg.fillRect(currentX + 2 * unit, currentY - (1 + i) * unit, unit, 7 * unit);
 
        blackg.fillRect(currentX + 4 * unit, currentY - (i + 2) * unit, unit, 2 * unit);
        i++;
        blackg.fillRect(currentX - unit, currentY - i * unit, unit, unit);
        i++;
        blackg.fillRect(currentX - 2 * unit, currentY - i * unit, unit, unit);
        blackg.fillRect(currentX + unit, currentY - i * unit, unit, unit);
        i++;
        blackg.fillRect(currentX - unit, currentY - i * unit, 2 * unit, unit);
        blackg.fillRect(currentX + 2 * unit, currentY - i * unit, 2 * unit, unit);
 
    }
 
    public Dimension getPreferredSize() {
        return new Dimension(D_W, D_H);
    }
}
 
private class Line {
    int x;
    int y;
    int with;
}
 
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
 
        @Override
        public void run() {
            new BeeFly();
 
        }
    });
}
}

Comments