【问题标题】:Bouncing Cube Program from The Office来自办公室的弹跳立方体程序
【发布时间】:2014-04-17 00:06:28
【问题描述】:

我正在编写一个取自电视节目《办公室》的节目,当时他们正坐在会议室里,看着屏幕上弹跳的 DVD 徽标试图冲到角落里。正方形应该在碰到边缘时改变颜色。 但是,我遇到了一些问题。

问题一:Square 有时会从边缘反弹。其他时候它会下沉,我不知道为什么。

问题二:我不知道如何改变方块碰到边缘时的颜色。

问题三:我正在尝试学习如何制作全屏 JFRAME。不仅在我的屏幕上全屏,而且在任何人的屏幕上。

代码已发布到在线 IDE 以方便阅读。可以找到HERE

否则,如果您太忙于该链接。这里贴在下面。

 import java.util.Random;
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;

 public class BouncingMischievousSquare extends JPanel implements ActionListener { 

private static final int SQUARE_SIZE = 40;
private static final int SPEED_OF_SQUARE = 6;
private int xPosit, yPosit;
private int xSpeed, ySpeed;

BouncingMischievousSquare(){
    //speed  direction
    xSpeed = SPEED_OF_SQUARE;
    ySpeed = -SPEED_OF_SQUARE;
    //a timer for repaint 
    //http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
    Timer timer = new Timer(100, this);
    timer.start();
}
public void actionPerformed(ActionEvent e){
    //Screensize
    int width = getWidth();
    int height = getHeight();
    xPosit += xSpeed;
    yPosit += ySpeed;
    //test xAxis
    if(xPosit < 0){
        xPosit = 0;
        xSpeed = SPEED_OF_SQUARE;
    }
    else if(xPosit > width - SQUARE_SIZE){ 
        xPosit = width - SQUARE_SIZE;
        xSpeed = -SPEED_OF_SQUARE;
    }
    if(yPosit < 0){
        yPosit = 0;
        ySpeed = SPEED_OF_SQUARE;
    }
       else if(yPosit > height - SQUARE_SIZE){ 
        xPosit = height - SQUARE_SIZE;
        xSpeed = -SPEED_OF_SQUARE;
       }
    //ask the computer gods to redraw the square
    repaint();
}
 public void paintComponent(Graphics g){       
     super.paintComponent(g);
     g.fillRect(xPosit, yPosit, SQUARE_SIZE, SQUARE_SIZE );
 }
 }

主类

 import javax.swing.*;



public class MischievousMain {
public static void main(String[] args) {
   JFrame frame = new JFrame("Bouncing Cube");
   frame.setSize(500, 500);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   // mischievous square input
   frame.add(new BouncingMischievousSquare());
   frame.setVisible(true);
}  



}

无论如何,感谢您花时间阅读我的代码。值得赞赏。我真的对解决这个问题的不同方法很感兴趣。

【问题讨论】:

    标签: java swing jtable jframe jpanel


    【解决方案1】:

    问题一:Square 有时会从边缘反弹。其他时候 下沉,我不知道为什么。

    你会因此而讨厌自己,但是

    } else if (yPosit > height - SQUARE_SIZE) {
        xPosit = height - SQUARE_SIZE;
        xSpeed = -SPEED_OF_SQUARE;
    }
    

    应该是……

    } else if (yPosit > height - SQUARE_SIZE) {
        yPosit = height - SQUARE_SIZE;
        ySpeed = -SPEED_OF_SQUARE;
    }
    

    您使用的是xPosyitxSpeed 而不是yPosyitySpeed...

    问题二:不知道怎么改变方块的颜色 击中边缘。

    基本上,每当您检测到边缘碰撞并改变方向时,只需将面板的前景色更改为其他颜色...

    这可能需要您有一个颜色列表,您可以从中随机选择或简单地随机生成颜色

    然后在您的paintComponent 方法中,在填充矩形之前简单地使用g.setColor(getForeground())...

    ...ps...

    为了让生活更轻松,您可以编写一个生成随机颜色或将前景设置为随机颜色的方法,例如...

    protected void randomiseColor() {
    
        int red = (int) (Math.round(Math.random() * 255));
        int green = (int) (Math.round(Math.random() * 255));
        int blue = (int) (Math.round(Math.random() * 255));
    
        setForeground(new Color(red, green, blue));
    
    }
    

    问题三:我正在尝试学习如何制作 JFRAME 全屏。和 不仅在我的屏幕上全屏,而且在任何人的屏幕上。

    看看Full-Screen Exclusive Mode API

    【讨论】:

    • 啊哈。谢谢你指出这一点。非常感谢,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    相关资源
    最近更新 更多