Java基础学习2

补充

String类型的应用

package com.precisee;

import java.util.Scanner;

public class change {
    public static void main(String[] args) {
        System.out.print("请输入一个十进制整数:");
        Scanner input = new Scanner(System.in);
        int s = input.nextInt();
        String str = "";
        while (s > 0) {


            str = s % 2 + str;
            s = s / 2;

        }
        System.out.print(str);
  }
}

Awt绘图

组件绘图原理

Component中提供了三个方法来完成组件图形的绘制与刷新:
paint(Graphics g);绘制组件的外观
update(Graphics g);内部调用paint方法,刷新组件外观;
repaint();调用update方法,刷新组件外观;
注:paint方法是Component和Graphics的桥梁
在这里插入图片描述

Graphics

AWT中提供了Canvas类充当画布,提供了Graphics类来充当画笔,通过调用Graphics对象的setColor()方法可以给画笔设置颜色。
Graphics类中常用的一些方法
画图的步骤:
1.自定义类,继承Canves类,重写paint(Graphics g)方法来完成画图;
2.在paint()方法内,调用Graphics对象的setColor()、setFont()等方法设置画笔的颜色、字体等属性。
3.调用Graphics画笔的drawXxx()方法开始画图。
案例:
案例

package com.Demo;

import com.sun.org.apache.regexp.internal.RE;

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

public class Demo2 {

    private final String RECT_SHAPE="rect";
    private final String OVAL_SHAPE="oval";

    private Frame frame =new Frame("这里测试绘图");

    Button btnReact =new Button("绘制矩形");
    Button btnOval =new Button("绘制椭圆");

    private String shape="";

    private class MyCanvas extends Canvas{
        @Override
        public void paint(Graphics g) {
            if(shape.equals(RECT_SHAPE)){
                g.setColor(Color.BLACK);
                g.drawRect(100,100,150,100);
            }else if(shape.equals(OVAL_SHAPE)){
                g.setColor(Color.RED);
                g.drawOval(100,100,150,150);
            }
        }
    }
    MyCanvas drawArea=new MyCanvas();

    public void init(){
        btnReact.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shape=RECT_SHAPE;
                drawArea.repaint();
            }
        });
        btnOval.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shape=OVAL_SHAPE;
                drawArea.repaint();
            }
        });
        Panel p=new Panel();
        p.add(btnReact);
        p.add(btnOval);

        frame.add(p,BorderLayout.SOUTH);

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

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

位图处理

AWT也允许在组件上绘制位图,Graphics提供了drawlmage(image image)方法用于绘图,该方法需要提供一个lmage参数–代表位图。
位图使用步骤:
1.创建Image的子类对象Bufferedlmage(int width,int height,int ImageType)创建时需要指定位图的宽高及类型
属性:此时相当于在内存中生成了一张图片:
2.调用Bufferedlmage)对象的getGraphicso)方法获取画笔,此时就可以往内存中的这张图片上绘图了,绘图的方法
和之前学习的一模一样;
3,调用组件paint)方法中提供的Graphics对象的drawlmage(方法,一次性的内存中的图片Bufferedimages绘制

ImagelO的使用

在实际生活中,很多软件都支持打开本地磁盘已经存在的图片,然后进行编辑,编辑完毕后,再重新保存到本地磁盘。如果使用AWT要完成这样的功能,那么需要使用到ImageIO这个类,可以操作本地磁盘的图片文件。

方法说明
static BufferedImage read(File input)读取本地磁盘图片文件(所传参数为一个file对象)
static BufferedImage read(InputStream input)读取本地磁盘图片文件(所传参数为输入流)
static boolean write(RenderedImage im,String formatName,File output)往磁盘中输出图片文件

案例:

package com.Demo;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Demo3 {

        private Frame frame = new Frame("图片查看器");

        MenuBar menuBar = new MenuBar();
        Menu menu = new Menu("文件");
        MenuItem open = new MenuItem("打开");
        MenuItem save = new MenuItem("另存为");

        //声明BufferedImage对象,记录本地存取到内存中的图片
        BufferedImage image;
        private class MyCanvas extends Canvas{
            @Override
            public void paint(Graphics g) {
                g.drawImage(image,0,0,null);
            }
        }
        MyCanvas drawArea = new MyCanvas();

        public void init() throws Exception{
            //组装视图

            open.addActionListener(e->{
                //打开一个文件对话框
                FileDialog fileDialog = new FileDialog(frame,"打开图片",FileDialog.LOAD);
                fileDialog.setVisible(true);

                //获取用户选择的图片路径以及名称
                String dir = fileDialog.getDirectory();
                String fileName = fileDialog.getFile();

                try {
                    image = ImageIO.read(new File(dir,fileName));
                    drawArea.repaint();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            });

            save.addActionListener(e->{
                //展示一个文件对话框
                FileDialog fileDialog = new FileDialog(frame,"保存图片",FileDialog.SAVE);
                fileDialog.setVisible(true);

                //获取用户设置的保存路径以及文件名称
                String dir = fileDialog.getDirectory();
                String fileName = fileDialog.getFile();

                try {
                    ImageIO.write(image,"JPEG",new File(dir,fileName));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            });

            menu.add(open);
            menu.add(save);

            menuBar.add(menu);

            //把菜单条放入到窗口中
            frame.setMenuBar(menuBar);
            frame.add(drawArea);

            frame.setBounds(200,200,740,508);

            frame.setVisible(true);

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



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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值