The effect of Tetris in the JAVA SE framework

Source: Internet
Author: User

Mygame

 Packagecom.sun.c;ImportJava.awt.event.KeyListener;ImportCom.sun.v.myjpanel;ImportCom.sun.v.myjframe; Public classMygame { PublicMygame () {//TODO auto-generated Constructor stub    }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubMyjframe JFrame =NewMyjframe (); Myjpanel Panel=NewMyjpanel ();        Jframe.add (panel);    Jframe.addkeylistener (panel); }}
Myjframe
 Packagecom.sun.v;ImportJavax.swing.JFileChooser;ImportJavax.swing.JFrame;/** The MVC design pattern is used here, and the logic is separated from the page,*/ Public classMyjframeextendsjframe{ PublicMyjframe () { This. Settitle ("MyWorld");  This. setvisible (true);  This. SetSize (400,400);  This. Setdefaultcloseoperation ( This.    Exit_on_close); }     }

Myjpanel

 Packagecom.sun.v;ImportJava.awt.Graphics;Importjava.awt.event.KeyEvent;ImportJava.awt.event.KeyListener;ImportJavax.swing.JPanel;ImportCom.sun.m.dog; Public classMyjpanelextendsJPanelImplementskeylistener,runnable{ PublicMyjpanel () {//Myjpanel JP = new Myjpanel ();Thread td =NewThread ( This);    Td.start (); } Dog Dog=NewDog (20,20,2);  Public voidPaint (Graphics g) {Super. Paint (g); G.draw3drect (dog.x, DOG.Y,20, 20,false); } @Override Public voidkeytyped (KeyEvent e) {//TODO auto-generated Method Stub} @Override Public voidkeypressed (KeyEvent e) {//TODO auto-generated Method Stub//System.out.println ("Sunzhiyan");        Switch(E.getkeycode ()) { CaseKeyEvent.VK_W:dog.direct= 0;         Dog.move (Dog.direct);  Break;  CaseKeyEvent.VK_D:dog.direct= 1;         Dog.move (Dog.direct);  Break;  CaseKeyEvent.VK_S:dog.direct= 2;         Dog.move (Dog.direct);  Break;  CaseKeyEvent.VK_A:dog.direct= 3;         Dog.move (Dog.direct);  Break; }                 This. repaint (); } @Override Public voidkeyreleased (KeyEvent e) {//TODO auto-generated Method Stub} @Override Public voidrun () {//TODO auto-generated Method Stub         while(true){            Try{Thread.Sleep (1000); //System.out.println ("I was called up");Dog.direct = 2;                Dog.move (Dog.direct);  This. repaint (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                    }            }}

Dog

1  Packagecom.sun.m;2 3  Public classDog {4      Public intx;5      Public inty;6      Public intDirect;7      PublicDog (intXintYintDirect) {8          This. x =x;9          This. y =y;Ten          This. Direct =Direct; One     } A      Public voidMoveintDirect) { -         Switch(direct) { -          Case0: the              This. y--; -              Break; -          Case1: -              This. x + +; +              Break; -          Case2: +              This. y++; A              Break; at          Case3: -              This. x--; -              Break;  -         } -          -     } in}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.