9.八方向的操作
基本思路:
我们要使用KeyLister的接口,然后分别使用按下的抽象方法和松开的方法。当我们松开的时候,我们把值赋值为false.
import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; class panel extends Panel implements KeyListener { //实现接听功能 int x=100; int y=100; boolean press_a; boolean press_w; boolean press_d; boolean press_s; public void paint(Graphics g){ Font f=new Font("黑体",Font.BOLD,50); g.setFont(f); Color c=new Color(0,0,0); g.setColor(c); g.drawString("*",x,y); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { int i=e.getKeyCode(); //当按下操作 if(i==37){ x=x-10; } if(i==38){ y=y-10; } if(i==39){ x=x+10; } if(i==40){ y=y+10; } switch (i){ case 37: press_a=true;break; case 38: press_w=true;break; case 39:press_d=true;break; case 40:press_s=true;break; default:break; } if (press_a&&press_s){ x=x-10; y=y+10; } if (press_a&&press_w){ x=x-10; y=y-10; } if (press_w&&press_d){ x=x+10; y=y-10; } if (press_d&&press_s){ x=x+10; y=y+10; } repaint(); } @Override public void keyReleased(KeyEvent e) { int i=e.getKeyCode(); //当松开操作 switch (i){ case 37: press_a=false;break; case 38: press_w=false;break; case 39:press_d=false;break; case 40:press_s=false;break; default:break; } } }
import java.awt.*; public class hello { public static void main(String []avgs){ Frame f=new Frame("小球移动八个方向"); f.setSize(800,800); f.setLocationRelativeTo(null); panel p=new panel(); f.add(p); f.addKeyListener(p); p.addKeyListener(p); f.setVisible(true); } }
(二)、鼠标操作(MouseLister)
1.MouseListener单击操作
基本知识点:调用MouseLister接口,然后使用构造方法mouseClicked(),然后通过 e.getX()获得,x的坐标。通过e.getY()获得y的坐标。即可
import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; class panel extends Panel implements MouseListener { int []x=new int[20]; int []y=new int[20]; String []s1=new String[]{"1","2","1","2","1","2","1","2","1","2",}; //利用数组进行储存 int j=0; //画框架 public void paint(Graphics g){ //设置颜色 Color c=new Color(0,0,0); g.setColor(c); //矩形 g.drawRect(200,200,450,450); //线 g.drawLine(200,350,650,350); g.drawLine(200,500,650,500); g.drawLine(350,200,350,650); g.drawLine(500,200,500,650); if(j%2==0) { Color c1 = new Color(255, 0, 0); g.setColor(c1); } Font f=new Font("黑",Font.BOLD,30); g.setFont(f); x[0]=100; y[0]=100; for(int i=0;i<j+1;i++) { g.drawString(s1[i], x[i], y[i]); } //判断输赢 j++; } @Override public void mouseClicked(MouseEvent e) { x[j]=e.getX(); y[j]=e.getY(); if(x[j]>200&&x[j]<350&&y[j]>200&&y[j]<350){ x[j]=275; y[j]=275; }if(x[j]>200&&x[j]<350&&y[j]>350&&y[j]<500){ x[j]=275; y[j]=425; }if(x[j]>200&&x[j]<350&&y[j]>500&&y[j]<650){ x[j]=275; y[j]=575; } if(x[j]>350&&x[j]<500&&y[j]>350&&y[j]<500) { x[j]=425; y[j]=425; }if(x[j]>350&&x[j]<500&&y[j]>200&&y[j]<350) { x[j]=425; y[j]=275; }if(x[j]>350&&x[j]<500&&y[j]>500&&y[j]<650) { x[j]=425; y[j]=575; } if(x[j]>500&&x[j]<650&&y[j]>500&&y[j]<650){ x[j]=575; y[j]=575; }if(x[j]>500&&x[j]<650&&y[j]>200&&y[j]<350){ x[j]=575; y[j]=275; }if(x[j]>500&&x[j]<650&&y[j]>350&&y[j]<500){ x[j]=575; y[j]=425; } repaint(); } @Override public void mousePressed(MouseEvent e) { System.out.println("鼠标被按下:"); } @Override public void mouseReleased(MouseEvent e) { System.out.println("鼠标被松开"); } @Override public void mouseEntered(MouseEvent e) { System.out.println("----------鼠标进入框体--------"); } @Override public void mouseExited(MouseEvent e) { System.out.println("*********鼠标离开框体**********"); } //实现接听功能
import java.awt.*; public class hello { public static void main(String []avgs){ Frame f=new Frame("小球移动八个方向"); f.setSize(900,900); f.setLocationRelativeTo(null); panel p=new panel(); f.add(p); f.addMouseListener(p); p.addMouseListener(p); f.setVisible(true); } }
2.MouseMotinListener(移动操作)
import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; class panel extends Panel implements MouseMotionListener { int x=100; int y=100; public void paint(Graphics g) { g.fillOval(x,y,20,20); } @Override public void mouseDragged(MouseEvent e) { //鼠标按下并拖动 // x=e.getX(); // y=e.getY(); // repaint(); } @Override public void mouseMoved(MouseEvent e) { //鼠标移动不用按下 x=e.getX(); y=e.getY(); repaint(); } }
import java.awt.*; public class hello { public static void main(String []avgs){ Frame f=new Frame("小球移动八个方向"); f.setSize(900,900); f.setLocationRelativeTo(null); panel p=new panel(); f.add(p); f.addMouseMotionListener(p); p.addMouseMotionListener(p); f.setVisible(true); } }
(三)、窗体事件(WindowListener)
import java.awt.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; class panel extends Panel implements WindowListener { public void paint(Graphics g){ g.fillOval(10,10,100,100); } @Override public void windowOpened(WindowEvent e) { System.out.println("窗口被打开"); } @Override public void windowClosing(WindowEvent e) { //右上角的按钮 System.out.println("窗口已经被关闭---------"); System.exit(0); } @Override public void windowClosed(WindowEvent e) { // } @Override public void windowIconified(WindowEvent e) { System.out.println("窗口从正常状态转换为最小化"); } @Override public void windowDeiconified(WindowEvent e) { System.out.println("窗口从最小化转换为正常状态"); } @Override public void windowActivated(WindowEvent e) { System.out.println("窗口设置为活动窗口"); } @Override public void windowDeactivated(WindowEvent e) { System.out.println("窗口已经不是活动窗口"); } }
import java.awt.*; public class hello { public static void main(String []avgs){ Frame f=new Frame("小球移动八个方向"); f.setSize(900,900); f.setLocationRelativeTo(null); panel p=new panel(); f.add(p); f.addWindowListener(p); f.setVisible(true); } }
(四)、综合测试:打字幕游戏
import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random; class panel extends Panel implements Runnable, KeyListener { int sum=0; String []s=new String[]{"a","b","c","d","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; int []x=new int[500]; int []y=new int[550]; //对数组进行自定义初始化 public panel(){//初始化数组 for(int i=0;i<10;i++){ Random r=new Random(); int m1=r.nextInt(461)+1; x[i]=m1; } for(int i=0;i<10;i++){ Random r=new Random(); int m2=r.nextInt(483)+1; y[i]=m2; } for(int i=0;i<10;i++){ Random r=new Random(); int idex=r.nextInt(25); s[i]=s[idex]; } } //进行绘画的操作 public void paint(Graphics g){ int m=0; Color c=new Color(255,0,0); g.setColor(c); Font f=new Font("黑体",1,30); g.setFont(f); while (m++<10){ g.drawString(s[m],x[m],y[m]); } g.fillRect(0,0,200,50); g.setColor(Color.BLACK); g.drawString("您得分为:"+sum,0,30); } //进行移动的操作 @Override public void run() { while (true){ for(int i=0;i<10;i++){ y[i]++; if(y[i]>=483){ Random r=new Random(); int m1=r.nextInt(461)+1; x[i]=m1; y[i]=0; int idex=r.nextInt(25); s[i]=s[idex]; } } try { Thread.sleep(10); } catch (InterruptedException e) { System.out.println("错误"); break; }repaint(); } } //进行删除的操作 @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { char keyc=e.getKeyChar(); int NowHeight=-1; int NoeIdex=-1; Random r=new Random(); for (int i = 0; i <10; i++) { if (keyc==s[i].charAt(0)){ if(y[i]>NowHeight){ //进行求最大值 y坐标记录运行 NowHeight=y[i]; NoeIdex=i; } } } if(NoeIdex!=-1){ y[NoeIdex]=0; int m1=r.nextInt(461)+1; int m2=r.nextInt(25); x[NoeIdex]=m1; s[NoeIdex]=s[m2]; sum=sum+10; } } @Override public void keyReleased(KeyEvent e) { } }
import java.awt.*; public class hello { public static void main(String []avgs){ Frame f=new Frame("小球移动八个方向"); f.setSize(500,500); f.setLocationRelativeTo(null); panel p=new panel(); Thread t=new Thread(p); t.start(); f.add(p); f.addKeyListener(p); p.addKeyListener(p); f.setVisible(true); } }