初次发布编程学习的文章,也想在此记录我编程之路的点点滴滴
首先先记录一下时间:2024.8月开始接触编程
现在是2024.10.7,学习编程已有2个月
展示一下暑假期间完成的第一个小项目“图片修改器4.0”
import javax.swing.*;
import java.awt.*;
public class Show4 {
Listener4 li=new Listener4();
public void show(){
JFrame window=new JFrame("图片修改器4.0");
window.setSize(650,800);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
FlowLayout flow=new FlowLayout();
window.setLayout(flow);
window.setVisible(true);
String[] arr={"加载图片","原图","灰度","二值化","马赛克","反片","油画","轮廓画","红变绿","蓝变红"};
for (int i=0;i< arr.length;i++){
String text=arr[i];
JButton button=new JButton(text);
button.addActionListener(li);
window.add(button);
}
//画笔
Graphics g2=window.getGraphics();
li.g1=g2;
//鼠标
window.addMouseListener(li);
//滑杆
JSlider slider=new JSlider(0,255,128);
li.slide=slider;
JLabel la=new JLabel("二值化阈值:");
slider.addChangeListener(li);
window.add(la);
window.add(slider);
}
public static void main(String[] args) {
Show4 image=new Show4();
image.show();
}
}
import ImagePro24.Mouse;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class Listener4 implements ActionListener, MouseListener, ChangeListener {
int slidernumber;
JSlider slide;
Graphics g1;
BufferedImage img=null;
int w,h;
String btype;
public void actionPerformed(ActionEvent e){
String text=e.getActionCommand();
if (text.equals("加载图片")) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & PNG Images", "jpg", "png");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String path = chooser.getSelectedFile().getPath();
System.out.println("You chose to open this file: " + path);
File file = new File(path);
try {
img = ImageIO.read(file);// 加载图片
w = img.getWidth();
h = img.getHeight();// 加载宽高
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}else {
btype=text;
}
}
public void mouseClicked(MouseEvent e){
System.out.println("点击");
}
public void mousePressed(MouseEvent e){
System.out.println("按住");
int x=e.getX();
int y=e.getY();
if (btype.equals("原图")){
g1.drawImage(img,x,y,null);
} else if (btype.equals("灰度")) {
for ( int i=0;i<w;i++){
for (int j=0;j<h;j++){
int rgb=img.getRGB(i,j);
Color color1=new Color(rgb);
int red= color1.getRed();
int green=color1.getGreen();
int blue=color1.getBlue();
int gray=(red+green+blue)/3;
Color color2=new Color(gray,gray,gray);
g1.setColor(color2);
g1.fillRect(x+i,y+j,1,1);
}
}
} else if (btype.equals("二值化")) {
drawblackwhite(x,y);
} else if (btype.equals("马赛克")) {
drawmasaike(x,y);
} else if (btype.equals("反片")) {
drawopsite(x,y);
} else if (btype.equals("油画")) {
drawoil(x,y);
} else if (btype.equals("轮廓画")) {
drawsome(x,y);
} else if (btype.equals("红变绿")) {
drawredtogreen(x,y);
} else if (btype.equals("蓝变红")) {
drawbluetored(x,y);
}
}
public void mouseReleased(MouseEvent e){
System.out.println("释放");
}
public void mouseEntered(MouseEvent e){
System.out.println("进入");
}
public void mouseExited(MouseEvent e){
System.out.println("退出");
}
//《——————————封装分界线————————————》
//二值化
public void drawblackwhite(int x,int y){
for ( int i=0;i<w;i++){
for (int j=0;j<h;j++){
int rgb=img.getRGB(i,j);
Color color1=new Color(rgb);
int red= color1.getRed();
int green=color1.getGreen();
int blue=color1.getBlue();
int gray=(red+green+blue)/3;
if (gray<slidernumber){
g1.setColor(Color.black);
}else {
g1.setColor(Color.white);
}
g1.fillRect(x+i,y+j,1,1);
}}}
//马赛克
public void drawmasaike(int x,int y){
for ( int i=0;i<w;i+=10){
for (int j=0;j<h;j+=10){
int rgb=img.getRGB(i,j);
Color color1=new Color(rgb);
g1.setColor(color1);
g1.fillRect(x+i,y+j,10,10);
}
}
}
//反片
public void drawopsite(int x,int y){
for ( int i=0;i<w;i+=1){
for (int j=0;j<h;j+=1){
int rgb=img.getRGB(i,j);
Color color1=new Color(rgb);
int red= color1.getRed();
int green=color1.getGreen();
int blue=color1.getBlue();
Color color2=new Color(255-red,255-green,255-blue);
g1.setColor(color2);
g1.fillRect(x+i,y+j,1,1);
}
}
}
//油画
public void drawoil(int x,int y){
Random random=new Random();
for ( int i=0;i<w;i+=3){
for (int j=0;j<h;j+=3){
int rgb=img.getRGB(i,j);
Color color1=new Color(rgb);
int red= color1.getRed();
int green=color1.getGreen();
int blue=color1.getBlue();
int width= random.nextInt(5)+5;
int height=random.nextInt(5)+7;
g1.setColor(color1);
g1.fillRect(x+i,y+j,width,height);
}
}
}
//轮廓画
public void drawsome(int x,int y){
for ( int i=0;i<w-2;i++){
for (int j=0;j<h-2;j++){
int rgb=img.getRGB(i,j);
Color color1=new Color(rgb);
int red= color1.getRed();
int green=color1.getGreen();
int blue=color1.getBlue();
int gray=(red+green+blue)/3;
int rgb1=img.getRGB(i+2,j+2);
Color color2=new Color(rgb1);
int red1= color2.getRed();
int green1=color2.getGreen();
int blue1=color2.getBlue();
int gray1=(red1+green1+blue1)/3;
if(Math.abs(gray-gray1)<10){
g1.setColor(Color.white);
}else {g1.setColor(Color.black);}
g1.fillRect(x+i,y+j,1,1);
}
}
}
//红变绿
public void drawredtogreen(int x,int y){
for (int i=0;i<w;i++){
for (int j=0;j<h;j++){
int rgb= img.getRGB(i,j);
Color color=new Color(rgb);
int red= color.getRed();
int green= color.getGreen();
int blue= color.getBlue();
Color color1=new Color(green,red,blue);
g1.setColor(color1);
g1.fillRect(x+i,y+j,1,1);
}
}
}
//蓝变红
public void drawbluetored(int x,int y){
for (int i=0;i<w;i++){
for (int j=0;j<h;j++){
int rgb=img.getRGB(i,j);
Color color=new Color(rgb);
int red= color.getRed();
int blue= color.getBlue();
int green= color.getGreen();
Color color1=new Color(blue,green,red);
g1.setColor(color1);
g1.fillRect(x+i,y+j,1,1);
}
}
}
//滑杆
public void stateChanged(ChangeEvent e){
slidernumber=slide.getValue();
}
}