JFrame repaint()和revalidate()仅在Mac OS上调整窗口大小时更新

时间:2017-03-04 01:37:04

标签: java macos swing jframe repaint

我将这个课程用于我的学校应用项目。这是我设置应用程序的方式,它扩展了JFrame并实现了Runnable。现在,无论何时我在学校的Windows计算机上使用它,一切正常,屏幕更新,但在Mac上的家里却没有。我使用Eclipse霓虹灯和JDK 1.8.0_101 请帮帮我,我不能在家测试任何项目的原因。

import java.awt.Graphics;
import javax.swing.JFrame;

public abstract class GUIApplication extends JFrame implements Runnable{

    private Screen currentScreen;
    //no main, cant instentiate an abstract class
    public GUIApplication(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int x=40;
        int y=40;
        int width=1000;
        int height=640;
        setBounds(x,y,width,height);
        initScreen();
        setVisible(true);
    }
    //this is a method for creating the starting screen
    protected abstract void initScreen();

    public void setScreen(Screen screen){
        //stop controls from previous screen
        removeListeners();

        setCurrentScreen(screen);
        //add new controls
        addListeners();
    }

    private void removeListeners(){
        if(getCurrentScreen() != null){
            if(getCurrentScreen().getMouseListener() != null)     removeMouseListener(getCurrentScreen().getMouseListener());
            if(getCurrentScreen().getMouseMotionListener() != null) removeMouseMotionListener(getCurrentScreen().getMouseMotionListener());
            if(getCurrentScreen().getKeyListener() != null) removeKeyListener(getCurrentScreen().getKeyListener());
        //      if(currentScreen.getMouseWheelListener() != null) removeMouseWheelListener(currentScreen.getMouseWheelListener());
        }
    }

    private void addListeners(){
        if(getCurrentScreen() != null){
            if(getCurrentScreen().getMouseListener() != null)addMouseListener(getCurrentScreen().getMouseListener());
            if(getCurrentScreen().getMouseMotionListener() != null) addMouseMotionListener(getCurrentScreen().getMouseMotionListener());
            if(getCurrentScreen().getKeyListener() != null){
                addKeyListener(getCurrentScreen().getKeyListener());
            }
        //      if(currentScreen.getMouseWheelListener() != null) addMouseWheelListener(currentScreen.getMouseWheelListener());
        }
    }

    public void paint(Graphics g){
        g.drawImage(getCurrentScreen().getImage(), 0, 0, null);
    }

    public void run(){
        while(true){
            getCurrentScreen().update();
            repaint();
            try {
                Thread.sleep(30);
                repaint();
                revalidate();

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public Screen getCurrentScreen() {
        return currentScreen;
    }
    public void setCurrentScreen(Screen currentScreen) {
        this.currentScreen = currentScreen;
    }

}

这就是游戏开始的方式:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.JFrame;


import game.mainScreenTeam.Dragon;
import game.mainScreenTeam.HomeScreen;
import game.miniGameTeam.GameInstructions;
import game.miniGameTeam.GameScreen;
import game.miniGameTeam.HighScoreScreen;
import game.shopScreen.BuyScreenWendy;
import game.shopScreen.HomeShopScreen;
import game.shopScreen.SellShopZheng;

import guiPractice.GUIApplication;
import guiPractice.Screen;
import guiPractice.components.AnimatedComponent;

/**
 * @author Kat
 *
 */
public class DragonLand extends GUIApplication {

    public static DragonLand game;
    public static int coins = 1500; 
    public static HomeScreen homeScreen;
    public static Screen shopMain; // shop 1
    public static Screen sellScreen; // shop 2
    public static Screen buyScreen; // shop 3
    public static Screen highscoreScreen; // high score
    public static GameScreen miniGameScreen; // minigame
    public static Screen gameInstructionsScreen;
    public static Screen HelpScreen;
    public static Color NAVY;
    public static Color BRIGHT_PINK;
    public static Color LIGHT_PINK;
    public static Color LIGHT_NUDE;
    public static Color DARKER_NUDE;


    /**
     * 
     */
//  public static void addDragon(AnimatedComponent a){
//      dragonList.add(a);
//  }
    public DragonLand() {

    }

    /* (non-Javadoc)
     * @see guiPractice.GUIApplication#initScreen()
     */
    @Override
    protected void initScreen() {
        initColors();


        miniGameScreen = new GameScreen(getWidth(),getHeight());
        shopMain = new HomeShopScreen(getWidth(),getHeight());
        sellScreen = new SellShopZheng(getWidth(),getHeight());
        homeScreen = new HomeScreen(getWidth(),getHeight());
        buyScreen = new BuyScreenWendy(getWidth(),getHeight());
        highscoreScreen = new HighScoreScreen(getWidth(),getHeight());
        HomeScreen.jenCode = new game.mainScreenTeam.HomeJenniber();
        gameInstructionsScreen = new GameInstructions(getWidth(), getHeight());

        setScreen(homeScreen);


    }
    private void initColors() {
        NAVY = new Color(62,74,99);
        BRIGHT_PINK = new Color(224,102,102);
        LIGHT_PINK = new Color(248,186,182);
        LIGHT_NUDE = new Color(244,215,183);
        DARKER_NUDE = new Color(230,195,147);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        game = new DragonLand();
        Thread go = new Thread(game);
        go.start();
    }

    //public coin getter + setter
        public void setCoins(int x){
            coins = x;
        }
        public int getCoins(){
            return coins;
        }

}

这是主屏幕

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import game.DragonLand;
import guiPractice.ClickableScreen;
import guiPractice.components.Action;
import guiPractice.components.AnimatedComponent;
import guiPractice.components.Button;
import guiPractice.components.Graphic;
import guiPractice.components.TextLabel;
import guiPractice.components.Visible;
import guiPractice.sampleGames.MouseFollower;

/**
 * @author Kat 
 * @author Jenniber
 *
 */
public class HomeScreen extends ClickableScreen implements Runnable{

    private Graphic background;
    public static HomeJenniber jenCode;

    public HomeScreen(int width, int height) {
        super(width, height);
        Thread play = new Thread(this);
        play.start();

    }

    @Override
    public void initAllObjects(ArrayList<Visible> viewObjects) {

        background=new Graphic(0,0,getWidth(),getHeight(),"img/Grassland.png");
        viewObjects.add(background);
        HomeKat katCode=new HomeKat(viewObjects, getWidth(), getHeight());

    }


    @Override
    public void run() {
    }

}

katCode在屏幕上添加按钮和图像动画

1 个答案:

答案 0 :(得分:2)

public void paint(Graphics g){
    g.drawImage(getCurrentScreen().getImage(), 0, 0, null);
}

不要覆盖JFrame上的paint()。

执行自定义绘制的正确方法是覆盖paintComponent(...)(或JComponent)上的JPanel,然后您可以将框架的内容窗格设置为此面板。并且不要忘记调用super.paintComponent(...)作为方法中的第一个语句。阅读Custom Painting上Swing教程中的部分,了解更多信息和工作示例。

但是如果你确实变懒,那么至少你需要调用super.paint(...)作为paint(...)方法中的第一个语句。

另外,我怀疑你需要revalidate(),因为你似乎没有从帧中添加/删除组件。

但总的来说,顺序应该是:

revalidate(); // to invoke the layout manager
repaint(); // paint components in new location.

我也不知道你为什么要调用update()方法。这似乎是你在Swing中不使用的旧AWT代码。我建议你看看我给你的教程链接,看看其他Swing基础知识的目录。