📄 draggingarea.java
字号:
distance = Math.sqrt( (endX - startX) * (endX - startX)
+ (endY - startY) * (endY - startY) );
//steps needed moving from start point to the end point
steps = (int)(distance / speed);
if(steps != 0)
{
stepX = (endX - startX) / steps;
stepY = (endY - startY) / steps;
} // if
else
{
stepX = 0;
stepY = 0;
} //else
//animation loop
for(int s = 0; s < steps; s++)
{
theMoving.setLocation(startX + stepX * s,
startY + stepY * s);
clipRepaint();
try
{
Thread.sleep(100L);
} // try
catch(InterruptedException e)
{
} // catch
} // for
slot.fillWith(theMoving);
resetClip();
clipRepaint();
((Arithmetic24)applet).soundList.playClip(
((Arithmetic24)applet).fillSlotSound );
try
{
Thread.sleep(200L);
} // try
catch(InterruptedException e)
{
} // catch
} // for
theMoving = null;
animating = false;
resetClip();
} //presentSolution
public void clearSlots()
{
for(int i = 0; i < 13; i++)
{
draggingSlots[i].empty();
} //for
for(int i = 0; i < 18; i++)
{
operators[i].setLocation(510 + i % 6 / 2 * 30, 15 + i % 6 % 2 * 45);
} // for
} //clearSlots
public void removeCards()
{
// empty card slots
for(int i = 0; i < 13; i++)
{
if(draggingSlots[i] instanceof CardSlot)
{
draggingSlots[i].empty();
} // if
} // for
for(int i = 0; i < 4; i++)
{
if(currentCards[i] != null)
{
dragImages.removeElement(currentCards[i]);
} //if
} // for
clipRepaint();
} // removeCards
public void addCards()
{
for(int j = 0; j < 4; j++)
{
currentCards[j].setLocation( 150 + j * 80, 10 );
dragImages.addElement( currentCards[j] );
} // for
} // addCards
/*
* track all card and operator images loading
*/
public void loadAllImages()
{
tracker = new MediaTracker(this);
cardImages = new Image[52];
operatorImages = new Image[6];
imageCount = 0;
// track card images
for(int i = 0; i < 4; i++)
{
for(int j = 1; j <= 13; j++)
{
cardImages[imageCount] = applet.getImage( applet.getCodeBase(),
"image/" + Card.SUIT_NAMES[i] + "-" + j + ".GIF" );
tracker.addImage(cardImages[imageCount], imageCount);
imageCount++;
} // for j
} // for i
// track operator images
for(int i = 0; i < 6; i++)
{
operatorImages[i] = applet.getImage( applet.getCodeBase(),
"image/" + Operator.OP_NAMES[i] + ".GIF" );
tracker.addImage(operatorImages[i], imageCount);
imageCount++;
} // for i
// track cardDeck image
cardDeckImage = applet.getImage( applet.getCodeBase(),
"image/CardSet.GIF" );
tracker.addImage(cardDeckImage, imageCount);
imageCount++;
// begin loading
tracker.checkAll(true);
} // loadAllImages
public synchronized void resetClip()
{
updateLeft = updateTop = 0;
updateRight = getSize().width;
updateBottom = getSize().height;
} // resetClip
public void paint(Graphics g)
{
g.setClip(updateLeft, updateTop,
updateRight - updateLeft, updateBottom - updateTop);
//Paint the image onto the screen.
g.drawImage(offScreenImage, 0, 0, this);
//set update area again.
if(theMoving != null)
{
Point location = theMoving.getLocation();
Dimension size = theMoving.getSize();
updateLeft = location.x;
updateTop = location.y;
updateRight = updateLeft + size.width;
updateBottom = updateTop + size.height;
} // if
} // paint
/**
* use double-buffering and clipping to update dragging area
*/
public synchronized void update(Graphics g)
{
// if not all image loaded, show status bar
if(!tracker.checkAll(true))
{
int loadedCount = 0;
for(int i = 0; i < imageCount; i++)
{
if(tracker.statusID(i, true) == 8)
loadedCount++;
//System.out.println(loadedCount);
}
int barWidth = size().width - 100;
int fillWidth = (barWidth * loadedCount) / imageCount;
offScreenGraphics.setColor(Color.yellow);
offScreenGraphics.drawString("Loading images:",
50, size().height - 50);
offScreenGraphics.drawRect(50, size().height - 30, barWidth, 15);
offScreenGraphics.fillRect(50, size().height - 30, fillWidth, 15);
paint(g);
return;
} //if
// clip, only draw changed area
offScreenGraphics.setClip(updateLeft, updateTop, updateRight -
updateLeft, updateBottom - updateTop);
// Erase the previous image.
offScreenGraphics.setColor(getBackground());
offScreenGraphics.fillRect(0, 0, size().width, size().height);
// draw all things
drawDragSlots(offScreenGraphics);
drawDragImages(offScreenGraphics);
paint(g);
} // update
/**
* Updates the clip area, then request repaint
*/
public synchronized void clipRepaint()
{
if(theMoving != null)
{
Point location = theMoving.getLocation();
Dimension size = theMoving.getSize();
// enlarge update area to include newly changed area
updateLeft = Math.min(updateLeft, location.x);
updateTop = Math.min(updateTop, location.y);
updateRight = Math.max(updateRight, location.x + size.width);
updateBottom = Math.max(updateBottom, location.y + size.height);
} // if
repaint();
} //clipRepaint
// draw all the slots
protected void drawDragSlots(Graphics g)
{
for( int i = 0; i < 13; i++ )
{
draggingSlots[i].paint(g);
} // for
} // drawDragSlots
// draw all of the dragging images
protected void drawDragImages(Graphics g)
{
for(Enumeration e = dragImages.elements(); e.hasMoreElements(); )
{
((DraggingImage)e.nextElement()).paint(g);
} // for
} // drawDragImages
// inner class to handle mouse events
class MyMouseAdapter extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
if( animating ) return;
for( int i = dragImages.size() - 1; i >= 0; i-- )
{
DraggingImage d = (DraggingImage)dragImages.elementAt(i);
if( d.contains(event.getX(), event.getY()) )
{
if( d.isDraggable() )
{
theMoving = d;
synchronized(dragImages)
{
//put the dragged on top
dragImages.removeElement(theMoving);
dragImages.addElement(theMoving);
} //synchronized
grabPoint.x = event.getX() - d.getLocation().x;
grabPoint.y = event.getY() - d.getLocation().y;
updateLeft = d.getLocation().x;
updateTop = d.getLocation().y;
updateRight = updateLeft + d.getSize().width;
updateBottom = updateTop + d.getSize().height;
} // if
else
{
// clicked on deck
if( d == cardDeck && ((CardDeck)d).isClickable() )
{
((Arithmetic24)applet).soundList.playClip(
((Arithmetic24)applet).clickDeckSound );
removeCards();
arrangeOperators();
currentCards = ((CardDeck)d).deal();
if( currentCards == null )
{
// finished a round
// TO DO: record user name if his score is
// high, some networking staff needed
dragImages.removeElement( cardDeck );
status.set(PlayingStatus.ROUND_OVER);
} //if
else
{
addCards();
((CardDeck)d).disableClick();
status.set(PlayingStatus.DEALED);
} // else
clipRepaint();
} // if*/
} // else
return;
} // if
} // for
} // mousePressed
public void mouseReleased(MouseEvent event)
{
if( animating ) return;
if( theMoving == null ) return;
boolean isOnSlot = false;
for(int i = 0; i < 13; i++)
{
DraggingSlot slot = draggingSlots[i];
if( slot.underneath(theMoving) )
{
int slotType, imageType;
if(slot instanceof CardSlot)
slotType = ((CardSlot)slot).getType();
else slotType = ((OperatorSlot)slot).getType();
if(theMoving instanceof Card)
imageType = ((Card)theMoving).getType();
else imageType = ((Operator)theMoving).getType();
if( slotType != imageType ) continue;
if( slot.isEmpty() )
{
if(theMoving.isSettled())
{
//remove from original slot
theMoving.getSlot().empty();
} // if
slot.fillWith(theMoving);
((Arithmetic24)applet).soundList.playClip(
((Arithmetic24)applet).fillSlotSound );
} // if slot empty
else
{
if(theMoving != slot.getHoldenImage())
{
slot.kickOff(theMoving);
if(theMoving.isSettled())
{
theMoving.getSlot().empty();
} // if
} // if
else slot.fillWith(theMoving);
} // else
isOnSlot = true;
break;
} // if underneath
} // for
if( !isOnSlot )
{
if( theMoving.isSettled() )
{
//remove from the original slot
theMoving.getSlot().empty();
} // if
} // if
resetClip();
clipRepaint();
theMoving = null;
} // mouseReleased
} // MyMouseAdapter
class MyMouseMotionAdapter extends MouseMotionAdapter
{
public synchronized void mouseDragged(MouseEvent event)
{
if( animating ) return;
if(theMoving != null)
{
theMoving.setLocation( event.getX() - grabPoint.x,
event.getY() - grabPoint.y );
clipRepaint();
} // if
} // mouseDrgged
public void mouseMoved(MouseEvent event)
{
if( animating ) return;
for( Enumeration e = dragImages.elements(); e.hasMoreElements(); )
{
DraggingImage d = (DraggingImage)e.nextElement();
if( d.contains(event.getX(), event.getY()) )
{
setCursor(handCursor);
return;
} // if
} // for
if(getCursor() != defaultCursor) setCursor(defaultCursor);
} // mouseMoved
} // MyMouseMotionAdapter
} // DraggingArea
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -