我沒用java寫過代碼,所以我只說算法,代碼你自己翻譯下
創(chuàng)新互聯(lián)建站專注于企業(yè)營(yíng)銷型網(wǎng)站、網(wǎng)站重做改版、皮山網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為皮山等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
按C的語(yǔ)法來:
void xiaoqiu
{
int UB=10,DB=200,LB=10,RB=200; //定義彈球范圍的邊界
int sh=1; //定義橫向步長(zhǎng)
int sz=1; //定義縱向步長(zhǎng)(兩步長(zhǎng)之比決定了反彈的角度)
int x=LB,y=UB; //定義坐標(biāo)
int i=10000; //循環(huán)次數(shù)(自己選擇跳出手段)
while(i0)
{
i--;
x=x+sh;
if(x=RB||x=LB) sh=-sh; //碰壁后步長(zhǎng)變反
y=y+sz;
if(y=DB||y=UB) sz=-sz; //碰壁后步長(zhǎng)變反
(顯示代碼)
}
return;
}
總得來說,就是相當(dāng)于橫向和縱向分別處理移動(dòng)、反彈的問題,碰壁后步長(zhǎng)變?yōu)橄喾磾?shù)
不懂請(qǐng)追問
這個(gè)問題主要的幾點(diǎn)就是:移動(dòng)與邊界判斷,小球與木板移動(dòng),這個(gè)通過修改座標(biāo)來實(shí)現(xiàn),邊界判斷,這個(gè)也通過座標(biāo)值判斷;移動(dòng)的板跟隨鼠標(biāo)移動(dòng),通過MouseMotionListener監(jiān)聽鼠標(biāo)移動(dòng),再修改木板座標(biāo)實(shí)現(xiàn);小球移動(dòng),設(shè)置2個(gè)方向,水平與垂直,0,座標(biāo)值增加(向右或向下移動(dòng)),0座標(biāo)減?。ㄏ蛏匣蛳蜃笠苿?dòng)),到達(dá)邊界后,修改方向值,使其向反方向移動(dòng)。
因?yàn)?,有大神先貼了代碼,就不獻(xiàn)丑了。如果需要代碼,可以追問我。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CopyOfBallCrash implements Runnable {
public static void main(final String[] args) {
new Thread(new CopyOfBallCrash()).start();
}
private final int ? ? ? ? ? width ? ? = 400;
private final int ? ? ? ? ? height ? ?= 700;
private int ? ? ? ? ? ? ? ? mouse_X, mouse_Y;
private final BufferedImage offscreen = new BufferedImage(width, height,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?BufferedImage.TYPE_4BYTE_ABGR);
private final JPanel ? ? ? ?panel ? ? = new JPanel();
private final Shape ? ? ? ? ball ? ? ?= new Shape(100, 100, 1, 1, 20);
private final Shape ? ? ? ? rect ? ? ?= new Shape(0, 100, 20);
public CopyOfBallCrash() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(new Dimension(width, height));
ball.setBounds(width, height);
rect.setBounds(width, height);
frame.setContentPane(panel);
panel.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(final MouseEvent e) {}
@Override
public void mouseMoved(final MouseEvent e) {
mouse_X = e.getX();
mouse_Y = e.getY();
}
});
frame.pack();
frame.setVisible(true);
}
public void paint(final Graphics g) {
final Graphics2D g2d = offscreen.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.blue);
rect.drawRect(g2d, mouse_X);
g2d.setColor(Color.red);
ball.drawOval(g2d, rect);
if (Shape.isLose) g2d.drawString("你輸了!!!", 100, 300);
g2d.dispose();
g.drawImage(offscreen, 0, 0, null);
}
@Override
public void run() {
while (true)
paint(panel.getGraphics());
}
}
class Shape {
public int ? ? ? ? ? ?width, height;
public int ? ? ? ? ? ?x, y, vx, vy, r, w, h;
public static boolean isLose;
public Shape(final int x, final int y, final int vx, final int vy, final int r) {
super();
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.r = r;
}
public Shape(final int x, final int w, final int h) {
super();
this.x = x;
this.w = w;
this.h = h;
}
public final void setBounds(final int width, final int height) {
this.width = width;
this.height = height;
}
public final void drawOval(final Graphics2D g2d, final Shape shape) {
if (y + h = height) {
isLose = true;
return;
}
if (x + vx = 0 || x + vx + w = width) vx = -vx;
if (y + vy = 0) vy = -vy;
if (isCrashOutside(shape.x, shape.y, shape.w, shape.h) y + w = shape.y)
vy = -vy;
x += vx;
y += vy;
g2d.fillOval(x, y, r, r);
}
public final void drawRect(final Graphics2D g2d, final int mouseX) {
y = height - h;
if (x + w width x mouseX) x++;
if (x 0 x mouseX) x--;
g2d.fillRect(x, y, w, h);
}
public final boolean isCrashOutside(final int x, final int y, final int w,
final int h) {
return (this.x x ? this.x = w + x : x = r + this.x)
(this.y y ? this.y = h + y : y = r + this.y);
}
}