˹á
 úҹ
  C/C++ new
  Java new
 Sourcecode.TV
  Android
  Blackberry
  C/C++
  Java
  Linux
  Windows Phone
  ͧͧ
 
  .NET
  Android
  Blackberry
  C++Builder
  Delphi
  Java
  Mobile
  Open Source
  Pocket PC
  Visual Basic
  Web
  Windows Phone
  Others
 Ẻ¹
  ASP
  ASP.NET Webmatrix
  C++
  eVB
  J2ME
  Java
  PHP
 
  Android
  ASP
  C++
  VC++
  CGI-Perl
  Delphi
  Java
  jQuery
  PHP
  Visual Basic
  .NET
 ԡèҡ
  Դ...
  DoodeeHosting
  Ѻ͡Ẻ Ѳ䫵
  Freeware
  ѵҤɳ
  ª١
  ʶԵԼ
 Ŵ
 Ң
 дҹʹ
  .NET
  C/C++
  Delphi
  Java
  Palm/PocketPC
  Visual Basic
  Web
 
 дҹҹ
 ԧ
 ǡѺ
 ѵҤɳ
Social Network

Facebook  Twitter  YouTube
 New Article
 Webboard
 Freelanceboard
Freeware
 (BerDee)
(Android)
駩غ
駩غ(Android)
WebcamMonitor
WebcamMonitor(Windows)
scSendMail
scSendMail(Windows)
MSN Caption
MSN Caption(Windows)
  Freelance comment
  ԴͺʹФѺ...
2015-07-29 13:35:58
  Ҽͧ纷ѲҴ HTML5 ...
2015-01-18 15:33:54
  Ҽͧ纷ѲҴ HTML5 ...
2015-01-18 15:32:51
  ԴͺФѺ
ҹ...

2014-01-06 12:47:38
  ѧҤ͹ ͧ֡ͧ...
2013-07-06 01:04:37
  ʹ觢ͺࢵҤҤҡ͹ФѺ...
2013-03-24 18:54:20
  ѧդѺҹԴѺ 08...
2012-12-16 19:18:14
  ʹ㨤 Դ 0905076277...
2012-11-12 11:07:46
  Ѻ礹ѡ֡ͺѷ
...

2012-10-29 03:10:46
  sukit_te34@hotmail.com...
2012-10-29 03:09:36
  Webboard comment
  ͧ gotoxy ѧ...
2017-08-28 11:45:52
  ͧҧա u...
2017-08-27 20:09:01
  #define GLUT_DISABLE_ATEXIT...
2017-08-27 12:53:17
  դԻ͹ ...
2017-08-27 10:50:02
  Ѻ ͹ matlab ¹ matlab...
2017-06-03 16:27:23
  ੾зҧҴ ͧҢ...
2017-05-25 16:18:08
  ͧ˹Ѻ os ...
2017-05-25 16:12:47
  ¹ php curl ...
2017-04-20 01:04:20
  ¹ ...
2017-03-27 12:50:54
  ʧҷ ͧѾഷ...
2017-03-27 12:37:20
  Homework comment
  ͺسҡѺ...
2017-08-16 18:27:25
  ͧ ͧ ѧ ^^...
2017-04-18 18:40:46
  ͧ ͧ ѧ ^^...
2017-04-18 18:37:54
  á flowchart ѹ...
2017-04-18 17:43:33
  ҡ flowchart ...
2017-04-18 16:25:29
  kungking ӶäѺ ?...
2017-03-18 23:28:36
  ¹ʴͧ...
2017-03-06 10:56:25
  鴷ӧҹẺ ...
2016-09-06 01:00:12
  ҡҺ鴹Ѻѧ...
2016-09-05 09:31:27
  ͧ֡¹...
2016-04-22 00:47:49
  Article comment
  [b]5 ...
2017-10-18 08:19:17
  - ...
2017-10-18 07:17:11
  [b] ...
2017-10-18 06:07:31
  [b] 96 ...
2017-10-18 06:05:28
  - ...
2017-10-18 05:49:56
  code ͧ VB ú...
2017-09-28 16:50:02
  ҨѺ ͧ
...

2017-07-11 09:59:35
  ö͡Ѻ anycast ...
2017-07-05 10:12:35
  Ѻ س "Դ" ͧ֡...
2016-06-18 15:21:09
  vs2008 c# Ѻ ͺسѺ...
2016-06-17 14:39:30
  9Mcode comment
  ҡ...
2014-02-21 08:52:19

Link Exchange

ѵҤɳ
Statistics of Sourcecode in Thailand
 
 Webboard

- - - - - - - - - - - - - - ʹѺʹع- - - - - - - - - - - - - -

з #3309 [Java]

úǹ͸Ժ ͡ slapshot դ

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

public class Slapshot extends Frame implements ActionListener, MouseListener, MouseMotionListener, Runnable
{
Menu menu1;
MenuBar menubar;
MenuItem menuitem0, menuitem1, menuitem2, menuitem3;
Image memoryImage;
Image backGroundImage;
Image[] gifImages = new Image[2];
Graphics memoryGraphics;
Thread thread;
MediaTracker tracker;
Vector<Puck> pucks = new Vector<Puck>();
int yourScore = 0;
int theirScore = 0;
int offsetX = 0;
int offsetY = 0;
int speed = 50;
int maxVelocity = 10;
Label label1, label2;
int retVal = 0;
boolean dragging = false;
boolean stop = true;
boolean runOK = true;
OkCancelDialog textDialog;

public static void main(String[] args)
{
new Slapshot();
}

Slapshot()
{
menubar = new MenuBar();

menu1 = new Menu("File");

menuitem0 = new MenuItem("Start");
menu1.add(menuitem0);
menuitem0.addActionListener(this);

menuitem1 = new MenuItem("End");
menu1.add(menuitem1);
menuitem1.addActionListener(this);

menuitem2 = new MenuItem("Set speed...");
menu1.add(menuitem2);
menuitem2.addActionListener(this);

menuitem3 = new MenuItem("Exit");
menu1.add(menuitem3);
menuitem3.addActionListener(this);

menubar.add(menu1);
setMenuBar(menubar);

addMouseListener(this);
addMouseMotionListener(this);

textDialog = new OkCancelDialog(this,
"Set speed (1-100)", true);

setLayout(null);

label1 = new Label();
label1.setText("0");
label1.setBounds(180, 310, 20, 20);
label1.setVisible(false);
add(label1);

label2 = new Label();
label2.setText("0");
label2.setBounds(400, 310, 20, 20);
label2.setVisible(false);
add(label2);

tracker = new MediaTracker(this);
backGroundImage = Toolkit.getDefaultToolkit().
getImage("rink.gif");
tracker.addImage(backGroundImage, 0);

gifImages[0] = Toolkit.getDefaultToolkit().
getImage("puck.gif");
tracker.addImage(gifImages[0], 0);

gifImages[1] = Toolkit.getDefaultToolkit().
getImage("blocker.gif");
tracker.addImage(gifImages[1], 0);

try {
tracker.waitForID(0);
}catch (InterruptedException e) {
System.out.println(e);
}

setTitle("Slapshot!");

setResizable(false);

setSize(backGroundImage.getWidth(this),
backGroundImage.getHeight(this));

setVisible(true);

memoryImage = createImage(getSize().width, getSize
().height);
memoryGraphics = memoryImage.getGraphics();

thread = new Thread(this);
thread.start();

this.addWindowListener(new WindowAdapter(){
public void windowClosing(
WindowEvent e){
runOK = false;
System.exit(0);
}
}
);
}

public void init()
{
Point position, velocity;
pucks = new Vector<Puck>();

Rectangle edges = new Rectangle(10 + getInsets().left,
getInsets().top, getSize().width - (getInsets().left
+ getInsets().right), getSize().height - (getInsets().top
+ getInsets().bottom));

for (int loopIndex = 0; loopIndex < 12; loopIndex++){

pucks.add(new Puck(gifImages[0],
0, maxVelocity, edges, this));

try {
Thread.sleep(20);
}
catch (Exception exp) {
System.out.println(exp.getMessage());
}
}

pucks.add(new Puck(gifImages[1], 1, maxVelocity, edges,
this));

pucks.add(new Puck(gifImages[1], 2, maxVelocity, edges,
this));
}

public void run()
{
Puck puck;

while (runOK) {
if(!stop){

int numberLeft;
for (int loopIndex = 0; loopIndex < 12; loopIndex++){
puck = (Puck)pucks.elementAt(loopIndex);

if(puck.gone()){
continue;
}

retVal = puck.slide(pucks.elementAt
(13).rectangle, pucks.elementAt
(12).rectangle);

numberLeft = 0;
for (int loopIndex2 = 0; loopIndex2 < 12;
loopIndex2++){
if(!((Puck)pucks.elementAt(loopIndex2))
.gone()){
numberLeft++;
}
}

if(retVal < 0){
if(yourScore + theirScore + numberLeft == 11){
label1.setText(String.valueOf
(++yourScore));
}
}

if(retVal > 0){
if(yourScore + theirScore + numberLeft == 11){
label2.setText(String.valueOf
(++theirScore));
}
}

int struckPuck = -1;

for (int loopIndex3 = 0; loopIndex3 < 13;
loopIndex3++){
Puck testPuck = (Puck)pucks.elementAt
(loopIndex3);

if (puck == testPuck || testPuck.gone()){
continue;
}

if(puck.rectangle.intersects
(testPuck.rectangle)){
struckPuck = loopIndex3;
}
}

if (struckPuck >= 0){
Puck puck1 = (Puck)pucks.elementAt(struckPuck);
Puck puck2 = (Puck)pucks.elementAt(loopIndex);

if(puck2.immovable()){
puck1.velocity.x = -puck1.velocity.x;

retVal = puck1.slide(pucks.elementAt
(13).rectangle, pucks.elementAt
(12).rectangle);

numberLeft = 0;
for (int loopIndex4 = 0; loopIndex4 < 12;
loopIndex4++){
if(!((Puck)pucks.elementAt
(loopIndex4)).gone()){
numberLeft++;
}
}

if(retVal < 0){
if(yourScore + theirScore + numberLeft
== 11){
label1.setText(String.valueOf
(++yourScore));
}
}

if(retVal > 0){
if(yourScore + theirScore + numberLeft
== 11){
label2.setText(String.valueOf
(++theirScore));
}
}

} else if(puck1.immovable()){
puck2.velocity.x = -puck2.velocity.x;

retVal = puck2.slide(pucks.elementAt(13).
rectangle, pucks.elementAt
(12).rectangle);

numberLeft = 0;
for (int loopIndex5 = 0; loopIndex5 < 12;
loopIndex5++){
if(!((Puck)pucks.elementAt
(loopIndex5)).gone()){
numberLeft++;
}
}

if(retVal < 0){
if(yourScore + theirScore + numberLeft
== 11){
label1.setText(String.valueOf
(++yourScore));
}
}

if(retVal > 0){
if(yourScore + theirScore + numberLeft
== 11){
label2.setText(String.valueOf
(++theirScore));
}
}
}
else {
retVal = puck1.slide(pucks.elementAt
(13).rectangle, pucks.elementAt
(12).rectangle);

numberLeft = 0;

for (int loopIndex6 = 0; loopIndex6 < 12;
loopIndex6++){
if(!((Puck)pucks.elementAt(loopIndex6))
.gone()){
numberLeft++;
}
}

if(retVal < 0){
if(yourScore + theirScore + numberLeft
== 11){
label1.setText(String.valueOf
(++yourScore));
}
}

if(retVal > 0){
if(yourScore + theirScore + numberLeft
== 11){
label2.setText(String.valueOf
(++theirScore));
}
}

retVal = puck2.slide(pucks.elementAt
(13).rectangle, pucks.elementAt
(12).rectangle);

numberLeft = 0;
for (int loopIndex7 = 0; loopIndex7 < 12;
loopIndex7++){
if(!((Puck)pucks.elementAt
(loopIndex7)).gone()){
numberLeft++;
}
}

if(retVal < 0){
if(yourScore + theirScore + numberLeft
== 11){
label1.setText(String.valueOf
(++yourScore));
}
}

if(retVal > 0){
if(yourScore + theirScore + numberLeft
== 11){
label2.setText(String.valueOf
(++theirScore));
}
}
}
}

int lowestTime = 10000;
int impactY = -1;

for (int loopIndex3 = 0; loopIndex3 < 12;
loopIndex3++){
Puck movingPuck = (Puck)pucks.elementAt
(loopIndex3);
Rectangle r = movingPuck.rectangle;
Point mPosition = new Point(r.x, r.y);
Point mVelocity = movingPuck.velocity;

if(mVelocity.x > 0 && !movingPuck.gone()){
int yHit = (mVelocity.y / mVelocity.x) *
(backGroundImage.getWidth(this) -
mPosition.x) + mPosition.y;

if(yHit > 115 && yHit < 223){
int time = (backGroundImage.getWidth
(this) - mPosition.x)
/ mVelocity.x;
if(time <= lowestTime){
impactY = yHit;
}
}
}

if(impactY > 0){
Puck block = pucks.elementAt(12);
int blockPosition = block.rectangle.y;

if(blockPosition < impactY){
block.slide(Math.min(blockPosition +
40, impactY));
} else {
block.slide(Math.max(blockPosition -
40, impactY));
}
repaint();
}
label2.setText(String.valueOf(theirScore));
}
}
repaint();
try {
Thread.sleep(speed);
}
catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}

public void update(Graphics g)
{
memoryGraphics.drawImage(backGroundImage, 0, 0, this);

for (int loopIndex = 0; loopIndex < pucks.size(); loopIndex++){
if(!stop){
((Puck)pucks.elementAt(loopIndex)).drawPuckImage
(memoryGraphics);
}
}

g.drawImage(memoryImage, 0, 0, this);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == menuitem0){
if(!stop){
stop = true;
repaint();
}
init();
label1.setVisible(true);
label2.setVisible(true);
stop = false;
label1.setText("0");
label2.setText("0");
yourScore = 0;
theirScore = 0;
}

if(e.getSource() == menuitem1){
stop = true;
label1.setText("0");
label2.setText("0");
yourScore = 0;
theirScore = 0;
repaint();
}

if(e.getSource() == menuitem2){
textDialog.setVisible(true);
if(!textDialog.data.equals("")){
int newSpeed = Integer.parseInt(textDialog.data);
newSpeed = 101 - newSpeed;
if(newSpeed >= 1 && newSpeed <= 100){
speed = newSpeed;
}
}
}

if(e.getSource() == menuitem3){
runOK = false;
System.exit(0);
}
}

public void mousePressed(MouseEvent e)
{
Rectangle r1 = pucks.elementAt(13).rectangle;
if(r1.contains(new Point(e.getX(), e.getY()))){
offsetX = e.getX() - r1.x;
offsetY = e.getY() - r1.y;
dragging = true;
}
}

public void mouseReleased(MouseEvent e)
{
dragging = false;
}

public void mouseDragged(MouseEvent e)
{
if(dragging){
int newY = e.getY() - offsetY;

pucks.elementAt(13).slide(newY);
repaint();
}
}

public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
}

class Puck
{
Image image1;
Component rink;
Rectangle rectangle;
Point velocity;
Point location;
Rectangle edges;
Random random;
boolean outOfAction = false;
boolean doNotMove = false;
int maxVelocity;

public Puck(Image image1, int type, int maxVelocity,
Rectangle edges, Component rink)
{
this.rink = rink;
this.image1 = image1;
if (type > 0){
doNotMove = true;
}
this.maxVelocity = maxVelocity;
this.edges = edges;

random = new Random(System.currentTimeMillis());

if(type == 0){
location = new Point(100 + (Math.abs(random.nextInt())
% 300), 100 + (Math.abs(100 + random.nextInt()) %
100));

this.velocity = new Point(random.nextInt() % maxVelocity,
random.nextInt() % maxVelocity);

while(velocity.x == 0){
velocity.x = random.nextInt(maxVelocity / 2)
- maxVelocity / 2;
}
}

if(type == 1){
location = new Point(((Slapshot)rink).backGroundImage
.getWidth(rink) - 18,
((Slapshot)rink).backGroundImage.getHeight
(rink)/2);
this.velocity = new Point(0, 0);
}

if(type == 2){
location = new Point(10,
((Slapshot)rink).backGroundImage.getHeight(rink)/2);
this.velocity = new Point(0, 0);
}

this.rectangle = new Rectangle(location.x, location.y,
image1.getWidth(rink), image1.getHeight(rink));
}

public int slide(Rectangle blocker, Rectangle blocker2)
{
Point position = new Point(rectangle.x, rectangle.y);
int returnValue = 0;

if (doNotMove){
return returnValue;
}

if(random.nextInt(100) <= 1){
velocity.x += random.nextInt() % maxVelocity;

velocity.x = Math.min(velocity.x, maxVelocity);
velocity.x = Math.max(velocity.x, -maxVelocity);

while(velocity.x == 0){
velocity.x = random.nextInt(maxVelocity / 2)
- maxVelocity / 2;
}

velocity.y += random.nextInt() % maxVelocity / 2;

velocity.y = Math.min(velocity.y, maxVelocity / 2);
velocity.y = Math.max(velocity.y, -(maxVelocity / 2));
}

position.x += velocity.x;
position.y += velocity.y;

if (position.x < edges.x + 5) {

if(position.y > 120 && position.y < 225){
if(!rectangle.intersects(blocker)){
returnValue = 1;
outOfAction = true;
return returnValue;
}
}
position.x = edges.x;

if(velocity.x > 0){
velocity.x = -6;
}else{
velocity.x = 6;
}
}else if ((position.x + rectangle.width)
> (edges.x + edges.width - 5)){
if(position.y > 120 && position.y < 225){
if(!rectangle.intersects(blocker2)){
returnValue = -1;
outOfAction = true;
return returnValue;
}
}
position.x = edges.x + edges.width - rectangle.width;

if(velocity.x > 0){
velocity.x = -6;
}else{
velocity.x = 6;
}
}

if (position.y < edges.y){
position.y = edges.y;
velocity.y = -velocity.y;

}else if ((position.y + rectangle.height)
> (edges.y + edges.height)){
position.y = edges.y + edges.height -
rectangle.height;
velocity.y = -velocity.y;
}

this.rectangle = new Rectangle(position.x, position.y,
image1.getWidth(rink), image1.getHeight(rink));
return returnValue;
}

public void slide(int y)
{
rectangle.y = y;
}

public boolean gone()
{
return outOfAction;
}

public boolean immovable()
{
return doNotMove;
}

public void drawPuckImage(Graphics g)
{
if(!outOfAction){
g.drawImage(image1, rectangle.x,
rectangle.y, rink);
}
}
}

class OkCancelDialog extends Dialog implements ActionListener
{
Button ok, cancel;
TextField text;
public String data;

OkCancelDialog(Frame hostFrame, String title, boolean dModal)
{
super(hostFrame, title, dModal);
setSize(280, 100);
setLayout(new FlowLayout());
text = new TextField(30);
add(text);
ok = new Button("OK");
add(ok);
ok.addActionListener((ActionListener)this);
cancel = new Button("Cancel");
add(cancel);
cancel.addActionListener(this);
data = new String("");
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource() == ok){
data = text.getText();
} else {
data = "";
}
setVisible(false);
}
}
ҡس : / rirut_9999@hotmail.com [2012-05-28 14:48:12]  

58.8.26.40 Դ #28628 (ҡ IP: 58.8.26.40)
ç㨨Ъ µͧ仡͹Сѹ
ҡس : [2012-06-01 18:53:39]
ͺз
´

- - - - - - - - - - - - - - ʹѺʹع- - - - - - - - - - - - - -

 
 
µͨ Ѻͧ macbook pro 2016 (usb-c to HDMI)
µͨ Ѻͧ macbook pro 2016 (usb-c to HDMI) ´дѺ 4K
ŧ Mini DisplayPort to HDMI Adapter ŧ Mini DisplayPort to HDMI Adapter Ѻͧ macbook air/pro, mac mini
ŧ Mini DisplayPort to HDMI Adapter Ѻͧ macbook air/pro, mac mini
µٿѧ Ъ Ѻ iPhone 7  7 Plus
µٿѧ Ъ Ѻ iPhone 7 7 Plus
ŧҡ USB-C Type  USB 3.1 Ѻ MacBook
ŧҡ USB-C Type USB 3.1 Ѻ MacBook
䢤ǧѺͧ Macbook Air/Pro
䢤ǧ Ѻͧ Macbook Air/Pro (300ҷ)
HDMI  3 ͡  1
HDMI 3 ͡ 1 (350ҷ)
Ի͹¹
¹¹ͷ
¹¹͹´
¹¹
¹¹ վʾ
¹¹
¹ҹԹء
¹¹Թ⿹
¹¹Ϊ ʤ
9M Blog ͧ͡ ¹ͧ
ԡ ҾѺԭ
OrmSin.com: ˹ѧͧҹ--չ (country crafts)
ѵҤɳ
 
Tutorial
eVB Tutorial
ASP.NET Webmatrix Tutorial
J2ME Tutorial
C++  Tutorial
Java  Tutorial
PHP Tutorial
ASP Tutorial
 
ẺǨ
Freelance ѺѲ

Home - Article - Tutorial - Sourcecode - Dev Handbook - Search - WebBoard - Links - About Us

ʧǹԢԷ Ѵ͡ ӫ Ѵŧ ٻẺ Ѻ͹حҵѡɳѡ
Ţ¹ҳԪ硷͹ԡҡзǧҳԪ: 7100503002000
Copyright 2001 SourceCode.in.th