java项目学习绘总

代码练习

public class SwitchControlFlowDemo {
    public static void main(String[] args){
        int a = (int)(Math.random()*26);
        char c = (char)('a'+a);

        switch (c){
            case 'a':
                System.out.println("The value of c is:'a'");
                break;
            case 'b':
                System.out.println("The value of c is:'b'");
                break;
            case 'c':
                System.out.println("The value of c is:'c'");
                break;
            default:
                System.out.println("c has value:"+c);
                break;
        }
    }
}
public class NumericOpDemo {
    public static  void main(String[] args){
        int a = 1+2;
        int b = a*2;
        int c = a/2; //小数部分直接舍去
        int d = b%4;
        int e = a-4;
        int f = -a;
        int i = 6;
        int j = i++;
        int k = ++i;

        System.out.println("a="+a);
        System.out.println("b="+b);
        System.out.println("c="+c);
        System.out.println("d="+d);
        System.out.println("e="+e);
        System.out.println("f="+f);
        System.out.println("i="+i);
        System.out.println("j="+j);
        System.out.println("k="+k);
    }
}
public class LoopControlFlowDemo {
    public static void main(String[] args){
        int sum = 0;
        int count = 0;
        int dest = 50;

        do {
            count++;
            sum += count;
        }while(count<dest);

        System.out.println("count="+count);
        System.out.println("sum="+sum);

        sum=0;
        count=0;

        while(count<dest){
            count++;
            sum += count;
        }
        System.out.println("count="+count);
        System.out.println("sum="+sum);

        sum = 0;
        for (int i=0;i<=dest;i++){
            sum += i;
        }
        System.out.println("sum="+sum);
    }
}
public class LogicOpDemo {
    public static void main(String[] args){
        int a = 10;
        int b = 5;

        boolean t = true;
        boolean f = false;

        System.out.println("a>b:"+(a>b));
        System.out.println("a==b:"+(a==b));
        System.out.println("t&f:"+(t&f));
        System.out.println("t|f:"+(t|f));
        System.out.println("t^f:"+(t^f));   //按位取反
        System.out.println("t&&f:"+(t&&f));
        System.out.println("t||f:"+(t||f));
    }
}
public class JumpInLoopDemo {
    public static void main(String[] args){
        int count = 0;
        int sum = 0;
        int i = 0;

        while (true){
            i++;
            if (i>100)
                break;
            if (i%5==0){
                count++;
                continue;
            }

            sum += i;
        }
        System.out.println("i="+i);
        System.out.println("count="+count);
        System.out.println("sum="+sum);
    }
}
import java.util.Random;

public class IfControlFlowDemo {
    public static void main(String[] args){
        Random randomProducer = new Random();

        int a = randomProducer.nextInt();   //a和b的值随机生成
        int b = randomProducer.nextInt();

        if (a>b){
            System.out.println("a is bigger than b");
        }
        else if(a<b){
            System.out.println("a is smaller than b");
        }
        else{
            System.out.println("a and b have the same value");
        }

        System.out.println("a="+a);
        System.out.println("b="+b);
    }
}
public class DataTypeDemo {
    public static void main(String[] args){
        int intData = 1;
        int hexData = 0xf000;   //十六进制数表示
        int octData = 010;  //八进制数表示

        short shortData = 1;

        long longData = 34L;    //声明long数据类型时,可以使用后缀"L",也可以不用
        long longData2 = 34;

        byte byteData = 24;

        float floatData = 1.3f; //声明float数据类型时,必须使用后缀"f"或者"F"

        double doubleData = 1.3;
        double doubleData2 = 1.3D;  //声明double数据类型时,可以不使用后缀,也可以使用

        char charData = 'a';
        char charData2 = 98;
        char charData3 = '\t';  //转义字符的使用
        char charData4 = '\u0030';  //unicode字符数据类型

        boolean boolData = false;

        System.out.println("intData="+intData);
        System.out.println("hexData="+hexData);
        System.out.println("octData="+octData);
        System.out.println("shortData="+shortData);
        System.out.println("longData="+longData);
        System.out.println("longData2="+longData2);
        System.out.println("byteData="+byteData);
        System.out.println("floatData="+floatData);
        System.out.println("doubleData="+doubleData);
        System.out.println("doubleData2="+doubleData2);
        System.out.println("charData="+charData);
        System.out.println("charData2="+charData2);
        System.out.println("charData3="+charData3);
        System.out.println("charData4="+charData4);
        System.out.println("boolData="+boolData);
    }
}
public class ArrayTest {
    public static void main(String[] args){
        int[] aaa={23, 14, 35, 2, 71, 55, 82, 41, 11, 6};
        Ary ay1 = new Ary(aaa);

        ay1.print();
        ay1.order();
        ay1.print();
        for(int m=0; m<aaa.length; m++)
            System.out.println("---"+aaa[m]);
        System.out.println();
        Ary ay2 = new Ary(ay1.subAry(2,6));
        ay2.print();
    }
}
    class Ary{
        int[] m_ary;

        Ary(int[] p){
            m_ary = p;
        }
        void print(){
            for(int i=0; i<m_ary.length; i++)
                System.out.println(m_ary[i]+" ");
                System.out.println();
        }
        void order(){
            int j,temp;
            boolean exchange = true;

            j = m_ary.length-2;
            while(j>=0 && exchange){
                exchange = false;
                for (int i=0; i<=j; i++)
                    if(m_ary[i]>m_ary[i+1]){
                        exchange = true;
                        temp = m_ary[i];
                        m_ary[i] = m_ary[i+1];
                        m_ary[i+1] = temp;
                    }
                j = j-1;
            }
        }
        int[] subAry(int a1, int a2){
            int[] sa;

            if(a1>=0 && a1<a2 && a2<m_ary.length){
                sa = new int[a2-a1+1];
                for (int i=0, j=a1; j<=a2; i++,j++)
                    sa[i] = m_ary[j];
                return sa;
            }
            else{
                System.out.println("error index");
                return new int[1];
            }
        }
    }
public class BitOpDemo {
    public static void main(String[] args){
        int a = 0xB5;
        int b = 0xA3;

        System.out.println("a="+Integer.toBinaryString(a));
        System.out.println("b="+Integer.toBinaryString(b));
        System.out.println("~a="+Integer.toBinaryString(~a));   //4个字符
        System.out.println("a&b="+Integer.toBinaryString(a&b));
        System.out.println("a|b="+Integer.toBinaryString(a|b));
        System.out.println("a^b="+Integer.toBinaryString(a^b));
        System.out.println("a<<3="+Integer.toBinaryString(a<<3));
        System.out.println("a>>3="+Integer.toBinaryString(a>>3));
        System.out.println("a>>>3="+Integer.toBinaryString(a>>>3));
    }
}
public class ValueTransfer {
    void modify(int i){
        i++;
    }

    void modify(int[] arr){
        for(int i=0; i<arr.length; i++){
            arr[i] = 1;
        }
    }

    void modify(SimpleClass simpleClass){
        simpleClass.field = 1;
    }

    public static void main(String[] args) {
        ValueTransfer valueTransfer = new ValueTransfer();

        int intValue = 0;
        valueTransfer.modify(intValue);
        System.out.println("intValue="+intValue);

        int[] intArr = new int[1];
        valueTransfer.modify(intArr);
        System.out.println("intArr[0]="+intArr[0]);

        SimpleClass simple = new SimpleClass();
        valueTransfer.modify(simple);
        System.out.println("simple.filed="+simple.field);
    }
}

class SimpleClass{
    int field;
}

雪花飘落

代码展示:
雪花
代码:

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

public class Snow {
    public static void main(String[] args) {
        JFrame f = new JFrame("雪花");
        Snow_Panel p = new Snow_Panel();
        f.add(p);
        f.setSize(1980, 1080);
        f.setVisible(true);
    }
}

class Snow_Panel extends JPanel{
    int[] x_index = new int[500];
    int[] y_index = new int[500];
    int[] size_index = new int[500];
    Snow_Panel(){
        for(int j=0; j<500; j++){
            x_index[j] = (int)(Math.random()*1980);
            y_index[j] = (int)(Math.random()*1080);
            size_index[j] = (int)(30-Math.random()*10);
        }
    }
    public void paint(Graphics g){
        super.paint(g);
        setBackground(Color.BLACK);
        g.setColor(Color.WHITE);

        for(int i=0; i<500; i++){
            int range = (int)(15-Math.random()*5);
            x_index[i] = x_index[i] + (int)(Math.random()*range-range/2);
            y_index[i] = (int)(y_index[i] + Math.random()*range);

            g.setFont(new Font("", 0, size_index[i]+range));
            g.drawString("*", x_index[i], y_index[i]);
            if (y_index[i]>1080) {
                y_index[i] = (int)(Math.random()*108);
            }
            if(x_index[i]<0 && x_index[i] >1980){
                x_index[i] = (int)(Math.random()*1980);
            }
        }
        try{
            Thread.sleep(50);
        }catch (Exception e){}

        repaint();
    }
}

十字路口

代码展示:
十字路口
代码:

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

public class CrossRoad {
    public static void main(String[] args) {
        JFrame f = new JFrame("十字路口");
        f.setSize(1980,1080);
        RoadPanel p = new RoadPanel();
        f.add(p);
        f.setVisible(true);
    }
}

class RoadPanel extends JPanel{
    List<Integer> x = new ArrayList<Integer>();
    List<Integer> y = new ArrayList<Integer>();
    List<Integer> color = new ArrayList<Integer>();
    // 1(y++);2(x++);3(y--)
    List<Integer> pattern = new ArrayList<Integer>();

    public void paint(Graphics g){
        super.paint(g);
        setBackground(Color.WHITE);
        road(g); //绘制马路
        plane(g); // 绘制飞机
        try{
            Thread.sleep(100);
        }catch (Exception e){}
        repaint();
    }

    void road(Graphics g){
        g.setColor(Color.BLACK);
        g.fillRect(890,440,200,200); //中间道璐
        g.fillRect(0,440,890,200); //左道璐
        g.fillRect(1090,440,890,200); //右道璐
        g.fillRect(890,0,200,440); //上道璐
        g.fillRect(890,640,200,440); //下道璐
    }

    void plane(Graphics g){
        g.setFont(new Font("",Font.BOLD, 100));
        for(int i=0; i<x.size(); i++){
            if(x.get(i)>1980 | y.get(i)>1080 | y.get(i)<-100){
                x.remove(i);
                y.remove(i);
                pattern.remove(i);
            }
        }
        if(x.size()<10){
            produce();
        }
        for(int i=0; i<x.size(); i++){
            g.setColor(new Color(color.get(3*i), color.get(3*i+1), color.get(3*i+2)));
            g.drawString("✈", x.get(i), y.get(i));
            if(pattern.get(i) == 1){ //switch出错
                for(int j=0; j<x.size(); j++){
                    if(pattern.get(j)==2 & x.get(j)>790 & x.get(j)<1090){
                        if(y.get(j) > 440){
                            y.set(i, y.get(i)+10);
                        }
                    }else{
                        y.set(i, y.get(i)+10);
                    }
                }
            }else if(pattern.get(i) == 2){
                for(int j=0; j<x.size(); j++){
                    if(pattern.get(j)!=2 & y.get(j)>340 & y.get(j)<640){
                        break;
                    }
                    else{
                        x.set(i, x.get(i)+10);
                    }
                }
            }else if(pattern.get(i) == 3){
                for(int j=0; j<x.size(); j++){
                    if(pattern.get(j)==2 & x.get(j)>790 & x.get(j)<1090){
                        break;
                    }
                    else{
                        y.set(i, y.get(i)-10);
                    }
                }
            }
        }
    }

    void produce(){ // 生成飞机
        Random rand = new Random();
        for(int i=0; i<3; i++){
            color.add(rand.nextInt(255));
        }
        int direction = rand.nextInt(3) + 1; //控制飞机生成的方向(1:上,2:左,3:下)
        pattern.add(direction);
        switch (direction){
            case 1: //上边(1)
                x.add(890);
                y.add(80);
                break;
            case 2: //左边(2)
                //控制飞机生成的位置(0:上、左,1:下、右)
                int location = rand.nextInt(2);
                if(location == 0){ //上(0)
                    x.add(0);
                    y.add(525);
                }else{ //下(1)
                    x.add(0);
                    y.add(625);
                }
                break;
            case 3: //下边(3)
                x.add(990);
                y.add(1010);
                break;
        }
    }
}

小球碰撞

代码展示:
小球碰撞
代码:

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

public class Ball {
    public static void main(String[] args) {
        Frame f = new Frame("球");
        f.setSize(1500, 800);
        Ball_Panel p = new Ball_Panel();
        f.add(p);
        f.setVisible(true);
    }
}


class Ball_Panel extends JPanel {
    int[] x = new int[15];
    int[] y = new int[15];
    int[] x_speed = new int[15];
    int[] y_speed = new int[15];
    int[] color = new int[45];
    int[] size = new int[15];

    Ball_Panel(){
        for(int i=0; i<15; i++){
            x[i] = (int)(100+Math.random()*1200);
            y[i] = (int)(100+Math.random()*500);
            x_speed[i] = (int)(1+Math.random()*5);
            y_speed[i] = (int)(1+Math.random()*5);
            color[i*3] = (int)(Math.random()*255);
            color[i*3+1] = (int)(Math.random()*255);
            color[i*3+2] = (int)(Math.random()*255);
            size[i] = (int)(Math.random()*10+30);
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        setBackground(Color.WHITE);
        g.setColor(Color.BLACK);
        g.drawRect(100, 100, 1300, 600);

        ball_collide(x, y, x_speed, y_speed, size);
        for (int i = 0; i < x.length; i++) {
            g.setColor(new Color(color[i*3], color[i*3+1], color[i*3+2]));
            x[i] += x_speed[i];
            if (x[i] > 1400-size[i]) {
                boundary_collide(x_speed, color, i);
            }
            if (x[i] < 100) {
                boundary_collide(x_speed, color, i);
            }
            y[i] += y_speed[i];
            if (y[i] > 700-size[i]) {
                boundary_collide(y_speed, color, i);
            }
            if (y[i] < 100) {
                boundary_collide(y_speed, color, i);
            }
            g.fillOval(x[i], y[i], size[i], size[i]);
            }
        try {
            Thread.sleep(10);
        } catch (Exception e) {}
        repaint();
    }

    void boundary_collide(int[] array, int[] color, int i){
        array[i] = -array[i];
        color[i*3] = (int)(Math.random()*255);
        color[i*3+1] = (int)(Math.random()*255);
        color[i*3+2] = (int)(Math.random()*255);
    }

    void ball_collide(int[] x, int[] y, int[] x_speed, int[] y_speed, int[] size){
        for(int i=0; i<x.length-1; i++){
            for(int j=i+1; j<x.length; j++){
                int distance = (int)(Math.pow(x[i]-x[j], 2)+Math.pow(y[i]-y[j], 2));
                if(distance < Math.pow((size[i]+size[j])/2, 2)){
                    boundary_collide(x_speed, color, i);
                    boundary_collide(y_speed, color, i);
                    boundary_collide(x_speed, color, j);
                    boundary_collide(y_speed, color, j);
                }
            }
        }
    }
}

大白与鸭子赛跑

代码展示:
大白
代码:

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

public class DaBai{
    public static void main(String[] args){
        JFrame f = new JFrame("动物");
        f.setSize(1920,1080);
        DB_Panel p = new DB_Panel();
        p.point();
        f.add(p);
        f.setVisible(true);
    }
}


class DB_Panel extends JPanel {
    int[] x = new int[100];
    int[] y = new int[100];
    int[] z = new int[100];
    int speed = 50;
    int j = 0;

    void point() {
        for( int i=0; i<100; i++) {
            x[i] = (int)(Math.random()*1920);
            y[i] = (int)(Math.random()*500+400);
            z[i] = (int)(Math.random()*20+10);
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        setBackground(Color.BLACK);
        //外嘴巴
        g.setFont(new Font("", Font.BOLD, 20));
        g.setColor(new Color(216, 185, 97));
        g.fillOval(860+speed*j, 845, 30, 15);
        //内嘴巴
        g.setColor(Color.YELLOW);
        g.fillOval(870+speed*j, 849, 10, 10);
        //身体
        g.setColor(Color.yellow);
        g.fillOval(785+speed*j, 800, 90, 90);
        g.fillOval(748+speed*j, 858, 120, 100);
        //眼睛
        g.setColor(Color.white);
        g.fillOval(830+speed*j, 820, 30, 30);
        g.setColor(Color.black);
        g.fillOval(835+speed*j, 825, 18, 18);
        //尾巴
        g.setColor(Color.yellow);
        g.fillOval(742+speed*j, 852, 30, 70);
        //翅膀
        g.setColor(new Color(251, 232, 128));
        g.fillOval(750+speed*j, 858, 90, 70);
        //纹路
        g.setColor(new Color(150, 136, 108));
        for (int i = 770+speed*j; i < 810+speed*j; i = i + 11) {
            g.drawString("~", i, 888);
            g.drawString("~", i, 910);
        }

        try{
            Thread.sleep(1100);
        }catch (Exception e){}
        j++;
        repaint();

        //画树
        g.setColor(Color.green);
        g.setFont(new Font("", Font.BOLD, 1080));
        g.drawString("♠", -100, 1000);
        g.setColor(Color.red);
        g.fillOval(10, 10, 100, 100);
        g.setColor(Color.black);
        g.fillRect(50,800, 600, 1080);
        g.setColor(new Color(176,137,80));
        g.fillRect(205,778, 177, 1080);

        //河水
        g.setColor(new Color(119,232,231));
        g.fillRect(0,950, 1920, 1080);
        g.setFont(new Font("",Font.BOLD,10));
        //河水闪耀的星星
        g.setColor(new Color(222,241,224));
        g.setFont(new Font("",Font.BOLD,10));
        for(int j=0;j<1200;j++) {
            g.drawString("*", (int)(Math.random()*1920),
                    (int)((Math.random()*100)+920));
        }

        //画花
        g.setColor(new Color(249,127,147));
        g.setFont(new Font("", Font.BOLD, 50));
        int [] a= {105,120,133,146,157,169,189,230,250,270,340,330,320,387,368};
        int [] b= {342,660,580,360,350,450,560,550,380,580,420,620,308,600,690};
        for(int c=0; c<15; c++) {
            g.drawString("❀", a[c], b[c]);
        }

        //花瓣飘落
        g.setColor(new Color(249,127,147));
        for(int i=0; i<100; i++) {
            g.setFont(new Font("",Font.BOLD,z[i]));
            g.drawString("❀",x[i] += (int)(Math.random()*4-2),y[i]+= (int)(Math.random()*1.3));

            if(y[i]>1000||x[i]>1900) {
                x[i] = (int)(Math.random()*1920) ;
                y[i] = (int)(Math.random()*1080) ;

            }
        }

        //大白
        g.setColor(Color.white);
        //身体
        g.fillOval(550+speed*j, 756, 80, 50);
        g.fillOval(528+speed*j, 779, 120, 165);
        //手臂和腿
        g.fillOval(519+speed*j, 815, 38, 90);
        g.fillOval(620+speed*j, 815, 38, 90);
        // 腿
        g.fillOval(536+speed*j, 880, 38, 90);
        g.fillOval(596+speed*j, 880, 38, 90);
        g.setColor(Color.black);
        g.fillOval(570+speed*j, 775, 10, 10);
        g.fillOval(605+speed*j, 775, 10, 10);
        g.drawLine(570+speed*j, 780, 607+speed*j, 780);
    }
}

汇总

代码展示:
期中作业
代码:

import java.awt.*;

public class Test {
    public static void main(String[] args) {
        Frame f = new Frame("期中作业");
        f.setSize(1200, 900);//屏幕大小
        MyPanel1 p = new MyPanel1();
        f.add(p);
        f.setVisible(true);//展示
    }
}


class MyPanel1 extends Panel {
    //太阳位置
    int sun_x = -50;
    int sun_y = 325;
    //雨水位置
    int[] rain_x = new int[100];
    int[] rain_y = new int[100];
    //球的位置
    int[] ball_x = new int[10];
    int[] ball_y = new int[10];
    //球的两个方向速度
    int[] ball_x_speed = new int[10];
    int[] ball_y_speed = new int[10];
    //球的颜色
    int[] ball_color = new int[30];
    //球的大小
    int[] ball_size = new int[10];
    //动物位置
    int[] animal_x = {600, 617, 750, 768, 605, 605, 650, 650, 590, 810, 510, 800, 512, 898, 517,
            907, 800, 575, 815, 590, 600, 670, 770, 680, 780, 685, 700, 700, 710, 740};
    int[] animal_y = {765, 900, 765, 900, 615, 655, 665, 705, 665, 665, 662, 662, 668, 668, 677,
            677, 465, 465, 480, 480, 465, 545, 545, 550, 550, 580, 595, 590, 585, 590};
    //动物走路运动控制(true:左脚,false:右脚)
    boolean control = true;
    int animal_speed = 50;//动物移动速度
    int num = 1;//非常关键的一个变量

    //初始化
    MyPanel1(){
        //雨水位置初始化
        for(int i=0; i<rain_x.length; i++){
            rain_x[i] = (int)(Math.random()*1200);
            rain_y[i] = (int)(Math.random()*900);
        }
        //球初始化
        for(int i=0; i<ball_x.length; i++){
            //位置初始化
            ball_x[i] = (int)(200+Math.random()*770);
            ball_y[i] = (int)(100+Math.random()*270);
            //速度初始化
            ball_x_speed[i] = (int)(1+Math.random()*5);
            ball_y_speed[i] = (int)(1+Math.random()*5);
            //颜色初始化
            ball_color[i*3] = (int)(Math.random()*255);
            ball_color[i*3+1] = (int)(Math.random()*255);
            ball_color[i*3+2] = (int)(Math.random()*255);
            //大小初始化
            ball_size[i] = (int)(Math.random()*10+20);
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        setBackground(Color.WHITE);//屏幕颜色
        g.setColor(Color.BLACK);//小球边框颜色
        g.drawRect(200, 100, 800, 300);
        //绘制太阳
        sun(g);
        //绘制雨水
        rain(g);
        //绘制球
        ball(g);
        //绘制动物
        animal(g);
        try {
            Thread.sleep(100);
        } catch (Exception e) {}
        repaint();//重新绘制
    }

    //动物
    void animal(Graphics g){
        //动物宽度与高度
        int[] animal_width = {100, 65, 100, 65, 250, 250, 150, 150, 60, 60, 150, 150,
                50, 50, 35, 35, 80, 80, 50, 50, 250, 20, 20, 5, 5, 100, 70, 70, 50, 12};
        int[] animal_height = {200, 70, 200, 70, 300, 250, 200, 150, 60, 60, 60, 60,
                50, 50, 35, 35, 80, 80, 50, 50, 220, 20, 20, 5, 5, 70, 50, 50, 30, 12};
        //边界条件
        for(int i=0; i<animal_x.length; i++){
            int width = animal_x[i]+animal_width[i];
            if(animal_x[i]<0 | width>1200){
                animal_speed = -animal_speed;//方向改变
                control = (control) ? false:true;//出发腿改变
                num++;
                break;
            }
        }
        if(control){//左腿走
            animal_x[0] += animal_speed;
            animal_x[1] += animal_speed;
            if(animal_speed<0){
                for (int i = 4; i < animal_x.length; i++) {//身体走
                    animal_x[i] += animal_speed;
                }
            }
        }else {//右腿走
            animal_x[2] += animal_speed;
            animal_x[3] += animal_speed;
            if(animal_speed>0){
                for (int i = 4; i < animal_x.length; i++) {//身体走
                    animal_x[i] += animal_speed;
                }
            }
        }
        if(num%2==0){
            animal_speed = -animal_speed;//方向改变
            control = (control) ? false:true;//出发腿改变
            num = 0;
        }
        control = (control) ? false:true;//出发腿改变
        body(g, animal_x, animal_y, animal_width, animal_height);//绘制身体
    }

    //动物身体
    void body(Graphics g, int[] x, int[] y, int[] animal_width, int[] animal_height){
        //左脚
        g.setColor(new Color(222, 130,74));
        g.fillOval(x[0], y[0], animal_width[0], animal_height[0]);
        g.fillOval(x[1], y[1], animal_width[1], animal_height[1]);
        //右脚
        g.fillOval(x[2], y[2], animal_width[2], animal_height[2]);
        g.fillOval(x[3], y[3], animal_width[3], animal_height[3]);
        //外身体
        g.setColor(new Color(222, 130,74));
        g.fillOval(x[4], y[4], animal_width[4], animal_height[4]);
        g.fillOval(x[5], y[5], animal_width[5], animal_height[5]);
        //内身体
        g.setColor(new Color(240,198,172));
        g.fillOval(x[6], y[6], animal_width[6], animal_height[6]);
        g.fillOval(x[7], y[7], animal_width[7], animal_height[7]);
        //手
        g.setColor(new Color(222, 130,74));
        g.fillOval(x[8], y[8], animal_width[8], animal_height[8]);
        g.fillOval(x[9], y[9], animal_width[9], animal_height[9]);
        g.fillOval(x[10], y[10], animal_width[10], animal_height[10]);
        g.fillOval(x[11], y[11], animal_width[11], animal_height[11]);
        g.fillOval(x[12], y[12], animal_width[12], animal_height[12]);
        g.fillOval(x[13], y[13], animal_width[13], animal_height[13]);
        g.setColor(new Color(240,198,172));
        g.fillOval(x[14], y[14], animal_width[14], animal_height[14]);
        g.fillOval(x[15], y[15], animal_width[15], animal_height[15]);
        //外耳朵
        g.setColor(new Color(222, 130,74));
        g.fillOval(x[16], y[16], animal_width[16], animal_height[16]);
        g.fillOval(x[17], y[17], animal_width[17], animal_height[17]);
        //内耳朵
        g.setColor(new Color(240,198,172));
        g.fillOval(x[18], y[18], animal_width[18], animal_height[18]);
        g.fillOval(x[19], y[19], animal_width[19], animal_height[19]);
        //面部
        g.setColor(new Color(222, 130,74));
        g.fillOval(x[20], y[20], animal_width[20], animal_height[20]);
        //外眼睛
        g.setColor(Color.BLACK);
        g.fillOval(x[21], y[21], animal_width[21], animal_height[21]);
        g.fillOval(x[22], y[22], animal_width[22], animal_height[22]);
        //内眼睛
        g.setColor(Color.WHITE);
        g.fillOval(x[23], y[23], animal_width[23], animal_height[23]);
        g.fillOval(x[24], y[24], animal_width[24], animal_height[24]);
        //脸部
        g.setColor(new Color(240,198,172));
        g.fillOval(x[25], y[25], animal_width[25], animal_height[25]);
        //外嘴唇
        g.setColor(Color.BLACK);
        g.fillOval(x[26], y[26], animal_width[26], animal_height[26]);
        //内嘴唇
        g.setColor(new Color(240,198,172));
        g.fillOval(x[27], y[27], animal_width[27], animal_height[27]);
        //外鼻子
        g.setColor(Color.BLACK);
        g.fillOval(x[28], y[28], animal_width[28], animal_height[28]);
        //内鼻子
        g.setColor(Color.WHITE);
        g.fillOval(x[29], y[29], animal_width[29], animal_height[29]);
    }

    //小球
    void ball(Graphics g){
        ball_collide(ball_x, ball_y, ball_x_speed, ball_y_speed, ball_size);
        for (int i = 0; i < ball_x.length; i++) {
            g.setColor(new Color(ball_color[i*3], ball_color[i*3+1], ball_color[i*3+2]));//球颜色
            //球左右运动
            ball_x[i] += ball_x_speed[i];
            if (ball_x[i] > 1000-ball_size[i] | ball_x[i] < 200) {
                collide(ball_x_speed, ball_color, i);
            }
            //球上下运动
            ball_y[i] += ball_y_speed[i];
            if (ball_y[i] > 400-ball_size[i] | ball_y[i] < 100) {
                collide(ball_y_speed, ball_color, i);
            }
            //绘制
            g.fillOval(ball_x[i], ball_y[i], ball_size[i], ball_size[i]);
        }
    }

    //碰撞改变
    void collide(int[] array, int[] color, int i){
        array[i] = -array[i];//方向改变
        //颜色改变
        color[i*3] = (int)(Math.random()*255);
        color[i*3+1] = (int)(Math.random()*255);
        color[i*3+2] = (int)(Math.random()*255);
    }

    //判断小球碰撞
    void ball_collide(int[] x, int[] y, int[] x_speed, int[] y_speed, int[] size){
        for(int i=0; i<x.length-1; i++){
            for(int j=i+1; j<x.length; j++){
                int distance = (int)(Math.pow(x[i]-x[j], 2)+Math.pow(y[i]-y[j], 2));
                if(distance <= Math.pow((size[i]+size[j])/2, 2)){
                    collide(x_speed, ball_color, i);
                    collide(y_speed, ball_color, i);
                    collide(x_speed, ball_color, j);
                    collide(y_speed, ball_color, j);
                }
            }
        }
    }

    //雨水
    void rain(Graphics g){
        //雨水初始大小
        int rain_size = 10;
        g.setColor(Color.BLACK);//雨水颜色
        for(int i=0; i<rain_x.length; i++){
            int range = (int)(15-Math.random()*5);//变化范围
            //雨水移动
            rain_x[i] = rain_x[i] + (int)(Math.random()*range-range/2);
            rain_y[i] = (int)(rain_y[i] + Math.random()*range);
            //绘制
            g.setFont(new Font("", 0, rain_size+range));
            g.drawString("|", rain_x[i], rain_y[i]);
            //边界判断
            if (rain_y[i]>900) {
                rain_y[i] = (int)(Math.random()*100);
            }
            if(rain_x[i]<0 && rain_x[i] >1200){
                rain_x[i] = (int)(Math.random()*1200);
            }
        }
    }

    //太阳
    void sun(Graphics g){
        g.setColor(Color.YELLOW);//太阳颜色
        //太阳两个方向的速度
        int sun_x_speed = 2;
        int sun_y_speed = 1;
        g.fillOval(sun_x, sun_y,50,50);
        //边界判断
        if(sun_x < 575){//上升
            sun_x += sun_x_speed;
            sun_y -= sun_y_speed;
        }
        else if(sun_x < 1250){//下降
            sun_x += sun_x_speed;
            sun_y += sun_y_speed;
        }
        else{//复原
            sun_x = -50;
            sun_y = 325;
        }
    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胆怯与勇敢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值