⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 draggingarea.java

📁 24分扑克牌游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)DraggingArea.java Version 1.0 98/03/12
 * 
 * Copyright (c) 1998 by Huahai Yang
 * 
 * Use at your own risk. I do not guarantee the fitness of this 
 * software for any purpose, and I do not accept responsibility for 
 * any damage you do to yourself or others by using this software.
 * This file may be distributed freely, provided its contents 
 * are not tampered with in any way.
 *
 */

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Math;

/**
 * Main playing area. Cards and operators are put on it and can be 
 * dragged around.  Card deck is also put on it and caculate solutions
 * in a seperate thread.  
 */
public class DraggingArea extends Panel 
                          implements Runnable
{
   Thread dragThread;
   
	Applet applet;
	
	Dimension offScreenDimension;
	Image offScreenImage;
	Graphics offScreenGraphics;
	
	Image[] cardImages,
	        operatorImages;
	Image cardDeckImage;     
	
	MediaTracker tracker;
   int imageCount;
   
   CardDeck cardDeck;
   PlayingStatus status;
   
   Card [] currentCards;
   Operator [] operators;
   
   DraggingSlot [] draggingSlots;
      
   Vector dragImages;
   DraggingImage theMoving;
   Point grabPoint;
   
   Cursor defaultCursor,
          handCursor;
   
   int updateLeft, updateTop, updateRight, updateBottom;
   boolean animating;
   
   public DraggingArea()
	{
      setLayout(null);
      setForeground(new Color(0));
   	setBackground(new Color(32768));
   	
   	handCursor = new Cursor(Cursor.HAND_CURSOR);
   	defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);

   	addMouseListener(new MyMouseAdapter());
   	addMouseMotionListener(new MyMouseMotionAdapter());
	
	} // constructor
   
	public void start()
	{
	   applet = (Applet)getParent();
	   
	   loadAllImages();
   	
	   status = new PlayingStatus();
   	status.addObserver((Arithmetic24)applet);
	   status.set(PlayingStatus.WAITING);
	   
	   animating = false;
   	dragImages = new Vector();
   	theMoving = null;
   	grabPoint = new Point();
   	
      // add cardDeck and run a seperate thread to calculate all of
      // this deck's 13 deal's solutions
      cardDeck = new CardDeck(30, 10, cardDeckImage, this);
   	dragImages.addElement(cardDeck);
	   
      // create offscreen context
      Dimension d = size();
      if ( (offScreenGraphics == null)
            || (d.width != offScreenDimension.width)
            || (d.height != offScreenDimension.height) ) 
      {
         offScreenDimension = d;
         offScreenImage = createImage(d.width, d.height);
         offScreenGraphics = offScreenImage.getGraphics();
      } // if
      resetClip();
      
   	// add slots
      addSlots();
      
   	currentCards = new Card [4];
   	operators = new Operator [18];
      
      // add operators, each operator has 3 copies
      // arrange operaters this way:
      //    + * (
      //    - / )
      int count = 0;
      for(int i = 0; i < 3; i++)
      {
         for(int j = 0; j < 6; j++)
         {
            dragImages.addElement( operators[count++] = new Operator( 
                  j, 510 + j / 2 * 30, 15 + j % 2 * 45,
                  operatorImages[j], this ) );
         } // for j         
      } // for i
      
      if (dragThread == null) 
      {
         dragThread = new Thread(this, "Dragging");
         dragThread.start();
      } // if
      
   } // start  
   
   /**
    * put operaters back to original positions
    */
   public void arrangeOperators()
   {
      for(int i = 0; i < 13; i++)
      {
         if(draggingSlots[i] instanceof OperatorSlot)
         {
            draggingSlots[i].empty();
         } // if
      } // for   
      
      for(int i = 0; i < 18; i++)
      {
         operators[i]. setLocation(510 + i % 6 / 2 * 30, 
                  15 + i % 6 % 2 * 45);
      } // for 
   } //arrangeOperators
   
   // solts arranged like this: 
   // o c o o c o o o c o o c o
   // "o" is operator slot, "c" is card slot
   protected void addSlots()
   {
      int widthPointer;
      DraggingSlot slotPointer = null;
      
      draggingSlots = new DraggingSlot [13];
      
      slotPointer = draggingSlots[0] = new OperatorSlot(10, 160);
      widthPointer = slotPointer.getLocation().x + OperatorSlot.WIDTH;
      
      slotPointer = draggingSlots[1] = new CardSlot(widthPointer + 6, 130);
      widthPointer = slotPointer.getLocation().x + CardSlot.WIDTH;
      
      slotPointer = draggingSlots[2] = new OperatorSlot(widthPointer + 6, 160);
      widthPointer = slotPointer.getLocation().x + OperatorSlot.WIDTH;
      
      slotPointer = draggingSlots[3] = new OperatorSlot(widthPointer + 6, 160);
      widthPointer = slotPointer.getLocation().x + OperatorSlot.WIDTH;
      
      slotPointer = draggingSlots[4] = new CardSlot(widthPointer + 6, 130);
      widthPointer = slotPointer.getLocation().x + CardSlot.WIDTH;
      
      slotPointer = draggingSlots[5] = new OperatorSlot(widthPointer + 6, 160);
      widthPointer = slotPointer.getLocation().x + OperatorSlot.WIDTH;
      
      slotPointer = draggingSlots[6] = new OperatorSlot(widthPointer + 6, 160);
      widthPointer = slotPointer.getLocation().x + OperatorSlot.WIDTH;
      
      slotPointer = draggingSlots[7] = new OperatorSlot(widthPointer + 6, 160);
      widthPointer = slotPointer.getLocation().x + OperatorSlot.WIDTH;
      
      slotPointer = draggingSlots[8] = new CardSlot(widthPointer + 6, 130);
      widthPointer = slotPointer.getLocation().x + CardSlot.WIDTH;
      
      slotPointer = draggingSlots[9] = new OperatorSlot(widthPointer + 6, 160);
      widthPointer = slotPointer.getLocation().x + OperatorSlot.WIDTH;
      
      slotPointer = draggingSlots[10] = new OperatorSlot(widthPointer + 6, 160);
      widthPointer = slotPointer.getLocation().x + OperatorSlot.WIDTH;
      
      slotPointer = draggingSlots[11] = new CardSlot(widthPointer + 6, 130);
      widthPointer = slotPointer.getLocation().x + CardSlot.WIDTH;
      
      slotPointer = draggingSlots[12] = new OperatorSlot(widthPointer + 6, 160);
      widthPointer = slotPointer.getLocation().x + OperatorSlot.WIDTH;
      
   } // addSlots   
      
   /**
    * stop the running of dragging area
    */
   public void stop() 
   {
      animating = false;
      cardDeck.stop();
      dragImages.removeAllElements();
      
      if (dragThread != null) 
      {
         dragThread.stop();
         dragThread = null;
      } //if
   } // stop
   

   public void run()
   {
      Thread myThread = Thread.currentThread();
      while (dragThread == myThread) 
      {
         if( animating )
         {
            presentSolution();
         } // if
         else
         {
            try
            {
               clipRepaint();
               Thread.sleep(200L);
            } // try
            catch(InterruptedException e)
            {
            } // catch
         } // else  
      } // while
   } // run

   public Dimension getPreferredSize() 
   {
      return getMinimumSize();
   } // getPreferredSize

   public Dimension getMinimumSize() 
   {
      return new Dimension(640, 250);
   } // getMinimumSize
   
   public Vector userCreatedExpression()
   {
      Vector expression = new Vector();
      DraggingImage image = null;
      
      for(int i = 0; i < 13; i ++)
      {
         image = draggingSlots[i].getHoldenImage();
         if(image != null)
         {
            if(image instanceof Card)
               expression.addElement(((Card)image).getValue());
            else expression.addElement(((Operator)image).getValue());
         } // if   
         else 
         {
            expression.addElement(new Character(' '));
         } // if   
      } // for   
      return expression;
   } // userCreatedExpression   
   
   
   // full expression should has at least 4 cards, 3 operators
   public boolean isFullExpression()
   {
      int cardCount = 0,
          operatorCount = 0;
      DraggingImage image = null;
      
      for(int i = 0; i < 13; i ++)
      {
         image = draggingSlots[i].getHoldenImage();
         if(image != null)
         {
            if(image instanceof Card) cardCount++;
            else operatorCount++;
         } // if   
      } // for
      return ( cardCount > 3 && operatorCount > 2 );
   } // isFullExpression   
         
   public Vector currentSolution()
   {
      return ( cardDeck.currentSolution() );
   } // currentSolution  
   
   public void beginAnimation()
   {
      animating = true;
   } //beginAnimation
   
   public void endAnimation()
   {
      animating = false;
   } //endAnimation
   
   public void setStatus(int value)
   {
      status.set(value);
   } //setStatus
   
   /**
    * presents correct solution in animation
    */
   public void presentSolution()
   {
      int speed = 20,   //move 20 pixels each time
          startX, 
          startY,
          endX,   
          endY,
          steps, 
          stepX, 
          stepY;
      double distance;
      Point location;
      Dimension size;
      
      DraggingSlot slot = null;
      Vector solution = null;
      solution = currentSolution();
      Object element = null;
      
      resetClip();
      clearSlots();
      clipRepaint();
      try
      {
         Thread.sleep(200L);
      } // try
      catch(InterruptedException e)
      {
      } // catch  
      
      for(int i = 0; i < 13; i++)
      {
         slot = draggingSlots[i];
         element = solution.elementAt(i);
         if( element instanceof Integer )
         {
            for(int j = 0; j < 4; j++)
            {
               if( !(currentCards[j].isSettled()) && 
                     currentCards[j].getCardValue() == ((Integer)element).intValue() )
               {
                  theMoving = currentCards[j];
                  break;
               } // if 
            } // for j
         } // if is card
         else
         {
            boolean isOperator = false;
            for(int j = 0; j < 18; j++)
            {
               if( !(operators[j].isSettled()) &&
                  operators[j].getOpSymbol() == ((Character)element).charValue() )
               {
                  theMoving = operators[j];
                  isOperator = true;
                  break;
               } //if
            } // for j
            // is white space, skip
            if(!isOperator) continue;
         } // else is character   
         
         //put theMoving on top
         dragImages.removeElement(theMoving);
         dragImages.addElement(theMoving);
         
         location = theMoving.getLocation();
         size = theMoving.getSize();
         
         //initiate clipping area 
         updateLeft = location.x;
         updateTop = location.y;
         updateRight = updateLeft + size.width;
         updateBottom = updateTop + size.height;
         
         //start point of animation
         startX = location.x;
         startY = location.y;
         
         //end point of animation
         location = slot.getLocation();
         endX = location.x;
         endY = location.y;
         

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -