首页 > 技术文章 > Java GUI入门教程

mrzhang123 2015-12-15 09:50 原文

人生的烦恼,多在于知道的太多,而做的太少。

Java程序中开发GUI页面。下面我主要对Java中实现图像管理、图形绘制和颜色管理等做出介绍,与此同时,Swing包也做出一部分介绍。

像素与坐标

用于表示图像像素数目的称为图像分辨率。显示器用来显示图像的像素数目称为显示器分辨率
坐标:Java中的坐标系统是从屏幕的左上角为0,0
Java中的坐标系统

颜色表示

颜色的设置
用Color类,在Java中Color类将颜色按照RGB格式进行封装。RGB格式中,红蓝绿三原色的取值范围都是0~255。若某种颜色取值为0,则表示该颜色不起任何作用;若取值为255,则表示起最大作用。如果将三种颜色进行叠加就可以生成一种新的颜色,合成的RGB颜色的值为“000000”表示黑色,“ffffff”表示白色。首先调用Color的构造函数,产生一个Color对象,再用Graphics类里的setColor把它设置到Graphics对象上。
方法:setColor(Color c)
功能:设置图形对象的颜色为参数所指定的颜色

Color类的构造方法:
Color(int,int,int);
功能:根据指定0~255之间的红、绿、蓝(RGB)的值生成新的颜色。这样我们就可以创造出自己想要的颜色。Color中已经定义好的各种颜色常数我们也可以方便地直接使用:

英语 颜色
red
green 绿
blue
white
black
yellow
pink
cyan
orange
gray
darkGray 深灰
lightGray 浅灰

绘制图形

Graphics类提供多种几何图形功能, 通过它的方法可以实现画线、几何图形等。

画线

方法: drawLine(int x1, y1,int x2,int y2),
功能:在(x1,y1)和(x2,y2)这两个坐标之间画出一条直线,其中:
参数x1表示第1个点的x轴坐标
参数y1表示第1个点y轴坐标
参数x2表示第2个点x轴坐标
参数y2表示第2个点y轴坐标

绘制普通矩形

Java提供了3种矩形的绘制方式,我们先看普通型:

darwRect(int x,int y,int width,int height),它的功能是画出一个矩形,其中:
参数 x代表矩形左上角的x坐标
参数y代表矩形左上角的y坐标
参数width表示矩形的宽度
参数height表示矩形的长高度
方法:fillRect(int x,int y,int width,int height),
功能:使用当前图形对象的颜色填充参数指定的矩形,其中:
参数x为x轴坐标
参数y为y轴坐标
参数width为矩形的宽度
参数height为矩形的高度

绘制立体矩形

方法:draw3DRect(int x,int y,int width,int height,boolean raised);和fill3DRect(int x,int y,int width,int height,boolean raised);
方法draw3DRect(int x,int y,int width,int height,boolean raised):其作用为绘制一个突出的三维立体矩形,其中:
参数x为x轴坐标
参数y为y轴坐标
参数width为矩形的宽度
参数height为矩形的高度
参数raised表示立体矩形的状态,raised为true,则立体矩形为凸起状态;raised为false,则立体矩形为凹下状态。
方法draw3DRect(int x,int y,int width,int height,boolean raised);其作用为绘制一个突出的三维立体矩形并使用当前图形对象的颜色对其进行填充,其中:
参数x为x轴坐标
参数y为y轴坐标
参数width为矩形的宽度
参数height为矩形的高度
参数raised表示立体矩形的状态,raised为true,则立体矩形为凸起状态;raised为false,则立体矩形为凹下状态。

绘制圆角矩形

方法:drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)和fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)。
方法drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)其作用为绘制一个圆角矩形,其中:
参数x为x轴坐标
参数y为y轴坐标
参数width为矩形的宽度
参数height为矩形的高度
参数arcWidth为圆弧的横向直径
参数arcHeight为圆弧的纵向直径
方法fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)其作用为绘制一个圆角矩形并使用当前图形对象的颜色对其进行填充,其中:
参数x为x轴坐标
参数y为y轴坐标
参数width为矩形的宽度
参数height为矩形的高度
参数arcWidth为圆弧的横向直径
参数arcHeight为圆弧的纵向直径

Swing

主要理解组件、事件和监听器的关系和应用。
应用示例:

package lbz.jsdf.c04;

import javax.swing.JFrame;

import lbz.jsdf.c03.RocketPanel;

/** 
 * @author LbZhang
 * @version 创建时间:2015年12月14日 下午10:15:28 
 * @description 
 * 测试使用Swing的一些常见用法
 * 
 * 抽象窗口工具包(AWT)
 * 和Swing包
 */
public class PushCounter {

    public static void main(String[] args) {
        JFrame jframe = new JFrame("pushcounter");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jframe.getContentPane().add(new PushCounterPanel());
        jframe.pack();
        jframe.setVisible(true);
    }


}

///----------------------------
package lbz.jsdf.c04;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** 
 * @author LbZhang
 * @version 创建时间:2015年12月15日 上午9:29:55 
 * @description 类说明
 */
public class PushCounterPanel extends JPanel {

    private int count;
    private JButton pushBtn;
    private JLabel label;

    public PushCounterPanel(){
        count=0;
        pushBtn = new JButton("Push me");
        pushBtn.addActionListener(new ButtonListener());

        label = new JLabel("pushes:"+count);
        add(pushBtn);
        add(label);

        setBackground(Color.cyan);
        setPreferredSize(new Dimension(100,40));




    }

    private class ButtonListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            count++;
            label.setText("pusher:"+count);

        }

    }
}

下面给出几组源码样例作为分享

雪人

package lbz.jsdf.c01;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

/** 
 * @author LbZhang
 * @version 创建时间:2015年12月14日 下午5:14:11 
 * @description 类说明
 */
public class SnowmanPanel extends JPanel{

    private final int MID = 150;
    private final int TOP = 50;

    public SnowmanPanel(){
        setPreferredSize(new Dimension(300,225));
        setBackground(Color.cyan);//背景色的设置
    }

    /**
     * 当图形组件在屏幕上渲染的时候会自动调用该组件的paintComponent方法。
     * 注意:paintComponent方法接收一个Graphics对象作为参数,Graphics对象
     * 定义一个特定图像的上下文,我们可以与之交互。
     * 
     * draw a snowman
     */
    public void paintComponent(Graphics page){
        //确保背景色的绘制
        super.paintComponent(page);

        page.setColor(Color.blue);
        page.fillRect(0, 175, 300, 50);//Ground

        page.setColor(Color.yellow);
        page.fillOval(-50, -50, 80, 80);

        page.setColor(Color.white);
        page.fillOval(MID-20, TOP, 40, 40);
        page.fillOval(MID-35, TOP+35, 70, 50);
        page.fillOval(MID-50, TOP+80, 100, 60);

        page.setColor(Color.black);
        page.fillOval(MID-10, TOP+10, 5, 5);
        page.fillOval(MID+10, TOP+10, 5, 5);

        page.drawArc(MID-10, TOP+20, 20, 20, 190, 160);

        page.drawLine(MID-25, TOP+60, MID-50, TOP+20);
        page.drawLine(MID+55, TOP+60, MID+55, TOP+60);

        page.drawLine(MID-20, TOP+5, MID+20, TOP+5);

        page.fillRect(MID-15, TOP-20, 30, 25);
    }


}

雪人

火箭

package lbz.jsdf.c03;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

/**
 * @author LbZhang
 * @version 创建时间:2015年12月14日 下午9:30:42
 * @description 类说明
 */
public class RocketPanel extends JPanel {

    private int[] xRocket = { 100, 120, 120, 130, 130, 70, 70, 80, 80 };
    private int[] yRocket = { 15, 40, 115, 125, 150, 150, 125, 115, 40 };

    private int[] xWindow = { 95, 105, 110, 90 };
    private int[] yWindow = { 45, 45, 70, 70 };

    private int[] xFlame = {70,70,75,80,90,100,110,115,120,130,130};
    private int[] yFlame = {155,170,165,190,170,175,160,185,160,175,155};



    public RocketPanel() {
        setBackground(Color.cyan);
        setPreferredSize(new Dimension(200, 200));

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.red);
        g.fillPolygon(xRocket, yRocket, xRocket.length);

        g.setColor(Color.black);
        g.fillPolygon(xWindow, yWindow, xWindow.length);

//      g.drawPolyline(xFlame, yFlame, xFlame.length);

        g.setColor(Color.red);
        g.drawPolyline(xFlame, yFlame, xFlame.length);

    }
}

火箭

多个圆

package lbz.jsdf.c02;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

/** 
 * @author LbZhang
 * @version 创建时间:2015年12月14日 下午9:17:54 
 * @description 类说明
 */
public class SplatPanel extends JPanel {

    private Circle cir1,cir2,cir3,cir4,cir5;

    public SplatPanel(){
        cir1 = new Circle(30, Color.red, 70, 35);
        cir2 = new Circle(50, Color.green, 20, 20);
        cir3 = new Circle(100, Color.cyan, 60, 85);
        cir4 = new Circle(45, Color.yellow, 170, 30);
        cir5 = new Circle(60, Color.blue, 200, 60);

        setPreferredSize(new Dimension(300,200));
        setBackground(Color.black);

    }

    /**
     * 自动渲染函数的构建
     */
    public void paintComponent(Graphics gp){
        super.paintComponent(gp);

        cir1.draw(gp);
        cir2.draw(gp);
        cir3.draw(gp);
        cir4.draw(gp);
        cir5.draw(gp);
    }
}

package lbz.jsdf.c02;

import java.awt.Color;
import java.awt.Graphics;

/** 
 * @author LbZhang
 * @version 创建时间:2015年12月14日 下午9:19:17 
 * @description 类说明
 */
public class Circle {

    private int diameter,x,y;
    private Color color;

    public Circle(int size,Color shade,int upperX,int upperY){
        this.diameter = size;
        this.color = shade;
        this.x = upperX;
        this.y = upperY;
    }

    public void draw(Graphics g){

        g.setColor(color);
        g.fillOval(x, y, diameter, diameter);

    }

}

自管理

推荐阅读