图形对象不显示到屏幕

时间:2015-11-27 22:46:21

标签: java swing enums jpanel

所以答案可能是完全明显的,但我无法看到它。为什么我的Graphics对象没有显示我告诉它的内容?我非常肯定我会尊重Swing的并发性,但也许并非如此。这是我的JPanel代码:

<table class="table" cellspacing="0">
  <thead>
    <tr>
      <th>Data</th>
      <th>Titolo</th>
      <th>Sede</th>
      <th>Città</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a href="eventi.php?id=17">12/02/2015</a>
      </td>
      <td><a href="eventi.php?id=17"><span class="fixed glyphicon glyphicon-flag"></span>asd</a>
      </td>
      <td><a href="eventi.php?id=17">ada</a>
      </td>
      <td><a href="eventi.php?id=17">asdas</a>
      </td>
    </tr>
    <tr>
      <td><a href="eventi.php?id=13">04/02/2015</a>
      </td>
      <td><a href="eventi.php?id=13"><span class="fixed glyphicon glyphicon-flag"></span>ada</a>
      </td>
      <td><a href="eventi.php?id=13">prova</a>
      </td>
      <td><a href="eventi.php?id=13">asda</a>
      </td>
    </tr>
    <tr>
      <td><a href="eventi.php?id=12">07/09/2017</a>
      </td>
      <td><a href="eventi.php?id=12">Evento Lignano <br>
  sabbiadoro</a>
      </td>
      <td><a href="eventi.php?id=12">Palazzetto dello Sport</a>
      </td>
      <td><a href="eventi.php?id=12">Perugia</a>
      </td>
    </tr>
    <tr>
      <td><a href="eventi.php?id=54">09/09/2015</a>
      </td>
      <td><a href="eventi.php?id=54">aaa</a>
      </td>
      <td><a href="eventi.php?id=54">aaa</a>
      </td>
      <td><a href="eventi.php?id=54">aaa</a>
      </td>
    </tr>
    <tr>
      <td><a href="eventi.php?id=25">09/03/2015</a>
      </td>
      <td><a href="eventi.php?id=25">sfsd</a>
      </td>
      <td><a href="eventi.php?id=25">ada</a>
      </td>
      <td><a href="eventi.php?id=25">dadasd</a>
      </td>
    </tr>
  </tbody>
</table>

这是我的场景枚举:

package com.kraken.towerdefense.graphics;

import com.kraken.towerdefense.TowerDefense;
import com.kraken.towerdefense.listener.KeyHandler;
import com.kraken.towerdefense.listener.MouseMotionHandler;
import com.kraken.towerdefense.scene.Scene;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;

public class Screen extends JPanel implements Runnable {

    Thread thread = new Thread(this);

    private int FPS = 0;

    public Scene scene;

    TowerDefense tD;

    private boolean running = false;

    public RoundRectangle2D.Float playGame, quitGame;
    public boolean playGameHighlighted, quitGameHighlighted;

    @Override
    public void run() {
        long lastFrame = System.currentTimeMillis();
        int frames = 0;

        running = true;

        while (running) {
            repaint();

            frames++;

            if (System.currentTimeMillis() - 1000 >= lastFrame) {
                FPS = frames;
                frames = 0;

                lastFrame = System.currentTimeMillis();
            }
        }

        System.exit(0);
    }

    public Screen(TowerDefense tD) {
        thread.start();

        addKeyListener(new KeyHandler(this));
        addMouseMotionListener(new MouseMotionHandler(this));

        this.tD = tD;
        scene = Scene.MENU;
    }

    @Override
    public void paintComponent(Graphics g2) {
        super.paintComponent(g2);

        playGame = new RoundRectangle2D.Float((getWidth() / 2) - 200, (getHeight() / 2) - 100, 400, 100, 10, 10);
        quitGame = new RoundRectangle2D.Float((getWidth() / 2) - 200, (getHeight() / 2) + 20, 400, 100, 10, 10);

        Graphics2D g = (Graphics2D) g2.create();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.clearRect(0, 0, getWidth(), getHeight());

        g.drawString("FPS: " + FPS, 10, 10);

        if (scene == Scene.MENU) {
            if (playGameHighlighted) {
                g.setColor(new Color(255, 152, 56));
            } else {
                g.setColor(new Color(4, 47, 61));
            }
            g.fill(playGame);

            if (quitGameHighlighted) {
                g.setColor(new Color(255, 152, 56));
            } else {
                g.setColor(new Color(4, 47, 61));
            }
            g.fill(quitGame);

            g.setColor(Color.WHITE);
            g.setFont(new Font("Gisha", Font.PLAIN, 20));
            g.drawString("Play", (getWidth() / 2) - (g.getFontMetrics().stringWidth("Play") / 2), (getHeight() / 2) - 45);
            g.drawString("Quit", (getWidth() / 2) - (g.getFontMetrics().stringWidth("Quit") / 2), (getHeight() / 2) + 75);
        }
    }

    public class KeyTyped {
        public void keyESC() {
            running = false;
        }
    }
}

我非常确定我不需要提供JFrame代码,但如果有必要,我会。我可以给你的代码中任何其他问题的解决方案非常感谢。谢谢!

编辑1

这是我的MouseMotionListener类:

package com.kraken.towerdefense.scene;

public enum Scene {
    MENU,
    GAME
}

这是我的JFrame代码:

package com.kraken.towerdefense.listener;

import com.kraken.towerdefense.graphics.Screen;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class MouseMotionHandler implements MouseMotionListener {

    Screen screen;

    public MouseMotionHandler(Screen screen) {
        this.screen = screen;
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if (screen.playGame.contains(e.getPoint())) {
            screen.playGameHighlighted = true;
        } else {
            screen.playGameHighlighted = false;
        }

        if (screen.quitGame.contains(e.getPoint())) {
            screen.quitGameHighlighted = true;
        } else {
            screen.playGameHighlighted = false;
        }
    }
}

这是我的KeyListener代码:

package com.kraken.towerdefense;

import com.kraken.towerdefense.graphics.Screen;

import javax.swing.*;

public class TowerDefense extends JFrame {

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

    public TowerDefense() {
        setExtendedState(MAXIMIZED_BOTH);
        setUndecorated(true);
        setTitle("Tower Defense");

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setResizable(false);

        Screen screen = new Screen(this);
        this.add(screen);

        setVisible(true);
    }

}

所以这些都是我的课程,我希望有帮助

1 个答案:

答案 0 :(得分:3)

所以,我掏空你的代码让它运行并能够显示......

Display

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GraphicsTest {

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

    public GraphicsTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Screen());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public enum Scene {
        MENU,
        GAME
    }

    public class Screen extends JPanel implements Runnable {

        Thread thread = new Thread(this);

        private int FPS = 0;

        public Scene scene;

        private boolean running = false;

        @Override
        public void run() {
            long lastFrame = System.currentTimeMillis();
            int frames = 0;

            running = true;
            scene = Scene.MENU;

            while (running) {
                repaint();

                frames++;

                if (System.currentTimeMillis() - 1000 >= lastFrame) {
                    FPS = frames;
                    frames = 0;

                    lastFrame = System.currentTimeMillis();
                }
            }

            System.exit(0);
        }

        public Screen() {
            thread.start();
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.clearRect(0, 0, getWidth(), getHeight());

            g.drawString("FPS: " + FPS, 10, 10);

            if (scene == Scene.MENU) {
                g.setColor(Color.BLACK);

                g.fillRoundRect((getWidth() / 2) - 100, (getHeight() / 2) - 50, 200, 100, 25, 25);
            }
        }

    }
}

因此,这表明您所描述的问题是其他问题。

对我而言;

g.clearRect(0, 0, tD.getWidth(), tD.getHeight());

看起来很可疑,因为当你依赖于组件自己的宽度/高度属性时,依赖于TowerDefense属性(除了在这种情况下实际上不需要clearRect)。

这进一步让我怀疑你实际上并没有将Screen组件添加到任何可显示的组件

另一个可能的问题是您正在使用适当的布局管理器,但由于您的Screen课程没有提供任何preferredSize提示,这将是您需要的另一个问题没有展示

根据对原始问题的更改进行了更新

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import static java.awt.Frame.MAXIMIZED_BOTH;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import javafx.scene.Scene;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;

public class TowerDefense extends JFrame {

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

    public TowerDefense() {
//        setExtendedState(MAXIMIZED_BOTH);
//        setUndecorated(true);
        setTitle("Tower Defense");

        setDefaultCloseOperation(EXIT_ON_CLOSE);

//        setResizable(false);

        Screen screen = new Screen(this);
        this.add(screen);
        pack();

        setVisible(true);
    }

    public enum Scene {
        MENU,
        GAME
    }

    public class Screen extends JPanel implements Runnable {

        Thread thread = new Thread(this);

        private int FPS = 0;

        public Scene scene;

        TowerDefense tD;

        private boolean running = false;

        public RoundRectangle2D.Float playGame, quitGame;
        public boolean playGameHighlighted, quitGameHighlighted;

        @Override
        public void run() {
//            long lastFrame = System.currentTimeMillis();
//            int frames = 0;
//
//            running = true;
//
//            while (running) {
//                repaint();
//
//                frames++;
//
//                if (System.currentTimeMillis() - 1000 >= lastFrame) {
//                    FPS = frames;
//                    frames = 0;
//
//                    lastFrame = System.currentTimeMillis();
//                }
//            }
//
//            System.exit(0);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        public Screen(TowerDefense tD) {
            thread.start();

            addKeyListener(new KeyHandler(this));
            addMouseMotionListener(
                    new MouseAdapter() {

                @Override
                public void mouseMoved(MouseEvent e) {
                    playGameHighlighted = playGame.contains(e.getPoint());
                    quitGameHighlighted = quitGame.contains(e.getPoint());
                    repaint();
                }
            });

            this.tD = tD;
            scene = Scene.MENU;

        }

        @Override
        public void paintComponent(Graphics g2) {
            super.paintComponent(g2);

            playGame = new RoundRectangle2D.Float((getWidth() / 2) - 200, (getHeight() / 2) - 100, 400, 100, 10, 10);
            quitGame = new RoundRectangle2D.Float((getWidth() / 2) - 200, (getHeight() / 2) + 20, 400, 100, 10, 10);

            Graphics2D g = (Graphics2D) g2.create();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            g.clearRect(0, 0, getWidth(), getHeight());

            g.drawString("FPS: " + FPS, 10, 10);

            if (scene == Scene.MENU) {
                if (playGameHighlighted) {
                    g.setColor(new Color(255, 152, 56));
                } else {
                    g.setColor(new Color(4, 47, 61));
                }
                g.draw(playGame);

                if (quitGameHighlighted) {
                    g.setColor(new Color(255, 152, 56));
                } else {
                    g.setColor(new Color(4, 47, 61));
                }
                g.draw(quitGame);

                g.setColor(Color.WHITE);
                g.setFont(new Font("Gisha", Font.PLAIN, 20));
                g.drawString("Play", (getWidth() / 2) - (g.getFontMetrics().stringWidth("Play") / 2), (getHeight() / 2) - 45);
                g.drawString("Quit", (getWidth() / 2) - (g.getFontMetrics().stringWidth("Quit") / 2), (getHeight() / 2) + 75);
            }
        }

        public class KeyTyped {

            public void keyESC() {
                running = false;
            }
        }

    }

    public class KeyHandler implements KeyListener {

        private Screen screen;
        private Screen.KeyTyped keyTyped;

        public KeyHandler(Screen screen) {
            this.screen = screen;

            keyTyped = screen.new KeyTyped();
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == 27) {
                keyTyped.keyESC();
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {

        }

    }
}