- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我昨天开始了这个“项目”,我想尝试一个立方体与窗口边框碰撞的程序。看起来很简单。
但是,我与边界作斗争。我似乎无法在 window 的右侧和底部获得“完美”的碰撞。立方体总是比预期走得远一点。
我认为问题与我设置窗口大小的方式有关?我不确定,但我确实尝试了一些改变,但没有成功。
代码如下:
package spritesInJava;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Game {
public static JFrame myGame;
public static Timer timer;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGame();
timer.start();
}
});
}
public static void createGame() {
myGame = new JFrame("Felicia");
myGame.add(new MyPanel());
myGame.pack();
myGame.setSize(new Dimension(1000,1000));
myGame.setPreferredSize(new Dimension(1000,1000));
myGame.setVisible(true);
myGame.setDefaultCloseOperation(1);
MyPanel.loadImage();
}
}
class MyPanel extends JPanel implements ActionListener {
private static Image image; //Sprite
private static final long serialVersionUID = 1251340649341903871L;
private static ImageObserver observer;
private int x = 50;
private int y = 50;
private int width = 200;
private int height = 100;
private int speedx = 2;
private int speedy = 2;
MyPanel() {
Game.timer = new Timer(10,this);
}
@Override
public void actionPerformed(ActionEvent e) {
int screenWidth = Game.myGame.getWidth();
int screenHeight = Game.myGame.getHeight();
x+=speedx;
y+=speedy;
if(x >= screenWidth - width) {
speedx = -speedx;
}
else if(x < 0 ) {
speedx = -speedx;
}
if(y >= screenHeight- height ) {
speedy = -speedy;
}
else if(y < 0 ) {
speedy = -speedy;
}
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//g.drawImage(image, x, y,width,height,observer); //Uses the image that was loaded below and draws it.
g.fillRect(x, y, width, height);
}
public static void loadImage() { //Load the image
ImageIcon icon = new ImageIcon("photos/Felicia.png");
image = icon.getImage(); //Set the "temp" ImageIcon icon to the public ImageIcon
}
}
立方体应该击中 4 个边界之一,然后更改为“-speed”,因此如果速度为 5,则速度应完全相反(负)并转向另一个方向。这确实有效,但是有点太晚了。看来我做的碰撞系统不起作用
抱歉将所有代码放在一个类中!
提前致谢!
最佳答案
所以,立即看来,这似乎是错误的......
myGame.add(new MyPanel());
myGame.pack();
myGame.setSize(new Dimension(1000, 1000));
myGame.setPreferredSize(new Dimension(1000, 1000));
myGame.setVisible(true);
您在设置/确定首选尺寸之前打包
窗口,这意味着窗口将希望采用可能的最小尺寸(作为内容的preferredSize
)默认为 0x0
)。
作为一般规则,您应该避免调用 setPreferredSize
,但我特别避免在 JFrame
上使用它。
JFrame
有装饰(窗口标题和边框),它们插入到窗口中,所以可视大小实际上是
frameSize -decorationInsets
,这成为问题的核心稍后。
因此,我会重写 MyPanel
中的 getPreferredSize
并指定组件的“可视”范围
class MyPanel extends JPanel implements ActionListener {
//...
@Override
public Dimension getPreferredSize() {
return new Dimension(1000, 1000);
}
然后在你的主类中你可以做...
myGame = new JFrame("Felicia");
myGame.add(new MyPanel());
myGame.pack();
myGame.setVisible(true);
API 将为您解决问题。
好的,进入下一期...
public void actionPerformed(ActionEvent e) {
int screenWidth = Game.myGame.getWidth();
int screenHeight = Game.myGame.getHeight();
Game.myGame
是一个 JFrame
,您要求的是窗口大小,但正如我之前所说,框架有装饰,它们插入到框架中,减少组件可用的可视区域。相反,您应该使用组件本身的大小
public void actionPerformed(ActionEvent e) {
int screenWidth = getWidth();
int screenHeight = getHeight();
我认为这些的必要性......
public static JFrame myGame;
public static Timer timer;
但我在使用 static
时遇到问题;)。使 myGame
static
可以“轻松”地从代码中不承担任何责任的位置访问其功能。
在这种情况下,计时器是有争议的,但我可能会想要一个封装它和过度更新工作流程的“更新引擎”,但这就是我;)
我认为这不是必需的(静态
)
public static void loadImage() { //Load the image
ImageIcon icon = new ImageIcon("photos/Felicia.png");
image = icon.getImage(); //Set the "temp" ImageIcon icon to the public ImageIcon
}
该功能应在创建时由组件本身创建(或通过其他“设置”阶段)。我还鼓励使用 ImageIO.read
,因为当出现问题时,它会生成 IOException
,这与 ImageIcon
不同,后者会静默失败。
作为(ImageIO.read
)的一个附带好处,您还可以知道图像何时完成加载,因为该方法在完成加载之前不会返回,这意味着您不必冒某些风险空帧。
你不需要...
private static ImageObserver observer;
它也不需要是静态
(因为私有(private)
它有点破坏任何好处),但是,JPanel
实际上是一个ImageObserver
,因此您只需将 this
传递给 drawImage
关于java - 我与 JPanel、getWidth 和 getHeight 作斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60582651/
我想在 64 位数字中设置各个位,并认为 uint64_t是这份工作的理想选择。我遇到了一些奇怪的问题,所以我做了一些实验: 我写了下面的测试程序: #include #include #incl
所以我知道 mapply 将按元素顺序遍历一个数组(或一系列数组)——我想知道是否有办法阻止它对某些参数执行此操作……。让我进一步解释 我有一组数据点,带有 X 和 Y 坐标……X1 X2 ……Xn
我是 android 编程的新手,我正在尝试创建一个好友列表类。 首先,我创建了一个从数据库加载对象的数组: friendArray = new Friend[NumberOfFriendsInDat
我目前正在开发一个应用程序,该应用程序定期(每 30 秒)使用 json 格式从两个指定的足球队获取英国境内的两条推文。在 json 文件中,我可以访问当前文本的每条推文的位置(曼彻斯特、布里斯托尔等
我正在尝试编写一些 javascript 来询问用户,当他们离开页面时,是否要填写调查(无论这可能多么烦人!)。我认为我的解决方案是通过此站点上的答案找到的。这是我目前拥有的代码,但似乎无法正常工作:
好吧,我有一些关于使用拆分 Pane 将我的框架分成两个区域的技巧,但我无法让它显示一些有用的东西。代码如下所示: public class Whiteboard extends JPanel { i
我在 mod_rewrite htaccess 上苦苦挣扎了至少几天,但仍然无法解决这个问题。 我想在我的网站上强制使用 HTTPS SSL,但仅限于网络外部。 我有这样的东西: RewriteCon
我有一个非常简单的 XML 文件,需要用 xmlstarlet 解析(我在 Windows 下): IDENTIFYER 2013-01-25T11:15:
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 2年前关闭。 Improve t
我真的很努力地处理类、扩展和实现。 在 android 中,我想创建一个首选项屏幕并检查用户输入。 因此,我可以使用名为preferences.class的文件,并使用以下类执行我需要的所有操作:
我正在尝试运行 hello world boost python example乍一看一切都很好。我得到以下输出 >bjam ...patience... ...patience... ...foun
抱歉打扰大家了,这让我很痛苦。这是代码: char buf[500]; sprintf(buf,"D:\\Important\\Calibration\\Results\\model_%i.xml",
我最近了解了在 C++ 中使用反向迭代器的正确方法(特别是当您需要删除一个时)。 (参见 this question 和 this one。) 你应该这样做: typedef std::vector
我在使用 flex-layout 时遇到了一些问题。 我的代码通常如下所示: 我希望第二个 flex d
我遇到一个问题,一个简单的函数在到达 std::cin 请求时似乎终止了。编译器不会抛出任何警告或错误,也不会发生运行时错误,程序只会返回到主菜单。 片段: #include #include u
好的,希望这对您来说应该很容易。我是 git、BitBucket 的新手,也是版本控制的新手,我会遗漏一些对您来说很明显的东西。 所以这就是我所做的:我在我的机器上安装了 git 并设置了我的全局变量
我正在处理一个需要高度自动化的 Oracle 过程,因此我正在使用动态 SQL。我刚开始使用动态 SQL。 我正在使用 varchar2 变量来构建需要执行的 SQL。我使用了 dbms_output
我正在学习 python 和 Kivy,并且我真的很难理解如何从 Kivy GUI 调用函数和继续函数。 这是我的.py: import csv import os import easygui im
我试图在 WebView 中显示一个用 LaTex 编写的数学公式,但我在字符“\”上挣扎:我不能写“\”,因为这是错误的,但如果我使用“\\"应用无法呈现公式。 url += "This is a
当我只有一项时,我在使用 ng-repeat 时遇到了一些问题。 这是我的 Controller 中的部分代码: Data.get('company').then(function(data){
我是一名优秀的程序员,十分优秀!