import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.Timer;
public class BouncingLinesWithSound implements ActionListener
{
private Random rand = new Random();
private Timer myTimer;
private Picture myPic;
//TODO: Changed form Graphics
private Graphics2D canvas;
//TODO: Requires SoundPlayer.java to be in same project src folder as this file
private SoundPlayer boinkSound;
private static final int DRAW_WIDTH = 600;
private static final int DRAW_HEIGHT = 600;
private int x1, y1, x2, y2;
private int x3, y3, x4, y4;
private int speedX1, speedX2, speedY1, speedY2;
private int speedX3, speedX4, speedY3, speedY4;
//////////////////////////////////////////////////////////////////////
// BouncingLine Constructor. This constructor:
// 1) Creates an empty Picture Frame
// 2) Sets the initial location, and speed of each end point of a line.
// 3) Start the timer.
/////////////////////////////////////////////////////////////////////
public BouncingLinesWithSound()
{
myPic = new Picture(DRAW_WIDTH, DRAW_HEIGHT);
//TODO
//If you want a Graphics2D rather than a graphics, just cast
// the value returned by myPic.getOffScreenGraphics()
canvas = (Graphics2D)myPic.getOffScreenGraphics();
canvas.setColor(Color.WHITE);
canvas.fillRect(0, 0, DRAW_WIDTH, DRAW_HEIGHT);
myPic.setTitle("Bouncing Line");
x1 = rand.nextInt(DRAW_WIDTH);
y1 = rand.nextInt(DRAW_HEIGHT);
x2 = rand.nextInt(DRAW_WIDTH);
y2 = rand.nextInt(DRAW_HEIGHT);
x3 = rand.nextInt(DRAW_WIDTH);
y3 = rand.nextInt(DRAW_HEIGHT);
x4 = rand.nextInt(DRAW_WIDTH);
y4 = rand.nextInt(DRAW_HEIGHT);
speedX1 = rand.nextInt(25)-12;
speedY1 = rand.nextInt(25)-12;
speedX2 = rand.nextInt(25)-12;
speedY2 = rand.nextInt(25)-12;
speedX3 = rand.nextInt(25)-12;
speedY3 = rand.nextInt(25)-12;
speedX4 = rand.nextInt(25)-12;
speedY4 = rand.nextInt(25)-12;
////////////////////////////////////////////
try
{
boinkSound = new SoundPlayer();
}
catch (Exception e)
{
System.out.println("BouncingLinesWithSound():: "+e.getMessage());
System.exit(0);
}
myTimer = new Timer(30, this); // miliseconds
myTimer.start();
}
///////////////////////////////////////////////////////////////////////
// Called each time the timer fires.
// 1) Erase the current line
// 2) Move the two end points of the line.
// 3) Check to see if either end point moved outside the frame. If so,
// give that end point a random speed in the direction away from
// the edge it moved off.
// 4) Draw the line in its new location.
////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent arg0)
{
//Erase Line in its current location
canvas.setColor(Color.WHITE);
canvas.drawLine(x1, y1, x2, y2);
canvas.drawLine(x3, y3, x4, y4);
x1 += speedX1;
y1 += speedY1;
x2 += speedX2;
y2 += speedY2;
x3 += speedX3;
y3 += speedY3;
x4 += speedX4;
y4 += speedY4;
boolean hit = false;
if (x1 < 0)
{ speedX1 = rand.nextInt(12)+1;
hit = true;
}
else if (x1 > DRAW_WIDTH)
{
speedX1 = -(rand.nextInt(12)+1);
hit = true;
}
if (y1 < 0)
{ speedY1 = rand.nextInt(12)+1;
hit = true;
}
else if (y1 > DRAW_HEIGHT)
{ speedY1 = -(rand.nextInt(12)+1);
hit = true;
}
if (x2 < 0)
{ speedX2 = rand.nextInt(12)+1;
hit = true;
}
else if (x2 > DRAW_WIDTH)
{
speedX2 = -(rand.nextInt(12)+1);
hit = true;
}
if (y2 < 0)
{ speedY2 = rand.nextInt(12)+1;
hit = true;
}
else if (y2 > DRAW_HEIGHT)
{ speedY2 = -(rand.nextInt(12)+1);
hit = true;
}
if (x3 < 0)
{ speedX3 = rand.nextInt(12)+1;
hit = true;
}
else if (x3 > DRAW_WIDTH)
{
speedX3 = -(rand.nextInt(12)+1);
hit = true;
}
if (y3 < 0)
{ speedY3 = rand.nextInt(12)+1;
hit = true;
}
else if (y3 > DRAW_WIDTH)
{
speedY3 = -(rand.nextInt(12)+1);
hit = true;
}
if (x4 < 0)
{ speedX4 = rand.nextInt(12)+1;
hit = true;
}
else if (x4 > DRAW_WIDTH)
{
speedX4 = -(rand.nextInt(12)+1);
hit = true;
}
if (y4 < 0)
{ speedY4 = rand.nextInt(12)+1;
hit = true;
}
else if (y4 > DRAW_WIDTH)
{
speedY4 = -(rand.nextInt(12)+1);
hit = true;
}
if (hit) boinkSound.play();
//Draw Line in new location
canvas.setColor(Color.RED);
canvas.drawLine(x1, y1, x2, y2);
canvas.drawLine(x3, y3, x4, y4);
myPic.repaint();
}
/////////////////////////////////////////////////////////////////////
// Create and run BouncingLinesWithSound
/////////////////////////////////////////////////////////////////////
public static void main(String[] args)
{
new BouncingLinesWithSound();
}
}
こんにちは、2本の線が画面の端に到達すると壁に跳ね返ったり跳ね返ったりするコードがあります。今私がやろうとしているのは、衝突した場合に線が互いに跳ね返るようにすることですが、方法がわかりません始めても