涉及的技术需求
1. java面向对象编程
2. 界面编程
3. 绘图技术
绘图原理
paint(Graphics g)绘制组件的外观
paint()组件被调用:窗口大小发生改变时、repaint函数被调用时
paint可以绘制的图形需掌握主要:
1. drawLine直线,
2. drawRect矩形边框,
3. drawOval椭圆边框,
4. fillRect填充矩形,
5. fillOval填充椭圆,
6. drawImage画图片,
7. drawString画字符串,
8. setFont设置画笔的字体,
9. setColor设置画笔的颜色
6. Image im = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/拿督 (2).png"));
g.drawImage(im,90,90,500,300,this);//(图片,位置x,位置y,图片长度,图片宽度,放置在JFrame)
7. g.setColor(Color.blue);//设置颜色
g.setFont(new Font("隶书",Font.BOLD,30));//如果找不到字体就默认宋体,BOLD字体大小
g.drawString("祖国万岁", 100, 100);
repaint()刷新组件的外观//比如运动的坦克(动感)
package com.zhongjiamin.java;
import java.awt.*;
import javax.swing.*;
public class tank1 extends JFrame{
MyPanel mp = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
tank1 t1 = new tank1();
}
public tank1()
{
mp = new MyPanel();
this.add(mp);
this.setSize(1000, 1000);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//定义一个MyPanel(我自己的面板,用于绘图和实现绘图的区域)
class MyPanel extends JPanel
{
//覆盖JPanel的paint方法
//Graphics 是绘图的重要类,可以理解成一支画笔
public void paint(Graphics g)
{
//1.调用父类函数完成初始化
//super不能少
super.paint(g);
// g.drawOval(50, 50, 100, 100);
// g.drawLine(10, 10, 40, 10);
// g.drawRect(50, 50, 100, 50);
// g.setColor(Color.blue);//设置填充的颜色
// g.fillRect(200, 200, 100, 100);
// g.setColor(Color.red);
// g.fillOval(100, 0, 50, 50);
//在画板上画出图片
// Image im = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/拿督 (2).png"));
// g.drawImage(im,90,90,500,300,this);
//如何画出字
g.setColor(Color.blue);//设置颜色
g.setFont(new Font("隶书",Font.BOLD,30));//如果找不到字体就默认宋体,BOLD字体大小
g.drawString("祖国万岁", 100, 100);
}
}
绘制坦克
package com.zhongjiamin.java;
import java.awt.*;
import javax.swing.*;
public class tank1 extends JFrame{
MyPanel mp = null;
public static void main(String[] args) {
tank1 t1 = new tank1();
}
public tank1()
{
mp = new MyPanel();
this.add(mp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel
{
HeroTank ht = null;
public MyPanel()
{
ht = new HeroTank(100,100);
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(ht.getX(), ht.getY(), g, 0, 0);
}
//画坦克
public void drawTank(int x,int y,Graphics g,int direction,int type)
{
switch(type)
{
case 0:
g.setColor(Color.blue);
break;
case 1:
g.setColor(Color.red);
break;
}
switch(direction)
{
case 0:
g.fill3DRect(x, y,5,30,false);
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 15, 20,false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+15,x+10 , y);
}
}
}
class Tank
{
//设置坦克的坐标
int x=0;
int y=0;
public Tank(int x,int y)
{
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
class HeroTank extends Tank
{
public HeroTank(int x,int y)
{
super(x,y);
}
}
class EnemyTank extends Tank
{
public EnemyTank(int x,int y)
{
super(x,y);
}
}
4.事件处理机制
package com.zhongjiamin.java1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;//对事件处理所对应的包
public class shijianchuli extends JFrame implements ActionListener{
MyPanel mp = null;
JButton jb1,jb2;
public static void main(String[] args) {
// TODO Auto-generated method stub
shijianchuli sjcl = new shijianchuli();
}
public shijianchuli()
{
mp = new MyPanel();
jb1 = new JButton("红色");
mp.setBackground(Color.red);
jb2 = new JButton("黑色");
mp.setBackground(Color.black);
//注册监听
jb1.addActionListener(this);//监听主函数
jb1.setActionCommand("红色");
jb2.addActionListener(this);
jb2.setActionCommand("黑色");
this.add(jb1,BorderLayout.NORTH);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
this.setSize(500, 300);
this.setLocation(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//对事件处理的方法
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("红色"))
{
mp.setBackground(Color.red);
}else if(e.getActionCommand().equals("黑色"))
{
mp.setBackground(Color.black);
}else
{
System.out.println("不知道");
}
}
}
class MyPanel extends JPanel
{
public void paint(Graphics g)
{
super.paint(g);
}
}
5. 多线程
1.继承Thread类
package com.zhongjiamin.java1;
import java.util.*;
public class shijianchuli{
public static void main(String []args) {
MyThread mythread = new MyThread();
mythread.start();//启动分支栈,只需要一秒
for(int i=0;i<100;i++)
{
System.out.println("主线程-------->"+i);
}
}
}
class MyThread extends Thread
{
@Override
public void run()//重写run
{
for(int i=0;i<100;i++)
{
System.out.println("分支线程-------->"+i);
}
}
}
2.继承Runnable接口*****************
package com.zhongjiamin.java1;
import java.util.*;
public class shijianchuli{
public static void main(String []args) {
MyRunnable mr = new MyRunnable();
Thread t = new Thread(mr);
//Thread t = new Thread(new MyRunnable);
t.start();
for(int i=0;i<100;i++)
{
System.out.println("主线程-------->"+i);
}
}
}
class MyRunnable implements Runnable
{
@Override
public void run()//重写run
{
for(int i=0;i<100;i++)
{
System.out.println("分支线程-------->"+i);
}
}
}
2.继承Runnable接口*****************
package com.zhongjiamin.java1;
import java.util.*;
public class shijianchuli{
public static void main(String []args) {
Thread t = new Thread(new Runnable() {
public void run() {
for(int i=0;i<100;i++)
{
System.out.println("支线程-------->"+i);
}
}
});//匿名内部类
t.start();
for(int i=0;i<100;i++)
{
System.out.println("主线程-------->"+i);
}
}
}
2.继承Runnable接口*****************
package com.zhongjiamin.java1;
import java.util.*;
public class shijianchuli{
public static void main(String []args) {
Window w = new Window();
Thread t0 = new Thread(w);
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
t0.start();
t1.start();
t2.start();
}
}
class Window implements Runnable
{
private int num = 100;
public static String str = new String("weimeig");
public void run()
{
while(true)
{
synchronized(str)//只有这个类运行完后才能继续运行下一个类
{
if(num>0)
{
System.out.println(Thread.currentThread().getName()+"卖出第:"+num+"张票");
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
num--;
}else {
break;
}
}
}
}
}