【java核心技术笔记】图形程序设计

来源:互联网 发布:大数据时代心得体会 编辑:程序博客网 时间:2024/06/05 15:14

图形程序设计


Swing概述

优势:

  • 拥有一个丰富、便捷的用户界面元素集合
  • 对底层平台的依赖很少,与平台相关的bug少
  • 给予不同平台的用户一致的感觉

创建框架

顶层窗口被称为框架。

public class SwingFrame extends JFrame{    public static int DEFAULT_WIDTH = 300;    public static int DETAULT_HEIGHT = 200;    public SwingFrame(){        setSize(DEFAULT_WIDTH, DETAULT_HEIGHT);//设置框架的大小    }}...//所有的Swing组件必须由事件调度线程进行配置,线程将外设控制转移到用户接口组件EventQueue.invokeLater(new Runnable() {    @Override    public void run() {        SwingFrame sFrame = new SwingFrame();        //定义用户关闭框架时的相应动作        sFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //框架起初是不可见的,所以要设置显示框架        sFrame.setVisible(true);    }});

框架

框架定位

  • setLocation、setBounds 设置框架位置
  • setIconImage 设置图标
  • setTitle 设置标题栏文字
  • setResizable 用户是否可以改变框架大小

框架属性

通过get/set获取/设置框架属性

框架大小

应检查屏幕分辨率,根据分辨率编写代码重置框架的大小。

public SizedFrame() {        //获取Toolkit对象        Toolkit kit = Toolkit.getDefaultToolkit();        //获取屏幕大小        Dimension screenSizeDimension = kit.getScreenSize();        int height = screenSizeDimension.height;        int width = screenSizeDimension.width;        //设置frame大小        setSize(width/2, height/2);        //定位框架,由平台来选择一个合适的位置        setLocationByPlatform(true);        //设置图标        Image img = kit.getImage("icon.png");        setIconImage(img);        //设置title        setTitle("my study of swing");      }

在组件中显示信息

应把组件添加到内容窗格中。绘制一个组件,需要定义一个扩展JComponent的类,并覆盖其中的painConponent方法。这个方法中有一个Graphics类型的参数,所有的绘制都必须使用Graphics对象。在应用程序需要重新绘图时,会自动调用painConponent方法。
JPanel是一个可以包含其他组件的容器,也可以在其上面进行绘制。与JComponent相比,painConponent不透明,所以需要在面板的边界内绘制所有的像素。其中最容易的实现的方法是,在painConponent方法中调用super.painConponent(g)来用背景色绘制面板。

class NotHellWorldPanel extends JPanel{    private static final int MESSAGE_X = 75;    private static final int MESSAGE_Y = 100;    public void painConponent(Graphics g){        g.drawString("not hello world", MESSAGE_X, MESSAGE_Y);    }}...NotHellWorldPanel panel = new NotHellWorldPanel();add(panel);

2D图形

Java 2D库可以实现功能强大的图形操作。Java 2D库采用浮点坐标,也可以使用double类型的坐标。
使用Graphics2D来绘制图形,可以绘制直线(Line2D)、矩形(Rectangle2D)、圆(Ellipse2D)。

import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.geom.Ellipse2D;import java.awt.geom.Line2D;import java.awt.geom.Rectangle2D;import javax.swing.JComponent;import javax.swing.JFrame;public class DrawFrame extends JFrame{    public static final int DEATULT_WIDTH = 400;    public static final int DEFAULT_HEIGHT = 400;    public DrawFrame(){        setSize(DEATULT_WIDTH, DEFAULT_HEIGHT);        setTitle("Java 2D");        DrawComponent drawComponent = new DrawComponent();        add(drawComponent);    }}class DrawComponent extends JComponent{    @Override    protected void paintComponent(Graphics g) {        Graphics2D graphics2d = (Graphics2D) g;        //矩形        double leftX = 100;        double topY = 100;        double width = 200;        double height = 150;        Rectangle2D rectangle2d = new Rectangle2D.Double(leftX,topY, width, height);        graphics2d.draw(rectangle2d);        //椭圆        Ellipse2D ellipse2d = new Ellipse2D.Double();        ellipse2d.setFrame(rectangle2d);        graphics2d.draw(ellipse2d);        //对角线        graphics2d.draw(new Line2D.Double(leftX, topY, leftX+width,  topY+height));        //圆        double centerX = rectangle2d.getCenterX();        double centerY = rectangle2d.getCenterY();        double radius = 150;        Ellipse2D circleEllipse2d = new Ellipse2D.Double();        circleEllipse2d.setFrameFromCenter(centerX, centerY, centerX+radius, centerY+radius);        graphics2d.draw(circleEllipse2d);    }}...EventQueue.invokeLater(new Runnable() {    @Override    public void run() {                 DrawFrame dFrame = new DrawFrame();        dFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        dFrame.setVisible(true);    }});

2D图形

颜色

graphics2d.setPaint(Color.RED);graphics2d.drawString("变色了", 200, 200);graphics2d.setPaint(Color.BLUE);graphics2d.fill(circleEllipse2d);//定制颜色graphics2d.setPaint(new Color(0, 128, 128));

特殊字体

//获取本地可使用的字体名String[] fontNamesStrings = GraphicsEnvironment    .getLocalGraphicsEnvironment()    .getAvailableFontFamilyNames();

字体

图像

import java.awt.Graphics;import java.awt.Image;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JComponent;import javax.swing.JFrame;public class ImageFrame extends JFrame{    public static final int DEATULT_WIDTH = 500;    public static final int DEFAULT_HEIGHT = 500;    public ImageFrame(){        setSize(DEATULT_WIDTH, DEFAULT_HEIGHT);        setTitle("image");        ImageComponent imageComponent = new ImageComponent();        add(imageComponent);    }}class ImageComponent extends JComponent{    private Image image;    public ImageComponent() {        try {            image = ImageIO.read(new File("icon.png"));        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    @Override    protected void paintComponent(Graphics g) {        if(image == null) return;//      int width = image.getWidth(this);//      int height = image.getHeight(this);        g.drawImage(image, 0,0, null);        g.drawString("ooooooooooooooo", 200, 200);    }}
原创粉丝点击