Az előadás letöltése folymat van. Kérjük, várjon

Az előadás letöltése folymat van. Kérjük, várjon

V. labor Thread, animáció. Animáció A figurák a lépés kijelölése után nem rögtön az új helyen teremnek, hanem egyenes vonal mentén mozognak a cél felé.

Hasonló előadás


Az előadások a következő témára: "V. labor Thread, animáció. Animáció A figurák a lépés kijelölése után nem rögtön az új helyen teremnek, hanem egyenes vonal mentén mozognak a cél felé."— Előadás másolata:

1 V. labor Thread, animáció

2 Animáció A figurák a lépés kijelölése után nem rögtön az új helyen teremnek, hanem egyenes vonal mentén mozognak a cél felé. modell kiegészítés – mozgatott figura pozíciója – dropPiece nem rakja le a figurát, csak majd az animáció után view kiegészítés – ki kell rajzolni az aktuális pozícióba a mozgatottat controller kiegészítés – kell egy szál ami időnként szól a modellnek hogy mozdítsa odébb a figurát Ne barmoljunk bele az eddigiekbe AnimatedChessboard extends Chessboard AnimatedView extends ImageView AnimationController

3 Új class: AnimatedChessboard import java.awt.geom.Point2D; public class AnimatedChessboard extends Chessboard { // hány msec legyen egy lépés private static final int animationDuration = 2000; private Square moveFinishSquare = null; private boolean animating = false; private int animationTime; public boolean isAnimating(){return animating;} protected void finishMove(Square toSquare) { moveFinishSquare = toSquare; animating = true; animationTime = 0; }// folyt köv.

4 folyt: AnimatedChessboard public void finishAnimation() { animating = false; super.finishMove(moveFinishSquare); } public boolean step(int deltaTime) { animationTime += deltaTime; if (animationTime > animationDuration) { finishAnimation(); return false; } return true; }

5 vége: AnimatedChessboard public Piece getAnimatedPiece() { return grabbedPiece; } public Point2D.Float getAnimatedPiecePosition() { float t = (float) animationTime / animationDuration; return new Point2D.Float( moveStartSquare.file * (1 - t) + moveFinishSquare.file * t, moveStartSquare.rank * (1 - t) + moveFinishSquare.rank * t); }

6 Új class: AnimatedView import java.awt.*; import java.awt.geom.Point2D; import java.awt.image.BufferStrategy; public class AnimatedView extends ImageView { private AnimatedChessboard board; public AnimatedView(AnimatedChessboard board){ super(board); this.board = board; } public void paint(Graphics g) { super.paint(g); if (board.isAnimating()){ Point2D.Float animatedPiecePos = board.getAnimatedPiecePosition(); Piece grabbedPiece = board.getAnimatedPiece(); String key = grabbedPiece.toString(); Image im = pieceImages.get(key); g.drawImage(im, 19 + (int) (animatedPiecePos.x * 50), 19 + (int) ((7 - animatedPiecePos.y) * 50), null); }

7 Új class: AnimatedController import java.awt.*; import java.awt.event.*; public class AnimatedController { private AnimatedView view; private AnimatedChessboard board; public AnimatedController() { board = new AnimatedChessboard(); view = new AnimatedView(board); view.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getY() 20 && e.getX() 20) { Square s = new Square((e.getX() - 20) / 50, 7 - (e.getY() - 20) / 50); squareClicked(s); } }); }

8 Új: AnimatedController. createFrame public void createFrame() { Frame frame = new Frame("Chess"); frame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.add(view); frame.setSize(450, 600); frame.setResizable(false); frame.setVisible(true); }

9 új AnimatedController. squareClicked public void squareClicked(Square square) { if (!board.isPieceGrabbed()) { if (!board.grabPiece(square)) { view.printMessage("Invalid starting square!"); } else { view.printMessage("Click destination square!"); } } else { if (board.dropPiece(square)) { board.finishAnimation(); } else { view.printMessage("Invalid destination square, move cancelled!"); } view.display(); }

10 Chess.main public static void main(String[] args) { // TODO code application logic here System.out.println("!!!! Starting chess game !!!!"); //new ConsoleController(); //(new ButtonController()).createFrame(); //(new ImageController()).createFrame(); (new AnimatedController()).createFrame(); }

11 Próba ugyanúgy működik, mint az ImageView-os nincs animáció egyelőre – nem hívjuk az AnimatedChessboard.step-et – rendszeresen, mondjuk 40 msec-enként kéne hívni – erre létrehozunk egy szálat (Thread)

12 Belső osztály az AnimatedController -ben class AnimationThread extends Thread{ public void run(){ try { while (board.step(40)) { //míg nincs vége az animációnak view.display(); sleep(40); } } catch (InterruptedException ie) { } finally { view.display(); if (board.getWhiteOnTurn()) { view.printMessage("White to move!"); } else { view.printMessage("Black to move!"); } // meg egy ilyen típusú mező is kell AnimationThread animationThread;

13 AnimatedController. squareClicked public void squareClicked(Square square) { if (board.isAnimating()) { board.finishAnimation(); animationThread.interrupt(); } if (!board.isPieceGrabbed()) { if (!board.grabPiece(square)) { view.printMessage("Invalid starting square!"); } else { view.printMessage("Click destination square!"); } } else { if (board.dropPiece(square)) { board.finishAnimation(); animationThread = new AnimationThread(); animationThread.start(); } else { view.printMessage("Invalid destination square, move cancelled!"); } view.display(); }

14 Próba animál de villog dupla buffer kell

15 AnimatedController. createFrame végére view.createBufferStrategy(2);

16 Új metódus: AnimatedView. update public void update(Graphics g) { BufferStrategy bf = getBufferStrategy(); Graphics dg = bf.getDrawGraphics(); dg.clearRect(0, 0, getWidth(), getHeight()); paint(dg); dg.dispose(); bf.show(); }

17 Próba nem villog

18 Önálló feladat legyen simább a mozgás – getAnimatedPiecePosition – továbbra is a két végpont lineárkombinációja az aktuális pozíció – de nem lineárisan interpolálunk, hanem harmadfokú spline-nal – a súlyok: 1-(3t 2 – 2t 3 ) és 3t 2 – 2t 3 legyen a mozgás sokkal gyorsabb – 800 vagy 400 vagy 200 msec az animáció hossza 4 perc

19 V. labor vége – szorgalmi feladat amikor megfogtunk egy figurát, de még nem raktuk le, akkor az egeret kövesse – grabPiece végén vegyük le a tábláról – egérmozgás eseményt figyeljük és jegyezzük meg az egérpozíciót – ha épp lépésben vagyunk (van megfogott figura, de még nem animálunk), rajzoláskor rajzoljuk a fogott figurát az egérhez drag&drop lépéssel is lehet próbálkozni


Letölteni ppt "V. labor Thread, animáció. Animáció A figurák a lépés kijelölése után nem rögtön az új helyen teremnek, hanem egyenes vonal mentén mozognak a cél felé."

Hasonló előadás


Google Hirdetések