I have been following a video tutorial and need some questions answered about some of the things he types youtu.be/VE7ezYCTPe4?t=10m
Starting at 10:00 in the video he mentions that this BorderLayout is not something we need to know about. I am now getting errors with this code and I am wondering EXACTLY what this does and what perimeters I am not meeting.
In the tutorial he names his frame NAME but I chose a name like he mentioned we could. Here is my code and I get a red line under BorderLayout (I marker this in bold)
frame = new JFrame("D&A");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setLayout(new BorderLayout());
frame.add(this.BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);;
frame.setVisible(true);
Can anyone help? I have tried Ctrl + Shift + O and imported everything I have been told to.
[My Full Code So Far]
package ca.vanzeben.game;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH/12*9;
public static final int SCALE = 3;
public static final String NAME = "D&A";
private JFrame frame;
public boolean running = true;
public int tickCount = 0;
private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
public game() {
setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
frame = new JFrame("D&A");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setLayout(new BorderLayout());
frame.add(this.BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);;
frame.setVisible(true);
}
public synchronized void start() {
new Thread(this).start();
running = true;
}
public synchronized void stop() {
running = false;
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D/60D;
int frames = 0;
int ticks = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0; //How many nanoseconds have gone by so far
while(running) {
long now = System.nanoTime();
delta += (now - lastTime)/ nsPerTick;
lastTime = now;
boolean shouldRender = true;
while(delta >= 1) {
ticks++;
tick();
delta +=1;
shouldRender = true;
}
try{
Thread.sleep(2);
}
catch (InterruptedException e) {
e.printStackTrace();
}
if (shouldRender) {
frames++;
render();
}
if (System.currentTimeMillis() - lastTime >= 1000) {
lastTime += 1000;
frames = 0;
ticks = 0;
}
}
}
public void tick() {
tickCount++;
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}
public static void main(String[] args) {
new game().start();
}
}