框架中预期的红色矩形,什么也看不见

时间:2013-05-06 04:43:57

标签: java swing jpanel java-2d thread-sleep

以下是代码:

班级aa

package com.MahBonnets.Game;

import javax.swing.*;

public class aa {

public static ab f = new ab();
public static int width = 600;
public static int height = 400;
public static void main(String args[]) {
        f.setSize(width, height);
        f.setResizable(false);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("MAH BONNETS IS GONE");
        f.setLocationRelativeTo(null);
        System.out.println("Running!!");
}
}

AB

package com.MahBonnets.Game;

import java.awt.GridLayout;
import javax.swing.*;

public class ab extends JFrame {

public ac panel;

public ab() {
    panel = new ac(this);
    setLayout(new GridLayout (1, 1, 0, 0));
    add(panel);
}
}

和ac

package com.MahBonnets.Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.*;

public class ac extends JPanel implements Runnable {
public Rectangle floor;

public int floorheight = 80;
public int fps = 1000;

public boolean objectDefine = false;

public Thread game;
public ac(ab f) {
    setBackground(Color.black);

    defineObjects();

    game = new Thread(this);
    game.start(); }

void defineObjects() {
        floor = new Rectangle(-10, aa.height-floorheight, aa.width+10, floorheight);
        objectDefine = true;
        repaint();
}

public void paint(Graphics g) {
    super.paint(g);

    if(objectDefine) {
        g.setColor(Color.RED);
        g.fillRect(floor.x, floor.y, floor.width, floor.height);
}
}


public void fpsSetter() {
try{
    Thread.sleep(fps/1000); 
}catch(Exception e) {
    e.printStackTrace();

        }

  }
  @Override
  public void run(){
      // TODO Auto-generated method stub
  }
}

应该发生的事情是,一个红色矩形应该出现在JFrame的底部。我对编程完全不熟悉,但我查看了与矩形有关的代码部分,所有内容都按顺序查看......至少......据我所知。

如果你知道什么是错的,请帮助我。谢谢。

以下是我一直关注的http://www.youtube.com/watch?v=0lfhcKAIr-8

的youtube教程

3 个答案:

答案 0 :(得分:3)

  1. 不要依赖魔法数字......
  2. 不要依赖可能与现实不匹配的参数(public static int width = 600与您的子组件的大小不同)。使用getWidthgetHeight获取组件的实际大小......
  3. 摆脱您的defineObjects并依赖组件的实际已知状态
  4. 摆脱paint方法并使用paintComponent代替
  5. static并不总是你的朋友
  6. 请勿使用YouTube作为参考,除非海报有参考文献;)
  7. 请阅读Performing Custom Painting
  8. 请阅读Painting in AWT and Swing
  9. 请阅读Concurrency in Swing
  10. paint方法替换为更类似的内容......

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
    
        int x = -10;
        int y = getHeight() - floorheight;
        int width = getWidth() + 10;
        int height = floorheight;
        floor = new Rectangle(x, y, width, height);
        g.setColor(Color.RED);
        g.fillRect(floor.x, floor.y, floor.width, floor.height);
    }
    

    并且Thread.sleep(1000 / 1000)几乎没有睡眠就没有区别;) - 25fps粗糙40毫秒;)

答案 1 :(得分:2)

不要覆盖paint(Graphics g)。尝试覆盖paintComponent(Graphics g)

答案 2 :(得分:2)

在你的“aa”类中,而不是ab的静态实例(行:public static ab f = new ab();),尝试使用main方法的local变量。像:

//public static ab f = new ab();
public static int width = 600;
public static int height = 400;
public static void main(String args[]) {
        ab f = new ab(); // local var
        f.setSize(width, height);
        f.setResizable(false);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("MAH BONNETS IS GONE");
        f.setLocationRelativeTo(null);
        System.out.println("Running!!");
}

或者,您可以从“ac”类中的defineObjects()方法中删除对aa.width和aa.height的静态引用。

floor = new Rectangle(-10, aa.height-floorheight, aa.width+10, floorheight); //take out the static references !!!

做一个简单的测试来了解原因。在进行任何更改之前,打印aa.width和aa.height的值,您将看到它们为零。