数据类型详解

基本数据类型
Java有8种基本类型:

  数据类型  字节数  位数 默认值
整型 byte 1 8 0
short 2 16 0
int(默认值) 4 32 0
long 8 64 OL
浮点型 float 4 32 0.Of
double(默认值) 8 64 0.0d
布尔型 boolean 1 8 false
字符型 char 2 16 '\u0000'
转为int为0

引用数据类型
包括类(class)、接口(interface)、数组(array)等。


程序入口与结构

main方法规范
必须为public static void,参数为String[]数组:

public static void main(String[] args) {
    System.out.println(args[0]);  // 命令行参数
}

类名需与文件名一致(HelloWorld.javaclass HelloWorld)。


变量与运算符

变量声明
显式指定类型,支持局部变量(需初始化)和成员变量(有默认值):

int count = 10;  // 局部变量
double pi;       // 成员变量默认0.0

运算符分类

  • 算术:+, -, *, /, %(取余)
  • 关系:>, <, ==, !=,>=,<=
  • 逻辑:&&(与), ||(或), !(否)
  • 注释(Java运算符的优先级大部分符合数学运算顺序)

类与对象实践

类定义示例

public class Lx2 {
    public static void main(String[] args) {
        int x=0;
        for (int i = 2;i<=100;i+=2){
            x +=i;
            System.out.println("1~100的偶数和:"+x);

            }

        }
    }

对象实例化

DrawPad dp1 = new DrawPad();
dp1.showUI();

接口与实现

接口定义
仅声明方法,无实现:

public class DrawListen implements MouseListener {
    Graphics g2;
    int x1,y1,x2,y2;

实现类

public void mouseClicked(MouseEvent e) {
    System.out.println("鼠标点击");
}


public void mousePressed(MouseEvent e){
    System.out.println("鼠标按下");
    int x = e.getX();
    int y = e.getY();
    System.out.println("x坐标为:"+x+"y坐标为:"+y);
    x1 = x;
    y1 = y;}



public void mouseReleased(MouseEvent e){
    System.out.println("鼠标释放");
    int x = e.getX();
    int y = e.getY();
    System.out.println("x坐标为:"+x+"y坐标为:"+y);
    x2 = x;
    y2 = y;
    //生成x型
    int i=0;
        Color color1 =new Color (i,i,0);
        g2.setColor(color1);
        g2.drawLine(x1+i,y1+i,x2+2*i,y2+1);
        g2.drawLine(x2+i,y1+i,x1+i,y2+3*i);







}



public void mouseEntered(MouseEvent e){
    System.out.println("鼠标进入");
    int x = e.getX();
    int y = e.getY();}



public void mouseExited(MouseEvent e){
    System.out.println("鼠标离开");
    int x = e.getX();
    int y = e.getY();
g2.drawLine(x1,y1,x2,y2);
}


数组高级操作

动态初始化与遍历

public class Array {
    public static void main(String[] args) {
        // 动态初始化数组
        int[] scores = new int[4];
        scores[0] = 90;
        scores[1] = 85;
        
        // 遍历打印
        for (int i=0;i<score.length;i++) {
            System.out.println(arr[i]);  // 输出: 90, 85, 0, 0
        }
    }
}
 


界面开发进阶


JFrame为窗口,嵌套中间容器:

JFrame jf = new JFrame();
//使用jf调用他自己的set系列方法 设置相关基础属性 标题 尺寸 位置 关闭方式 可视化
jf.setTitle("画图板1.0");
jf.setSize(800,800);
jf.setLocation(100,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭时退出
jf.setVisible(true);

图像处理技术

package zzy0807;

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


//界面开发类:
//创建一个窗体
//设置好窗体的属性
//添加一组按钮
//给窗体添加键盘监听与鼠标监听器
public class ImageUI {
    ImageListen imageListen = new ImageListen();

    public void showUI(){
        JFrame jf = new JFrame("图像处理");
        jf.setSize(1000,800);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout fl = new FlowLayout();
        jf.setLayout(fl);
        //按钮
        JButton button1 = new JButton("加载图片");
        jf.add(button1);
        button1.addActionListener(imageListen);
        JButton button2 = new JButton("原图");
        jf.add(button2);
        button2.addActionListener(imageListen);
        JButton button3 = new JButton("灰度");
        jf.add(button3);
        button3.addActionListener(imageListen);
        JButton button4 = new JButton("去背景");
        jf.add(button4);
        button4.addActionListener(imageListen);//按钮添加监听器
        JButton button5 = new JButton("马赛克");
        jf.add(button5);
        button5.addActionListener(imageListen);
        JButton button6 = new JButton("氛围");
        jf.add(button6);
        button6.addActionListener(imageListen);
        JButton button7 = new JButton("油画");
        jf.add(button7);
        button7.addActionListener(imageListen);

         jf.setVisible(true);
        jf.addMouseListener(imageListen);//窗体监听鼠标点击事件
        //传画笔给监听器
        Graphics g = jf.getGraphics();
        imageListen.g =g;

    }
    public static void main(String[] args) {
        ImageUI imageUI = new ImageUI();
        imageUI.showUI();
    }



}
package zzy0807;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class ImageTools {
    BufferedImage bfimg;//图片在内存中的形式
    int[][] imgArr;//存储像素值
    int width;
    int height;
    int x = 50, y = 100;


    //加载图片
    public void loadImage(String path) {
        File file = new File(path);
        try {
            bfimg = ImageIO.read(file);//读取文件为照片
            width = bfimg.getWidth();
            height = bfimg.getHeight();
            //创建同等大小的二维数组
            imgArr = new int[width][height];
            //将像素移植一份到imgArr中
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    imgArr[i][j] = bfimg.getRGB(i, j);

                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public void drawImage(Graphics g) {
        g.drawImage(bfimg, x, y, null);
    }

    public void drawGrayImage(Graphics g) {
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int pix = imgArr[j][i];
                Color color = new Color(pix);//像素颜色值
                //拆分RGB 三通道值
                int red = color.getRed();
                int green = color.getGreen();
                int blue = color.getBlue();
                //计算灰度值
                //平均值
                int grey = (red + green + blue) / 3;
                //配比法
                int gray1 = (int) (red * 0.30 + green * 0.59 + blue * 0.11);
                int gray2 = red;
                Color color1 = new Color(gray2, gray2, gray2);
                g.setColor(color1);
                g.fillRect(x + i, y + j, 1, 1);
            }
        }
    }

    public void drawMosaicImage(Graphics g) {
        for (int i = 0; i < width; i += 10) {
            for (int j = 0; j < height; j += 10) {
                int pix = imgArr[i][j];
                Color color = new Color(pix);
                g.setColor(color);
                g.fillRect(x + i, y + j, 10, 10);
            }
        }
    }

    public void drawBinaryImage(Graphics g) {
        for (int i = 0; i < width; i += 10) {
            for (int j = 0; j < height; j += 10) {
                int pix = imgArr[i][j];
                Color color = new Color(pix);
                int red = color.getRed();
                int green = color.getGreen();
                int blue = color.getBlue();
                int gray = (int) (red * 0.30 + green * 0.59 + blue * 0.11);
                if (gray < 77) {
                    g.setColor(Color.black);
                } else {
                    g.setColor(Color.white);
                }
                g.fillRect(x + i, y +j, 10, 10);
            }
        }
    }

    //RGB修改
    public void drawStyleImage_01(Graphics g) {
        for (int i = 0; i < width; i += 1) {
            for (int j = 0; j < height; j += 1) {
                int pix = imgArr[i][j];
                Color color = new Color(pix);
                int red = color.getRed();
                int green = color.getGreen();
                int blue = color.getBlue();
                red *= 1.2;
                if (red > 255) {
                    red = 255;
                }
                blue *= 0.8;
                Color color1 = new Color(red, green, blue);
                g.setColor(color1);
                g.fillRect(x + i, y + j, 1, 1);
            }

        }
    }

    public void drawOilImage_01(Graphics g) {
        Random random = new Random();

        for (int i = 0; i < width; i += 3) {
            for (int j = 0; j < height; j += 3) {
                int pix = imgArr[i][j];
                Color color = new Color(pix);
                g.setColor(color);
                int w = random.nextInt(10) + 5;
                int h = random.nextInt(10) + 5;
                g.fillOval(x + i, y + j, w, h);

            }
        }
    }

    public void removeBG(Graphics g) {
        for (int i = 0; i < width; i += 1) {
            for (int j = 0; j < height; j += 1) {
                int pix = imgArr[i][j];
                Color color = new Color(pix);
                int red = color.getRed();
                int green = color.getGreen();
                int blue = color.getBlue();
                if (red > 125 && green > 125 && blue > 125) {
                    g.setColor(Color.GREEN);
                } else {
                    g.setColor(color);
                }
                g.fillRect(x + i, y + j, 1, 1);
            }
        }
    }
}
package zzy0807;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ImageListen implements MouseListener, ActionListener {
    //创建图片处理对象
    ImageTools imageTools = new ImageTools();
    //画笔
    Graphics g;
    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();//获取按钮的文本
        System.out.println(cmd);
        if (cmd.equals("加载图片")) {
            imageTools.loadImage("C:\\Users\\Lenovo\\OneDrive\\图片\\Camera Roll\\微信图片_2025-06-14_131339_696.jpg");
        } else if (cmd.equals("原图")){
                imageTools.drawImage(g);

            } else if (cmd.equals("灰度")) imageTools.drawGrayImage(g);
        else if (cmd.equals("去背景")) {
            imageTools.removeBG(g);
        }else if (cmd.equals("马赛克")){
            imageTools.drawMosaicImage(g);
        } else if (cmd.equals("油画")) {
            imageTools.drawOilImage_01(g);
        } else if (cmd.equals("氛围")) {
            imageTools.drawStyleImage_01(g);
        }


    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("按下-mousePressed");
        int x = e.getX();
        int y = e.getY();

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {



    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}


Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐