-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.java
More file actions
56 lines (49 loc) · 2.47 KB
/
Display.java
File metadata and controls
56 lines (49 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.BufferStrategy;
import java.awt.image.DataBufferByte;
import javax.swing.*;
public class Display extends Canvas{
public static JFrame frame;
private BufferedImage displayImage;
private RenderContext frameBuffer;
private byte[] displayComponents;
public BufferStrategy bufferStrategy;
private Graphics graphics;
public RenderContext GetFrameBuffer() { return frameBuffer; }
public Display(int width, int height, String title){
try{
Dimension size = new Dimension(width, height);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size); //set min and max size in code so that way window is not accidentally resized ever
frameBuffer = new RenderContext(width, height);
displayImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
displayComponents = ((DataBufferByte)displayImage.getRaster().getDataBuffer()).getData(); //why in the actual heck is it so difficult to access the bytes of the image
frameBuffer.Clear((byte)0x80); //set screen to grey
frame = new JFrame();
frame.add(this);
frame.pack(); //make not resizable
frame.setResizable(false); //make it not resizable because of 3d graphic stuff
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); //set frame in center of screen
frame.setTitle(title);
frame.setVisible(true);
frame.setSize(800, 600);
createBufferStrategy(1);
bufferStrategy = getBufferStrategy();
graphics = bufferStrategy.getDrawGraphics();
}catch (Exception e){
JOptionPane.showMessageDialog(Display.frame, "An error has occurred within starting the KobiWare Engine.\nLine " +e.getStackTrace()[0].getLineNumber() + ": " + e);
System.out.println("An error has occurred within starting the KobiWare Engine at line " + e.getStackTrace()[0].getLineNumber() + ", " + e);
System.exit(0);
}
}
public void SwapBuffers() {
frameBuffer.CopyToByteArray(displayComponents); //copy the bit map into display components
graphics.drawImage(displayImage, 0, 0, frameBuffer.GetWidth(), frameBuffer.GetHeight(), null);
bufferStrategy.show();
}
}