首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >获取类外部的JPanel的宽度和高度

获取类外部的JPanel的宽度和高度
EN

Stack Overflow用户
提问于 2019-01-04 02:47:14
回答 1查看 1.8K关注 0票数 0

所以我创建了一个简单的模拟,用随机向量和窗口边缘的反弹随机产生正方形。

我希望它能考虑到正在调整大小的窗口。因此,如果我将窗口的尺寸从600x600更改为1200x600,正方形将弹出新边框,而不是600x600。

我尝试执行getWidth() getHeight(),但它返回0。所以我把它放在pain()方法中(因为它在调整窗口大小时被调用),并将返回值保存为局部变量。但是我不能从Rect类调用getjpWidth()。

因此,基本上我需要的是将新的窗口维度放入Rect类的move()方法中。

请随时指出任何其他错误和可以做得更好的事情。我是2D编程新手(学习计算机科学)

应用程序

代码语言:javascript
复制
import javax.swing.*;

public class Application {
    private Application(){
        //create a JFrame window
        JFrame frame = new JFrame("Moving Squares");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //add a JPanel
        GamePanel gamePanel = new GamePanel();
        frame.add(gamePanel);
        //pack the window around the content
        frame.pack();
        //center
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String args[]){
        new Application();
    }
}

GamePanel

代码语言:javascript
复制
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;

public class GamePanel extends JPanel implements Runnable{
    private int jpWidth=0, jpHeight=0;

    //set JPanel size
    private static final Dimension DESIRED_SIZE = new Dimension(600,600);
    @Override
    public Dimension getPreferredSize(){
        return DESIRED_SIZE;
    }

    //constructor
    GamePanel(){
        Thread t = new Thread(this);
        t.start();
    }

    private ArrayList <Rect> rect=new ArrayList<>();
    public void run(){
        for(int i=0; i<15; i++){
            rect.add(new Rect());
        }
        while(true){
            for(Rect rect:rect){
                rect.move();
            }
            //repaint still image for better frames
            //should be 100fps instead it's >144fps
            repaint();
            try{Thread.sleep(10);}
            catch(InterruptedException e){/**/};
            repaint();
            try{Thread.sleep(10);}
            catch(InterruptedException e){/**/};
            repaint();
            try{Thread.sleep(10);}
            catch(InterruptedException e){/**/};
        }
    }

    public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D) g.create();
        jpWidth=getWidth();
        jpHeight=getHeight();
        g2d.setColor(Color.white);
        g2d.fillRect(0,0,jpWidth,jpHeight);
        for(Rect rect:rect) {
            g2d.setColor(Color.black);
            g2d.fillRect(rect.getXcord()-1, rect.getYcord()-1, rect.getWidth()+2, rect.getHeight()+2);
            g2d.setColor(Color.getHSBColor(rect.getR(), rect.getG(), rect.getB()));
            g2d.fillRect(rect.getXcord(), rect.getYcord(), rect.getWidth(), rect.getHeight());
        }
    }

    public int getJpWidth() {
        return jpWidth;
    }

    public int getJpHeight() {
        return jpHeight;
    }
}

Rect

代码语言:javascript
复制
import java.util.Random;

public class Rect {
    //properties
    private int width=30, height=30;
    private int R, G, B;
    //movement
    private int xCord, yCord;
    private int xVector, yVector;
    private int xSlope, ySlope;

    public Rect(){
        Random rand = new Random();
        //random color
        R=rand.nextInt(255);
        G=rand.nextInt(255);
        B=rand.nextInt(255);

        //random spawn position
        xCord=rand.nextInt(600-width);
        yCord=rand.nextInt(600-height);

        //direction
        do{
            xVector=rand.nextInt(3) - 1;
            yVector=rand.nextInt(3) - 1;
        }while(xVector==0 || yVector==0);

        //slope
        do{
            xSlope=rand.nextInt(3);
            ySlope=rand.nextInt(3);
        }while(xSlope==0 || ySlope==0);
        xVector*=xSlope;
        yVector*=ySlope;
    }

    public void move(){
        //if(xCord>=//how to get screen width ? ){}
        if((xCord>=600-width) || (xCord<=0)){
            bounceX();
        }
        if((yCord>=600-height) || (yCord<=0)) {
            bounceY();
        }
        xCord+=xVector;
        yCord+=yVector;
    }

    public void bounceX(){
        xVector*=-1;
    }

    public void bounceY(){
        yVector*=-1;
    }

    public int getR() {
        return R;
    }

    public int getG() {
        return G;
    }

    public int getB() {
        return B;
    }

    public int getXcord() {
        return xCord;
    }

    public int getYcord() {
        return yCord;
    }

    public int getWidth(){
        return width;
    }

    public int getHeight(){
        return height;
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54028090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档