JAVA绘图技术

坐标体系

坐标原点位于左上角,以像素为单位,像素是计算机屏幕上最小的显示单位,在java的坐标系中,第一个是x坐标,表示当前位置位水平方向,距离坐标原点x个像素;第二个是y坐标,表示当前位置位垂直方向,距离坐标点y个像素。

绘图原理

Component类提供了两个绘图相关最要的方法:

  Ⅰ.paint(Graphics g)绘制组件的外观 (当组件第一次在屏幕显示的时候,程序会自动调用paint函数)

  Ⅱ.repaint()刷新组件的外观。

在以下情况paint()将会被调用:

  Ⅰ.窗口最小化,再最大化

  Ⅱ.窗口的大小发生变化

  Ⅲ.repaint函数被调用

 1 /*
 2  * 作者:白客C
 3  * 时间:2020年03月07日
 4  * 内容:绘图原理
 5  */
 6 
 7 package com.beekc.www;
 8 import java.awt.*;
 9 import javax.swing.*;
10 
11 //继承JFanme
12 public class Beekc extends JFrame{
13 
14     //定义组件
15     MyPanle myPanle;
16     public static void main(String[] args){
17         Beekc beekc =new Beekc();
18     }
19 
20     //构造函数
21     public Beekc(){
22         //创建组件
23         myPanle = new MyPanle();
24 
25         //设置布局管理器
26 
27         //添加组件
28         this.add(myPanle);
29 
30         //设置窗体
31         this.setTitle("绘图技术");
32         this.setSize(400,300);
33         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
34 
35         //显示
36         this.setVisible(true);
37     }
38 
39 }
40 
41 //定义一个自己的面板,用于绘图和实现绘图的区域
42 class MyPanle extends JPanel
43 {
44     //覆盖JPanel的paint方法
45     //Graphicss绘图必备类,可以把它当成画笔
46     public void paint(Graphics g)
47     {
48         //调用父类完成初始化
49         super.paint(g);
50         //画出一个(椭)圆
51         g.drawOval(10,10,50,50);
52         //画出直线
53         g.drawLine(80,20,130,50);
54         //画出矩形框
55         g.drawRect(160,15,50,40);
56         //画出填充矩形
57         g.fillRect(240,15,50,40);
58         //画出填充矩形并换颜色
59         g.setColor(Color.BLUE);
60         g.fillRect(310,15,50,40);
61         //画出填充(椭)圆
62         g.setColor(Color.GRAY);
63         g.fillOval(10,80,50,50);
64 
65         //画图片
66         //Image im = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/qq_bg.jpg"));
67         //实现图片
68         //g.drawImage(im,150,150,200,150,this);
69 
70         //画图片
71         Image im = Toolkit.getDefaultToolkit().getImage("qq_bg.jpg");
72         //实现图片
73         g.drawImage(im,80,130,150,90,this);
74 
75         //画字体
76         g.setColor(Color.red);
77         g.setFont(new Font("华文彩云",Font.BOLD,30));
78         g.drawString("中国万岁",90,110);
79 
80     }
81 }

绘图坦克实例

  1 /*
  2  * 作者:白客C
  3  * 时间:2019年03月08日
  4  * 功能:坦克游戏
  5  * 版本:1.0
  6  */
  7 package com.beekc.www;
  8 import java.awt.*;
  9 import javax.swing.*;
 10 
 11 //继承JFrame
 12 public class TankGame extends JFrame{
 13 
 14     //定义组件
 15     MyPanel mp = null;
 16 
 17     public static void main(String[] args) {
 18         TankGame tankGame = new TankGame();
 19     }
 20 
 21     //构造函数
 22     public TankGame(){
 23 
 24         //创建组件
 25         mp = new MyPanel();
 26 
 27         //添加组件
 28         this.add(mp);
 29 
 30         //设置窗体
 31         this.setTitle("坦克大战");
 32         this.setSize(400,300);
 33         this.setLocation(200,200);
 34         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 35 
 36         //显示
 37         this.setVisible(true);
 38     }
 39 
 40 }
 41 
 42 //我的面板
 43 class MyPanel extends JPanel
 44 {
 45     //定义一个我的坦克
 46     Hero hero = null;
 47 
 48     //构造函数
 49     public MyPanel()
 50     {
 51         hero = new Hero(100,100);
 52     }
 53     @Override
 54     //覆盖paint
 55     public void paint(Graphics g) {
 56         //画笔
 57         super.paint(g);
 58         //画出活动区域
 59         g.fillRect(0,0,400,300);
 60         //画出我的坦克
 61         this.drawTank(hero.getX(),hero.getY(), g,0,0);
 62     }
 63 
 64     //画出坦克的函数
 65     /*
 66      *x,y <--> 坐标
 67      *g <--> 画笔
 68      *direct <--> 方向
 69      * type <--> 类型
 70      */
 71     public void drawTank(int x, int y, Graphics g, int direct, int type)
 72     {
 73         //判断坦克类型
 74         switch (type)
 75         {
 76             case 0:
 77                 g.setColor(Color.CYAN);
 78                 break;
 79             case 1:
 80                 g.setColor(Color.yellow);
 81                 break;
 82         }
 83 
 84         //判断方向
 85         switch (direct)
 86         {
 87             //向上
 88             case 0:
 89                 //画出左边矩形
 90                 g.fill3DRect(x,y,5,30,false);
 91                 //画出有边矩形
 92                 g.fill3DRect(x+15,y,5,30,false);
 93                 //画出中间矩形
 94                 g.fill3DRect(x+5,y+5,10,20,false);
 95                 //画出圆
 96                 g.fillOval(x+4,y+10,10,10);
 97                 //画出线
 98                 g.drawLine(x+9,y+15,x+9,y);
 99                 break;
100              //
101             case 1:
102 
103         }
104     }
105 }
106 
107 //坦克类
108 class Tank
109 {
110     //坦克的横坐标
111     int x = 0;
112     //坦克的纵坐标
113     int y = 0;
114 
115     //构造函数
116     public Tank(int x ,int y){
117         this.x = x;
118         this.y = y;
119     }
120 
121     public int getX() {
122         return x;
123     }
124 
125     public void setX(int x) {
126         this.x = x;
127     }
128 
129     public int getY() {
130         return y;
131     }
132 
133     public void setY(int y) {
134         this.y = y;
135     }
136 }
137 
138 //我的坦克
139 class Hero extends Tank
140 {
141     //构造函数
142     public Hero(int x, int y) {
143         //通过super来调用父类其他重载的构造方法
144         super(x, y);
145     }
146 }

猜你喜欢

转载自www.cnblogs.com/beekc/p/12439613.html
今日推荐