多字符下落实现

package paint;
	
	import java.awt.Paint;
import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import java.awt.event.KeyEvent;
	import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JButton;
	import javax.swing.JFrame;
import javax.swing.JOptionPane;
	
	public class PaintMain {
		JFrame jFrame;
		String []strings= {"a","b","c","d","e","f"};
		public static void main(String[] args) {
			// TODO 自动生成的方法存根
			PaintMain testMain=new PaintMain();
			testMain.initView();
		}
		
	public void initView()
	{
		
		jFrame =new JFrame();
		jFrame.setBounds(500, 500, 500, 500);
		jFrame.setLayout(null);
		ArrayListlist=new ArrayList();
		Data d1=new Data("a", 100, 100);
		list.add(d1);
		Data d2=new Data("b", 200, 100);
		list.add(d2);
		MyCanvas myCanvas=new MyCanvas(list);//画布 
		// 这个方法只适合单一的一个字符下落
		myCanvas.setBounds(100,100, 500, 500);
		myCanvas.addKeyListener(new KeyListener() {
			//键盘事件 
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				//按键响应
				// TODO 自动生成的方法存根
				char ch=e.getKeyChar();//存按下的字符;
				String string =String.valueOf(ch);
				//1获取当前集合
				//2遍历集合
				//3判断是否相等
				//4如果相等,随机获取新的字符替换旧的字符,修改y坐标,从新绘制界面
				for(int i=0;ilist=myCanvas.getList();
							for(int i=0;i

子函数1

package paint;

import java.awt.Canvas;
import java.awt.Graphics;
import java.lang.reflect.Array;
import java.util.ArrayList;



public class MyCanvas extends Canvas{
	//用构造函数进行初始化,但是又有局限性,写死了
	
	private ArrayList list=new ArrayList();
	public MyCanvas(ArrayListlist) {
		// TODO Auto-generated constructor stub
	
		this.list=list;
	}
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		for(int i=0;i getList() {
		return list;
	}
	public void setList(ArrayList list) {
		this.list = list;
	}
}

子函数2

package paint;

public class Data {
	private int xString;
	private int  yString;
	private String name;
	public Data(String name,int xString,int yString) {
		// TODO 自动生成的构造函数存根
		this.xString=xString;
		this.yString=yString;
		this.name=name;
		
	}
	public int getxString() {
		return xString;
	}
	public void setxString(int xString) {
		this.xString = xString;
	}
	public int getyString() {
		return yString;
	}
	public void setyString(int yString) {
		this.yString = yString;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}

你可能感兴趣的:(java)