Java上边界图形 [英] Java Upper Boundary Graphics

查看:226
本文介绍了Java上边界图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于基本碰撞检测的短片:

https://www.youtube.com/watch?v=ptqhnmP8FY0&list=PL6E90696571998DC2



有人可以告诉我为什么上限需要是球的大小?我认为椭圆形是从左上角开始绘制一个边界矩形。如果是这样的话,看起来y == 0将是上限,但显然不是这种情况。



总结问题:为什么上层如果边界三角形从左上角开始,则不是0。



以下是代码:

  public class JavaGame extends JFrame {

int x,y,sizeX = 350,sizeY = 350;
// boolean erase = false;

private Image dbImage;
私人图形dbg;

public class AL extends KeyAdapter {

public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();

if(keyCode == e.VK_LEFT){

if(x <= 0)
x = 0;
else
x- = 5; (keyCode == e.VK_RIGHT){
if(x == sizeX-20)
x = sizeX-20;
}
;
else
x + = 5;
if(keyCode == e.VK_UP){
if(y == 20)
y = 20;
else
y- = 5;
if(keyCode == e.VK_DOWN){
if(y == sizeY-20)
y = sizeY-20;
else
y + = 5;
}

/ * if(keyCode == e.VK_S){
erase = true;


public void keyReleased(KeyEvent e){
$ b}
}

public JavaGame() {
addKeyListener(new AL());
x = y = 150;
setTitle(Java Game);
setBackground(Color.WHITE);
setSize(sizeX,sizeY);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


$ b public void paint(Graphics g){

dbImage = createImage(getWidth(),getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage,0,0,this);


$ b public void paintComponent(Graphics g){

g.fillOval(x,y,20,20);

/ * if(erase){
g.setColor(Color.WHITE);
g.fillRect(0,0,500,500);
erase = false;
} * /

repaint();

$ b}

public static void main(String [] args){


new JavaGame();



$ b


解决方案

它的标题栏高度 JFrame 这就是为什么 y 在22之后开始可见



JFrame x >外边框宽度。



下面是示例代码以及快照

  g.setColor(Color.RED); 
g.drawRect(3,22,200,200);


- 编辑 -



我从来不建议您使用它。阅读下面的评论,因为它的缺点。


Regarding this short video of basic collision detection:

https://www.youtube.com/watch?v=ptqhnmP8FY0&list=PL6E90696571998DC2

Can someone tell me why the upper boundary needs to be the size of the ball? I thought that an oval was drawn with a bounding rectangle starting from the upper left. If that is the case then it would seem that y==0 would be the upper boundary, but that is obviously not the case.

Summary question: why is the upper boundary 20 and not 0 if the bounding triangle starts in the upper left.

Here is the code:

public class JavaGame extends JFrame {

int x,y,sizeX = 350,sizeY=350;
//boolean erase = false;

private Image dbImage;
private Graphics dbg;

public class AL extends KeyAdapter{

    public void keyPressed(KeyEvent e){
        int keyCode = e.getKeyCode(); 

        if (keyCode == e.VK_LEFT){

            if(x<= 0)
                x=0;
            else
                x-=5;
        }
        if (keyCode == e.VK_RIGHT){
            if(x==sizeX-20)
                x=sizeX-20;
            else
                x+=5;
        }if (keyCode == e.VK_UP){
            if(y==20)
                y=20;
            else
            y-=5;
        }if (keyCode == e.VK_DOWN){
            if(y==sizeY-20)
                y=sizeY-20;
            else
            y+=5;
        }

        /*if (keyCode == e.VK_S){
            erase = true;
        }*/
    }
    public void keyReleased(KeyEvent e){

    }
}

public JavaGame(){
    addKeyListener(new AL());
    x=y=150;
    setTitle("Java Game");
    setBackground(Color.WHITE);
    setSize(sizeX,sizeY);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


public void paint(Graphics g){

    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);

}

public void paintComponent(Graphics g){

    g.fillOval(x, y, 20, 20);

    /*if(erase){
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 500, 500);
        erase = false;
    }*/

    repaint();


}

public static void main(String[] args) {


    new JavaGame();

}

}

解决方案

Its the height of title bar of JFrame that's why y starts visible after 22.

Same for x that starts visible from 2 due to JFrame outer border width.

Here is the sample code along with snapshot

    g.setColor(Color.RED);
    g.drawRect(3, 22, 200, 200);

--EDIT--

I never suggest you to use it. Read below comments for it's drawbacks.

这篇关于Java上边界图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆