Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 矩形内移动球程序_Java_Swing_Graphics_Actionlistener_Actionevent - Fatal编程技术网

Java 矩形内移动球程序

Java 矩形内移动球程序,java,swing,graphics,actionlistener,actionevent,Java,Swing,Graphics,Actionlistener,Actionevent,我正在创建一个移动球程序,该程序的特点是一个球在一个矩形的墙上移动和反弹,底部有“前进”和“停止”按钮 我遇到的问题是,当程序运行时,我希望球开始移动,并从矩形内的直线反弹,这是我的主要问题。下面是我的代码: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagLayout; import

我正在创建一个移动球程序,该程序的特点是一个球在一个矩形的墙上移动和反弹,底部有“前进”和“停止”按钮

我遇到的问题是,当程序运行时,我希望球开始移动,并从矩形内的直线反弹,这是我的主要问题。下面是我的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

//Does the drawing
class MyDrawing extends JPanel {

private int xpos;
private int ypos;

public void setXPos(final int x) {
    this.xpos = x;
}

public void setYPos(final int y) {
    this.ypos = y;
}

public int getXpos() {
    return xpos;
}

public int getYpos() {
    return ypos;
}

@Override
public void paintComponent(final Graphics g) {
    super.paintComponent(g);
    final Graphics2D g2 = (Graphics2D) g;
    g.setColor(Color.red);
    final Ellipse2D.Double circle = new Ellipse2D.Double(xpos, ypos, 50, 50);
    g2.draw(circle);
    g2.fill(circle);
    final Rectangle box1 = new Rectangle(10, 10, 380, 300);
    g.setColor(Color.BLACK);
    g2.draw(box1);
}
}


public class ControlledBall extends JFrame {
private final JButton       flash = new JButton("Go");
private final JButton       steady = new JButton("Stop");
private final JPanel        panel = new JPanel(new GridBagLayout());
private final MyDrawing     drawing = new MyDrawing();
private final Timer         timer;


//direction
private int dx = 3;
private int dy = 2;

public ControlledBall() {
    panel.add(flash);
    panel.add(steady);
    this.add(panel, BorderLayout.SOUTH);
    this.add(drawing, BorderLayout.CENTER);
    drawing.setXPos(300);
    drawing.setYPos(150);
    steady.addActionListener(new SteadyListener());
    final MoveListener ml = new MoveListener();
    flash.addActionListener(ml);
    timer = new Timer(15, ml);
}

class MoveListener implements ActionListener {
    @Override
    public void actionPerformed(final ActionEvent event) {
        if (!timer.isRunning()){
            timer.start();
        }
        move();
    }
}

class SteadyListener implements ActionListener {
    @Override
    public void actionPerformed(final ActionEvent event) {
        if (timer.isRunning()){
            timer.stop();
        }
    }
}

private void move() {

    int x = drawing.getXpos();
    int y = drawing.getYpos();
    final int dia = 30;
    if (x + dx < 0 || x + dia + dx > getWidth()) {
        dx *= -1;
    }
    if (y + dy < 0 || y + dia + dy > getHeight()) {
        dy *= -1;
    }
    x += dx;
    y += dy;
    drawing.setXPos(x);
    drawing.setYPos(y);
    repaint();
}

public static void main(final String[] args) {
    final JFrame window = new ControlledBall();
    window.setSize(400, 400);
    window.setTitle("Controlled Ball");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.GridBagLayout;
导入java.awt.Rectangle;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.geom.Ellipse2D;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
//这幅画怎么样
类MyDrawing扩展了JPanel{
私人int XPO;
私营机构;
公共无效setXPos(最终整数x){
this.xpos=x;
}
公共无效setYPos(最终整数y){
this.ypos=y;
}
public int getXpos(){
返回XPO;
}
公共int getYpos(){
返回YPO;
}
@凌驾
公共组件(最终图形g){
超级组件(g);
最终图形2d g2=(图形2d)g;
g、 setColor(Color.red);
最终椭圆E2D.Double圆圈=新椭圆E2D.Double(xpos,YPO,50,50);
g2.画(圆);
g2.填充(圆圈);
最终矩形框1=新矩形(10,10,380,300);
g、 设置颜色(颜色为黑色);
g2.绘图(框1);
}
}
公共类ControlledBall扩展JFrame{
私有最终JButton flash=新JButton(“Go”);
私有最终按钮稳定=新按钮(“停止”);
私有最终JPanel面板=新JPanel(新GridBagLayout());
私人最终MyDrawing图纸=新MyDrawing();
私人最终定时器;
//方向
私有整数dx=3;
私有int dy=2;
公共控制球(){
面板。添加(闪存);
面板。添加(稳定);
此.add(面板,BorderLayout.SOUTH);
添加(图纸、边框布局、中心);
图纸:setXPos(300);
setYPos(150)号图纸;
addActionListener(新的SteadyListener());
final MoveListener ml=新的MoveListener();
flash.addActionListener(ml);
定时器=新定时器(15毫升);
}
类MoveListener实现ActionListener{
@凌驾
已执行的公共无效操作(最终操作事件){
如果(!timer.isRunning()){
timer.start();
}
move();
}
}
类SteadyListener实现ActionListener{
@凌驾
已执行的公共无效操作(最终操作事件){
if(timer.isRunning()){
timer.stop();
}
}
}
私人空位移动(){
int x=drawing.getXpos();
int y=drawing.getYpos();
最终内径=30;
如果(x+dx<0 | | x+dia+dx>getWidth()){
dx*=-1;
}
如果(y+dy<0 | | y+dia+dy>getHeight()){
dy*=-1;
}
x+=dx;
y+=dy;
图纸:setXPos(x);
图纸:setYPos(y);
重新油漆();
}
公共静态void main(最终字符串[]args){
最终JFrame窗口=新的可控球();
设置窗口大小(400400);
设置标题(“受控球”);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}

问题在于检查的边界。如果要对照矩形进行检查,则必须将大小作为变量实现,并将其输入“bounce”-检查或手动添加(从代码中获取矩形的大小):

if(x+dx<10 | | x+dia+dx>380){
dx*=-1;
}
如果(y+dy<10 | | y+dia+dy>300){
dy*=-1;
}

如果你想得到真实的距离,使用球的hallf大小的偏移量,并将其添加到此代码中。我认为这比速度向量
dx
dy
要好。

问题在于你检查的边界。如果要对照矩形进行检查,则必须将大小作为变量实现,并将其输入“bounce”-检查或手动添加(从代码中获取矩形的大小):

if(x+dx<10 | | x+dia+dx>380){
dx*=-1;
}
如果(y+dy<10 | | y+dia+dy>300){
dy*=-1;
}

如果你想得到真实的距离,使用球的hallf大小的偏移量,并将其添加到此代码中。我认为这比速度向量
dx
dy
更好。

如果你想让他们弹起,你还必须保存球的方向,并使用一些基本的三角来获得新的方向如果你想让他们弹起,你还必须保存球的方向,并使用一些基本的三角来获得新的方向
 if (x + dx < 10 || x + dia + dx > 380) {
        dx *= -1;
    }
    if (y + dy < 10 || y + dia + dy > 300) {
        dy *= -1;
    }