Screen went Blank after switching the version of OpenGL - java

I drew a white triangle in the screen(WIDTH=800,HEIGHT=600). I got the white screen sized triangle, working perfectly fine with OpenGl version 4.2.0 - Build 10.18.10.3496 (current version)
but when I switched to 3.3.0 - Build 10.18.10.3496
I got nothing but black blank screen. When I again went for 4.2.0, everything is just working fine.
I have no idea what's going on?
public abstract class Window {
public static void createNormalVersion(int width,int height,String title){
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle(title);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
}
public static void createSpecifiedVersion
(int width,int height,String title,int firstVer,int secondVer){
try{
PixelFormat pixel=new PixelFormat();
ContextAttribs context=new ContextAttribs(firstVer,secondVer)
.withForwardCompatible(true)
.withProfileCore(true);
Display.setDisplayMode(new DisplayMode(width,height));
Display.setTitle(title);
Display.create(pixel,context);
}catch(LWJGLException e){
e.printStackTrace();
}
}
// I choose the version from main method
public static void main(String[] args) {
Window.createSpecifiedVersion(WIDTH, HEIGHT, TITLE, 3, 3);
MainComponent game = new MainComponent();
game.start();
}

Related

Using jWindow as splash screen which shows good but image doesn't paint ... just blank jWindow opened...any thoughts?

jWindow opened for 2 seconds but image doesn't paint... any thoughts?
image file is in the same folder as class file...
public class CreateSplashScreen extends JWindow {
JWindow jw = new JWindow();
Image scImage = Toolkit.getDefaultToolkit().getImage("testImage.png");
ImageIcon imageIcon = new ImageIcon(scImage);
public CreateSplashScreen() {
try {
jw.setSize(700, 500);
jw.setLocationRelativeTo(null);
jw.setVisible(true);
} catch (Exception e) {
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(scImage, 0, 0, jw);
}
public void CloseSplashScreen() {
jw.setVisible(false);
}
public static void main(String[] args) {
CreateSplashScreen sp = new CreateSplashScreen();
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(CreateSplashScreen.class.getName()).log(Level.SEVERE, null, ex);
}
sp.CloseSplashScreen();
}
}
jWindow opened for 2 seconds but image doesn't paint... any thoughts?
image file is in the same folder as class file...
Why are you creating an internal JWindow when your class CreateSplashScreen already extends JWindow?
There is no need of it. You are messing with your program.
How?
You are actually viewing the inner JWindow by jw.setVisible(true); but you are painting the image in the CreateSplashScreen's `JWindow.
Try this code :
public class CreateSplashScreen extends JWindow
{
ImageIcon i = new ImageIcon(getClass().getResource("/createsplashscreen/testImage.png"));
public CreateSplashScreen() {
setSize(700, 500);
setLocationRelativeTo(null);
setVisible(true);
}
#Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(i.getImage(), 0, 0, null);
}
public void CloseSplashScreen() {
setVisible(false);
}
public static void main(String[] args) {
CreateSplashScreen sp = new CreateSplashScreen();
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
}
sp.CloseSplashScreen();
}
}
Note: I do not know about your method to fetch image resource from the source folder.
Edit: Assuming that the name of the package containing your class CreateSplashScreen is createsplashscreen, make sure that the image testImage.png is present in the createsplashscreen package of your project.
#Peter
For error code, I deleted one line that I added in mamifest.mf file and build a program...
This time, didn't give me an error, weird...
I was following error code when I got it and it led me to something like "CLASSPATH" section of application generated code... sorry I can't remember exactly
Really appreciate Peter for your help.
Wish you luck...

Image in JFrame doesn't want to display with the call in another class

I work on a Java development software with Swing and I have a problem with my code, I want to display an image with the LoadingFrame class, its main work but when I call the constructor and the start() method in my main class, the frame opens but the image doesn't display (I have no Exception).
Why it doesn't work with my main class?
public class LoadingFrame
{
private JFrame frame;
public LoadingFrame()
{
frame = new JFrame();
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setUndecorated(true);
frame.setContentPane(new Panneau());
}
public void start()
{
frame.setVisible(true);
}
public void stop()
{
frame.setVisible(false);
}
public static void main(String[] args)
{
LoadingFrame l = new LoadingFrame();
l.start();
try
{
Thread.sleep(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
l.stop();
}
}
public class Panneau extends JPanel
{
public void paintComponent(Graphics g)
{
System.out.println("hello");
try
{
Image img = ImageIO.read(new File("Images/loading.png"));
//g.drawImage(img, 0, 0, this);
//Pour une image de fond
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
The App class is my main class :
public class App {
//Attributes used to display the application
private JFrame frame;
//Attribute which display a waiting frame
private static LoadingFrame loadingFrame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
loadingFrame = new LoadingFrame();
loadingFrame.start();
App window = new App();
loadingFrame.stop();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public App()
{
initialize();
synchronizeScriptReferenceList();
synchronizeTests();
}
[...]
}
I was able to get this to work from App.java. For some reason, using EventQueue isn't cutting it. I tried to use SwingUtilities as well, but that doesn't work either. Finally I tried just get rid of the Thready-stuff in App.main at just straight up running it in the main thread. For some reason, this works when the other approaches do not! Here is my code:
// In the App class:
public static void main(String[] args) {
try {
loadingFrame = new LoadingFrame();
loadingFrame.start();
App window = new App();
loadingFrame.stop();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
When I used this code, I got it to work! (for some reason unknown to me), And here's a bonus rewrite of the Panneau class:
class Panneau extends JPanel
{
Image img;
public Panneau() {
try
{
img = ImageIO.read(new File("Images/loading.png"));
}
catch (IOException e)
{
e.printStackTrace();
}
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
There are two main difference with this class; problems which I addressed. Here they are:
I call super.paintComponent as the very first method in our own paintComponent
I only load the loading image once, in the constructor, and not every single time I want to draw, which moves everything along much smoother. (you don't want the loading screen to be CPU heavy, do you?)
Hopefully, with these improvements, I hope you can make your program work! It worked with me, so I wish the best of luck to you.
P.S. Don't call frame.pack(), that was a mistake on my part. For some reason, I think it doesn't work well with undecorated windows.

Display issues LWJGL

I am running this simple code to display a window in eclipse using lwjgl:
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
#SuppressWarnings("unused")
public class DisplayExample {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(1920, 1080));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL here
while (!Display.isCloseRequested()) {
// render OpenGL here
Display.update(); //flushes OpenGL pipeline and swaps back and front buffers. perhaps waits for v-sync.
}
Display.destroy();
}
public static void main(String[] argv) {
DisplayExample displayExample = new DisplayExample();
displayExample.start();
}
}
However the screen appears like this and is flickering:
http://tinypic.com/r/33upp2u/6
This is running on a mac, any ideas what is going wrong?
You aren't clearing the screen before you update the display. Add GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); before // render OpenGL here. You also need to import the org.lwjgl.opengl.GL11 class for this.

Issue with "GL11.glMatrixMode()"

I recevie 0 errors but I get this in the console:
>Exception in thread "main" java.lang.NullPointerException
>>at org.lwjgl.opengl.GL11.glMatrixMode(GL11.java:2052)
>>>at game.engine.GameLoop.start(GameLoop.java:22)
>>>>at game.engine.GameLoop.main(GameLoop.java:15)
I tried to put the actual value into "glMatrixMode()" instead of "GL11.GL_PROJECTION" but it returns same Exception.
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Display;
public class GameLoop
{
//Main
public static void main(String[] argv)
{
GameLoop.start();
}
//Metodo che gestisce il loop
public static void start()
{
//Inizializzazione OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
try
{
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
} catch (LWJGLException e)
{
e.printStackTrace();
System.exit(0);
}
while(!Display.isCloseRequested())
{
Entità.pulisci();
Entità.colora();
Entità.disegna();
Display.update();
}
}
}
import org.lwjgl.opengl.GL11;
public class Entità
{
//Disegna un poligono
public static void disegna()
{
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(100,100);
GL11.glVertex2f(200,100);
GL11.glVertex2f(200,200);
GL11.glVertex2f(100,200);
GL11.glEnd();
}
//Pulisce il buffer
public static void pulisci()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
}
//Setta il colore al poligono
public static void colora()
{
GL11.glColor3f(0.5f,0.5f,1.0f);
}
}
I don't have any experience with Java or LWJGL, but from looking at your code it seems
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
creates the actual display canvas (or however you may call it) which also sets up the OpenGL context. Thus calling any OpenGL function (like GL11.glMatrixMode) before those is likely to result in some error, as in your case a null pointer exception.
So try to put those four functions after the display creation. You are making changes to the OpenGL context, thus you first need a valid context.

What do I put for making Graphics g?

This is an extremely simple question, but I have no idea what to do.
package typeandscreen;
imports
public class DisplayWindow {
Font f = new Font();
void run(){
try {
Display.setDisplayMode(new DisplayMode(1000,560));
Display.setTitle("Test for Bitmaps / Letters");
Display.create();
} catch(LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
while(!Display.isCloseRequested()){
Graphics g;
What do I make Graphics g equal? I just want to draw the subImage f.letters[0] onto the screen, but I don't know what I'm supposed to put there.
Making the lower line Graphics.drawImage(etc) doesn't work, and leaving it how it is, is a null pointer exception. If I do "Graphics g = f.letters[0].getGraphics()" for the above line then it's also a null pointer exception.
g.drawImage(f.letters[0],0,0,null);
Display.update();
}
Display.destroy();
}
public static void main(String args[]){
DisplayWindow dw = new DisplayWindow();
dw.run();
}
}

Categories

Resources