问题描述
我伸出的画布
并将其添加到的JFrame
。据我所知,AWT和Swing不应混为一谈,并且借鉴的JPanel是preferred但我试图遵循一个游戏引擎教程,我想坚持下去,因为我得到了它迄今为止的工作。在画布
的的minimumSize
, MAXIMUMSIZE
和 prefferedSize
设置为 550,400 。当我做一个绘制调用 graphics.draw(0,0,550,400)
不填充整个屏幕,因为它应该。我改变了抽奖调用 graphics.draw(0,0,560,410)
本质添加10px的它和它充满了整个屏幕。什么错?
BTW: graphics.draw(10,10,550,400
开始绘制在拐角处正是因此,我不认为在JFrame是问题的矩形
在启动主通话
类
公共静态无效的主要(字串[] args){
SwingUtilities.invokeLater(Runnable的新(){
@覆盖
公共无效的run(){
game.setMinimumSize(尺寸);
game.setMaximumSize(尺寸);
game.set preferredSize(尺寸); game.frame =新的JFrame(名);
game.frame.setLayout(新的BorderLayout());
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.frame.add(游戏,BorderLayout.CENTER);
game.frame.pack(); game.frame.setResizable(假);
game.frame.setLocationRelativeTo(NULL);
game.frame.setVisible(真); Logger.log(TAG,游戏开始);
游戏开始();
}
});
}
绘图调用, Launcher.HEIGHT
和宽度
是 550,400
公共无效战平(浮动deltaTime,Graphics2D的图形){
graphics.setColor(Color.BLACK);
graphics.fillRect(0,0,550,400);
graphics.setColor(Color.DARK_GRAY);
graphics.fillRect(0,0,150,40);
graphics.fillRect(0,Launcher.HEIGHT - 100,Launcher.WIDTH,100);
graphics.setColor(Color.LIGHT_GRAY);
graphics.fillRect(125,Launcher.HEIGHT - 100,100,350);
}
扩展画布
类
公共抽象类游戏扩展画布实现Runnable {
私有静态最后弦乐TAG =游戏;公众的JFrame框架;
公众的JPanel面板;
公共布尔isApplet = FALSE;私人布尔gameRunning = FALSE;BufferStrategy中BufferStrategy中;私人屏幕屏幕;
私人螺纹renderThread;公共同步启动无效(){
//帆布
的setBounds(0,0,550,400);
setIgnoreRepaint(真);
createBufferStrategy(2);
BufferStrategy中= getBufferStrategy(); //屏幕,处理器,ETC
屏幕= getStartScreen(); //线程
renderThread =新主题(这一点,Launcher.NAME +_main);
renderThread.start();
gameRunning = TRUE;
}@覆盖
公共无效的run(){
长STARTTIME = System.nanoTime(); 而(gameRunning){
浮动deltaTime =(System.nanoTime() - startTime时)/ 1000000000.0f;
STARTTIME = System.nanoTime(); screen.update(deltaTime); Graphics2D的图形=(Graphics2D的)bufferStrategy.getDrawGraphics(); screen.draw(deltaTime,图形); graphics.dispose();
bufferStrategy.show(); // FPS计数器 // FPS旋盖机
}
}
}
Reqested SSCCE
进口java.awt.BorderLayout中;
进口java.awt.Canvas中;
进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.Graphics2D中;
进口java.awt.image.BufferStrategy;进口javax.swing.JFrame中;
进口javax.swing.SwingUtilities中;@燮pressWarnings(串行)
公共类游戏扩展画布实现Runnable { 公共静态最终诠释WIDTH = 550;
公共静态最终诠释HEIGHT = 400;
公共静态最终尺寸尺寸=新的外形尺寸(宽,高); 公共静态最后弦乐NAME =SSCCE; 公共布尔gameRunning = FALSE; 公众的JFrame框架;
公共BufferStrategy中BufferStrategy中; 公共静态无效的主要(字串[] args){
SwingUtilities.invokeLater(Runnable的新(){
@覆盖
公共无效的run(){
游戏游戏=新游戏();
game.setMinimumSize(尺寸);
game.setMaximumSize(尺寸);
game.set preferredSize(尺寸); game.frame =新的JFrame(名);
game.frame.setLayout(新的BorderLayout());
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.frame.add(游戏,BorderLayout.CENTER);
game.frame.pack(); game.frame.setResizable(假);
game.frame.setLocationRelativeTo(NULL);
game.frame.setVisible(真); 的System.out.println(游戏开始了);
游戏开始();
}
});
} 公共同步启动无效(){
的setSize(550,400);
的setBounds(0,0,550,400);
setIgnoreRepaint(真);
createBufferStrategy(2);
BufferStrategy中= getBufferStrategy(); //线程
螺纹renderThread =新主题(这一点,名称+_main);
renderThread.start();
gameRunning = TRUE;
} @覆盖
公共无效的run(){
而(gameRunning){
Graphics2D的图形=(Graphics2D的)bufferStrategy.getDrawGraphics(); graphics.setColor(Color.BLACK);
graphics.fillRect(0,0,宽度,高度); graphics.dispose();
bufferStrategy.show();
}
}}
至于在Java 2D游戏教程中,我跟随它是一个Web系列。尽管如此,链接如下。我修改了code颇多。
修改为MadProgrammer
进口java.awt.BorderLayout中;
进口java.awt.Canvas中;
进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.Graphics2D中;
进口java.awt.image.BufferStrategy;进口javax.swing.JFrame中;
进口javax.swing.SwingUtilities中;@燮pressWarnings(串行)
公共类游戏扩展画布实现Runnable { 公共静态最终诠释WIDTH = 550;
公共静态最终诠释HEIGHT = 400;
公共静态最终尺寸尺寸=新的外形尺寸(宽,高); 公共静态最后弦乐NAME =SSCCE; 公共布尔gameRunning = FALSE; 公众的JFrame框架;
公共BufferStrategy中BufferStrategy中; 公共静态无效的主要(字串[] args){
SwingUtilities.invokeLater(Runnable的新(){
@覆盖
公共无效的run(){
游戏游戏=新游戏();
game.setMinimumSize(尺寸);
game.setMaximumSize(尺寸);
game.set preferredSize(尺寸); game.frame =新的JFrame(名);
game.frame.setLayout(新的BorderLayout());
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.frame.add(游戏,BorderLayout.CENTER);
game.frame.pack(); game.frame.setResizable(假);
game.frame.setLocationRelativeTo(NULL);
game.frame.setVisible(真); 的System.out.println(游戏开始了);
游戏开始();
}
});
} 公共同步启动无效(){
的setSize(550,400);
的setBounds(0,0,550,400);
setIgnoreRepaint(真);
createBufferStrategy(2);
BufferStrategy中= getBufferStrategy(); //线程
螺纹renderThread =新主题(这一点,名称+_main);
renderThread.start();
gameRunning = TRUE;
} @覆盖
公共无效的run(){
而(gameRunning){
Graphics2D的图形=(Graphics2D的)bufferStrategy.getDrawGraphics(); graphics.setColor(Color.RED);
//使用的getWidth()
graphics.fillRect(0,0,的getWidth(),的getHeight());
graphics.setColor(Color.GREEN);
//使用这是用来设置画布的大小WIDTH
graphics.fillRect(5,5,宽度,高度); graphics.dispose();
bufferStrategy.show(); 尝试{
视频下载(60);
}赶上(InterruptedException的前){
}
}
}}
您已经提供了优秀作品的例子。我修改了它一点点地演示了如何使用的getWidth /身高
公共无效的run(){
而(gameRunning){
Graphics2D的图形=(Graphics2D的)bufferStrategy.getDrawGraphics(); graphics.setColor(Color.RED);
graphics.fillRect(0,0,的getWidth(),的getHeight());
graphics.setColor(Color.GREEN);
INT宽度=的getWidth() - 50;
INT高度=的getHeight() - 50;
graphics.fillRect(25,25,宽度,高度);
graphics.setColor(Color.BLACK);
FontMetrics对象FM = graphics.getFontMetrics();
graphics.drawString(外形尺寸为:+ frame.getWidth()+X+ frame.getHeight(),0,fm.getAscent());
graphics.drawString(画布大小:+的getWidth()+×+的getHeight(),0,fm.getAscent()+ fm.getHeight()); graphics.dispose();
bufferStrategy.show();
尝试{
视频下载(60);
}赶上(InterruptedException的前){
}
}
}
I am extending a Canvas
and adding it to a JFrame
. I understand that AWT and Swing should not be mixed and that drawing on JPanel is preferred but i'm trying to follow a game engine tutorial and i'd like to stick to it since I got it working so far. The Canvas
has minimumSize
, maximumSize
, and prefferedSize
set to the dimensions of 550, 400
. When I do a draw call graphics.draw(0,0,550,400)
it doesn't fill the entire screen as it should. I changed the draw call to graphics.draw(0,0,560,410)
essentially adding 10px to it and it filled the entire screen. Whats wrong?
BTW: graphics.draw(10,10,550,400
draws the rectangle starting at the corner exactly so I don't think the JFrame is the issue.
The main call inside Launcher
class
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
game.setMinimumSize(DIMENSIONS);
game.setMaximumSize(DIMENSIONS);
game.setPreferredSize(DIMENSIONS);
game.frame = new JFrame(NAME);
game.frame.setLayout(new BorderLayout());
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.add(game, BorderLayout.CENTER);
game.frame.pack();
game.frame.setResizable(false);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
Logger.log(TAG, "Game starting");
game.start();
}
});
}
The draw call, Launcher.HEIGHT
and WIDTH
are 550,400
public void draw(float deltaTime, Graphics2D graphics) {
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, 550, 400);
graphics.setColor(Color.DARK_GRAY);
graphics.fillRect(0, 0, 150, 40);
graphics.fillRect(0, Launcher.HEIGHT - 100, Launcher.WIDTH, 100);
graphics.setColor(Color.LIGHT_GRAY);
graphics.fillRect(125, Launcher.HEIGHT - 100, 100, 350);
}
The extended Canvas
class
public abstract class Game extends Canvas implements Runnable {
private static final String TAG = "Game";
public JFrame frame;
public JPanel panel;
public boolean isApplet = false;
private boolean gameRunning = false;
BufferStrategy bufferStrategy;
private Screen screen;
private Thread renderThread;
public synchronized void start() {
// Canvas
setBounds(0, 0, 550, 400);
setIgnoreRepaint(true);
createBufferStrategy(2);
bufferStrategy = getBufferStrategy();
// Screen, Handlers, ETC
screen = getStartScreen();
// Threads
renderThread = new Thread(this, Launcher.NAME + "_main");
renderThread.start();
gameRunning = true;
}
@Override
public void run() {
long startTime = System.nanoTime();
while (gameRunning) {
float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
startTime = System.nanoTime();
screen.update(deltaTime);
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
screen.draw(deltaTime, graphics);
graphics.dispose();
bufferStrategy.show();
// FPS Counter
// FPS Capper
}
}
}
Reqested SSCCE
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class Game extends Canvas implements Runnable {
public static final int WIDTH = 550;
public static final int HEIGHT = 400;
public static final Dimension DIMENSIONS = new Dimension(WIDTH, HEIGHT);
public static final String NAME = "SSCCE";
public boolean gameRunning = false;
public JFrame frame;
public BufferStrategy bufferStrategy;
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Game game = new Game();
game.setMinimumSize(DIMENSIONS);
game.setMaximumSize(DIMENSIONS);
game.setPreferredSize(DIMENSIONS);
game.frame = new JFrame(NAME);
game.frame.setLayout(new BorderLayout());
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.add(game, BorderLayout.CENTER);
game.frame.pack();
game.frame.setResizable(false);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
System.out.println("Game started");
game.start();
}
});
}
public synchronized void start() {
setSize(550, 400);
setBounds(0, 0, 550, 400);
setIgnoreRepaint(true);
createBufferStrategy(2);
bufferStrategy = getBufferStrategy();
// Threads
Thread renderThread = new Thread(this, NAME + "_main");
renderThread.start();
gameRunning = true;
}
@Override
public void run() {
while (gameRunning) {
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, WIDTH, HEIGHT);
graphics.dispose();
bufferStrategy.show();
}
}
}
As for the Java 2D game tutorial I am following it is a web series. Nevertheless, the link is the following click here. I modified the code quite a lot.
Modified for MadProgrammer
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class Game extends Canvas implements Runnable {
public static final int WIDTH = 550;
public static final int HEIGHT = 400;
public static final Dimension DIMENSIONS = new Dimension(WIDTH, HEIGHT);
public static final String NAME = "SSCCE";
public boolean gameRunning = false;
public JFrame frame;
public BufferStrategy bufferStrategy;
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Game game = new Game();
game.setMinimumSize(DIMENSIONS);
game.setMaximumSize(DIMENSIONS);
game.setPreferredSize(DIMENSIONS);
game.frame = new JFrame(NAME);
game.frame.setLayout(new BorderLayout());
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.add(game, BorderLayout.CENTER);
game.frame.pack();
game.frame.setResizable(false);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
System.out.println("Game started");
game.start();
}
});
}
public synchronized void start() {
setSize(550, 400);
setBounds(0, 0, 550, 400);
setIgnoreRepaint(true);
createBufferStrategy(2);
bufferStrategy = getBufferStrategy();
// Threads
Thread renderThread = new Thread(this, NAME + "_main");
renderThread.start();
gameRunning = true;
}
@Override
public void run() {
while (gameRunning) {
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
graphics.setColor(Color.RED);
// Using getWidth()
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(Color.GREEN);
// Using WIDTH which was used to set the size of the canvas
graphics.fillRect(5, 5, WIDTH, HEIGHT);
graphics.dispose();
bufferStrategy.show();
try {
Thread.sleep(60);
} catch (InterruptedException ex) {
}
}
}
}
The example you've provide works fine. I modified it a little to demonstrate the use of getWidth/height
public void run() {
while (gameRunning) {
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
graphics.setColor(Color.RED);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(Color.GREEN);
int width = getWidth() - 50;
int height = getHeight() - 50;
graphics.fillRect(25, 25, width, height);
graphics.setColor(Color.BLACK);
FontMetrics fm = graphics.getFontMetrics();
graphics.drawString("Frame Size: " + frame.getWidth() + "x" + frame.getHeight(), 0, fm.getAscent());
graphics.drawString("Canvas Size: " + getWidth() + "x" + getHeight(), 0, fm.getAscent() + fm.getHeight());
graphics.dispose();
bufferStrategy.show();
try {
Thread.sleep(60);
} catch (InterruptedException ex) {
}
}
}
这篇关于帆布fillRect()不灌装定义画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!