Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Java Full Screen Exclusive

Silicon MunkySilicon Munky Member Posts: 1
Ok, well I've been playing around with Java 1.4's new full screen exclusive graphics mode. Has anyone else tried it out? Cause I've run into some problem and I don't know what's causing it. The problem is that the program will just stop after a certain period of time. The time isn't really fixed either. I've also tested to see how many times the while loop runs and that is not fixed either. Can someone take a look and see if I'm over looking anything.

Here's the code.

//Code written by Peter Andrews(siliconmunky@shaw.ca)
//Some code extracted from a Sun Java Tutorial

import java.awt.*;
import java.awt.image.BufferStrategy;

public class MultiBufferTest {
private static DisplayMode[] BEST_DISPLAY_MODES = new DisplayMode[] {
new DisplayMode(1280, 1024, 32, 60),
new DisplayMode(1024, 768, 16, 0),
new DisplayMode(640, 480, 16, 0)
};

//this will return the first display mode in the array
private static DisplayMode getBestDisplayMode(GraphicsDevice device) {
for (int x = 0; x < BEST_DISPLAY_MODES.length; x++) {
DisplayMode[] modes = device.getDisplayModes();
for (int i = 0; i < modes.length; i++) {
if (modes[i].getWidth() == BEST_DISPLAY_MODES[x].getWidth()
&& modes[i].getHeight() == BEST_DISPLAY_MODES[x].getHeight()
&& modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth()
) {
return BEST_DISPLAY_MODES[x];
}
}
}
return null;
}

public static void chooseBestDisplayMode(GraphicsDevice device) {
DisplayMode best = getBestDisplayMode(device);
if (best != null) {
device.setDisplayMode(best);
}
}

Frame mainFrame;
public MultiBufferTest(int numBuffers, GraphicsDevice device) {
try {
GraphicsConfiguration gc = device.getDefaultConfiguration();
mainFrame = new Frame(gc);
mainFrame.setUndecorated(true);
mainFrame.setIgnoreRepaint(true);
device.setFullScreenWindow(mainFrame);
if (device.isDisplayChangeSupported()) {

//chooseBestDisplayMode(device);
}
Rectangle bounds = mainFrame.getBounds();//?
mainFrame.createBufferStrategy(numBuffers);
BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();


boolean done = false;
Lady[] testObject = new Lady[5];
for(int i = 0; i < testObject.length; i++){
testObject[i] = new Lady();
}

System.out.print("StartTime: ");
System.out.println(System.currentTimeMillis());
double startTime = System.currentTimeMillis();
double endTime = 0;
int timesLooped = 0;
while(!done){
Graphics g = bufferStrategy.getDrawGraphics();
if (!bufferStrategy.contentsLost()) {

g.setColor(Color.black);
g.fillRect(0,0,bounds.width, bounds.height);
for(int i = 0; i < testObject.length; i++){
testObject[i].render(g);
testObject[i].moveLady(bounds.width, bounds.height);
}
bufferStrategy.show();
g.dispose();
}
endTime = System.currentTimeMillis() - startTime;
timesLooped++;
System.out.print("End Time: " + System.currentTimeMillis()+ " ");
System.out.println("Total Time: " + endTime);
System.out.println("The number of times the loop gets iterated: " + timesLooped + "
");

}
System.out.println("Exited the while loop");//this never gets printed????
} catch (Exception e) {
e.printStackTrace();
} finally {
device.setFullScreenWindow(null);
}
}



public static void main(String[] args) {
try {
int numBuffers = 3; //sets the number of back buffers to 3

GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
MultiBufferTest test = new MultiBufferTest(numBuffers, device);
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
}


//Class Lady, used to draw to the screen.
class Lady{
int x = 0;
int y = 0;
int run = (int)(Math.random() * 25 + 1);
int rise = (int)(Math.random() * 25 + 1);

Lady(){
}

Lady(int newX, int newY){
x = newX;
y = newY;
}
public void moveLady(int xBound, int yBound){
if((x + run + 50 > xBound) || (x + run < 0)){
run = run * -1;
}
x = x + run;
if((y + rise + 200 > yBound) || (y + rise < 0)){
rise = rise * -1;
}
y = y + rise;
}
public void render(Graphics g){
g.setColor(Color.green);
g.fillOval(x, y, 50, 50);//head
g.drawLine(x + 25, y + 50, x + 25, y + 150);//body
g.drawLine(x, y + 75, x + 50, y + 75);//arms
g.drawArc(x, y + 150, 50, 75, 0, 180);//legs
}
}

Comments

  • Justin BibJustin Bib USAMember Posts: 0

    ___ { http://forcoder.org } free video tutorials and ebooks about < Go, PL/SQL, MATLAB, Visual Basic, R, PHP, C++, Swift, Java, Python, Delphi, Perl, C, Ruby, Assembly, Visual Basic .NET, C#, Objective-C, JavaScript, Scratch Scala, Bash, COBOL, D, Hack, Lisp, SAS, Apex, Rust, Erlang, LabVIEW, Transact-SQL, Kotlin, Ada, ABAP, Logo, Alice, Fortran, Julia, Scheme, VBScript, ML, Dart, Clojure, Crystal, Lua, Prolog, Awk, F#, FoxPro /> _

Sign In or Register to comment.