Results 1 to 15 of 15

Thread: Blank screen

  1. #1
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default Blank screen

    Hey there
    I am following a tutorial.
    From what I can see, I have exactly the same code as the tutorial does, but when I execute my code, I just get a blank screen.
    It is supposed to be an animation.
    Images are in the same folder as the source code.
    Can you tell me where I am going wrong (I am a complete beginner)
    Here is the code:
    Bucky class, contains main method
    Java Code:
    import java.awt.*;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import java.awt.Frame.*;
    import java.lang.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.JFrame;
    
    public class bucky {
    
        public static void main(String[] args) {
            bucky b = new bucky();
            b.run();
        }
        private Animation a;
        private ScreenManager s;
        private Image bg;
        private static final DisplayMode modes1[] = {
            new DisplayMode(800, 600, 32, 0),
            new DisplayMode(800, 600, 24, 0),
            new DisplayMode(800, 600, 16, 0),
            new DisplayMode(640, 480, 32, 0),
            new DisplayMode(640, 480, 24, 0),
            new DisplayMode(640, 480, 16, 0),};
    
        public void LoadImages() {
            bg = new ImageIcon("back.PNG").getImage();
            Image face1 = new ImageIcon("//face_up.PNG").getImage();
            Image face2 = new ImageIcon("//face_down.PNG").getImage();
            a = new Animation();
            a.addScene(face1, 250);
            a.addScene(face2, 250);
        }
    
        //main method called from main
        public void run() {
            s = new ScreenManager();
            try {
                DisplayMode dm = s.findFirstCompatibleMode(modes1);
                s.setFullScreen(dm);
                LoadImages();
                movieLoop();
            } finally {
                s.restoreScreen();
            }
        }
        //play movie
    
        public void movieLoop() {
            long startingTime = System.currentTimeMillis();
            long cumTime = startingTime;
            while (cumTime - startingTime < 5000) {
                long timePassed = System.currentTimeMillis() - cumTime;
                cumTime += timePassed;
                a.update(timePassed);
    
                //draw and update screen
                Graphics2D g = s.getGraphics();
                draw(g);
                g.dispose();
                s.update();
    
                try {
                    Thread.sleep(20);
                } catch (Exception ex) {
                }
            }
        }
    
        public void draw(Graphics g) {
            g.drawImage(bg, 0, 0, null);
            g.drawImage(a.getImage(), 0, 0, null);
        }
    }
    Screen Manager class
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.JFrame;
    
    public class ScreenManager {
    
        private GraphicsDevice vc;
    
        //give vc access to monitor
        public ScreenManager() {
            GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
            vc = e.getDefaultScreenDevice();
        }
    
        public DisplayMode[] getCompatibleDisplayModes() {
            return vc.getDisplayModes();
        }
    
        //compares dm passed in...
        public DisplayMode findFirstCompatibleMode(DisplayMode modes[]) {
            DisplayMode goodModes[] = vc.getDisplayModes();
            for (int x = 0; x < modes.length; x++) {
                for (int y = 0; y < goodModes.length; y++) {
                    if (displayModesMatch(modes[x], goodModes[y])) {
                        return modes[x];
                    }
                }
            }
            return null;
        }
    
        //get current dm
        public DisplayMode getCurrentDisplayMode() {
            return vc.getDisplayMode();
        }
    
        //chechs if two modes match eachother
        public boolean displayModesMatch(DisplayMode m1, DisplayMode m2) {
            if (m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()) {
                return false;
            }
            if (m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()) {
                return false;
            }
            if (m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()) {
                return false;
            }
    
            return true;
        }
    
        //make frame full screen
        public void setFullScreen(DisplayMode dm) {
            JFrame f = new JFrame();
            f.setUndecorated(true);
            f.setIgnoreRepaint(true);
            f.setResizable(false);
            vc.setFullScreenWindow(f);
    
            if (dm != null && vc.isDisplayChangeSupported()) {
                try {
                    vc.setDisplayMode(dm);
                } catch (Exception ex) {
                }
            }
            f.createBufferStrategy(2);
        }
        //we will set Graphics object =  to this
    
        public Graphics2D getGraphics() {
            Window w = vc.getFullScreenWindow();
            if (w != null) {
                BufferStrategy s = w.getBufferStrategy();
                return (Graphics2D) s.getDrawGraphics();
            } else {
                return null;
            }
        }
    
        //updates display
        public void update() {
            Window w = vc.getFullScreenWindow();
            if (w != null) {
                BufferStrategy s = w.getBufferStrategy();
                if (!s.contentsLost()) {
                    s.show();
    
                }
            }
        }
    
        public Window getFullScreenWinodw() {
            return vc.getFullScreenWindow();
        }
    
        public int getWidth() {
            Window w = vc.getFullScreenWindow();
            if (w != null) {
                return w.getWidth();
            } else {
                return 0;
            }
        }
    
         public int getHeight() {
            Window w = vc.getFullScreenWindow();
            if (w != null) {
                return w.getHeight();
            } else {
                return 0;
            }
        }
    
    
    
         public void restoreScreen(){
             Window w = vc.getFullScreenWindow();
             if(w != null){
                 w.dispose();
             }
             vc.setFullScreenWindow(null);
         }
    
         //create image compatible with monitor
    
         public BufferedImage createCompatibleImage(int w, int h, int t){
             Window win = vc.getFullScreenWindow();
             if(win != null){
                 GraphicsConfiguration gc = win.getGraphicsConfiguration();
                 return gc.createCompatibleImage(w, h, t);
             }
             return null;
         }
    }
    Animation class
    Java Code:
    import java.awt.Image;
    import java.util.ArrayList;
    
    public class Animation {
    
        private ArrayList scenes;
        private int sceneIndex;
        private long movieTime;
        private long totalTime;
    
        public Animation() {
            scenes = new ArrayList();
            totalTime = 0;
            start();
        }
    
        public synchronized void addScene(Image i, long t) {
            totalTime += t;
            scenes.add(new OneScene(i, totalTime));
        }
    
        //start animation form begining
        public synchronized void start() {
            movieTime = 0;
            sceneIndex = 0;
        }
    
        //change scenes
    
        public synchronized void update(long timePassed){
            if(scenes.size()>1){
                movieTime += timePassed;
                if(movieTime >= totalTime){
                    movieTime = 0;
                    sceneIndex = 0;
                }
                while(movieTime > getScene(sceneIndex).endTime){
                    sceneIndex++;
                }
            }
        }
    
    
        public synchronized Image getImage(){
            if(scenes.size()==0){
                return null;
            }
             else{
                return getScene(sceneIndex).pic;
    
            }
        }
    
        //get scene
        private OneScene getScene(int x){
            return (OneScene)scenes.get(x);
        }
    
    
        //private inner class
    
        private class OneScene{
            Image pic;
            long endTime;
    
            public OneScene(Image pic, long endTime){
                this.pic = pic;
                this.endTime = endTime;
            }
        }
    }
    THANKS :D

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    There are 3 .png files used. Do you have them?

  3. #3
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    yep



  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    I used some images I have and the program displayed them on the full screen in the upper left corner.

  5. #5
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    oh, did you change any code?
    if so
    can i have it please?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    The only changes I made were for the 3 images to be my images.

  7. #7
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    could you please send me that bit of code?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Java Code:
            bg = new ImageIcon(" images/middle.gif").getImage();
            Image face1 = new ImageIcon(" images/left.gif").getImage();
            Image face2 = new ImageIcon(" images/right.gif").getImage();
    Without the leading blank inside the "s. The forum changes what's in the "s if there is no blank

  9. #9
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    thanks but I am still confused
    i tried to follow that format but I couldn't get it to work.
    Could you have a look at the following two screenshots that display the directory my images are stored in and the code and tell me where I am going wrong please.
    your time is appreciated!


  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Just an aside: I see that you're using default packages, and that begs the question, "why"? You really need to learn about and use packages before attempting animation. The Java tutorials can show you how.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Sorry, I have no idea how to configure or use your IDE.

  12. #12
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Just an aside: I see that you're using default packages, and that begs the question, "why"? You really need to learn about and use packages before attempting animation. The Java tutorials can show you how.
    I am a complete beginner in java.
    I have read a couple of tutorials with packages but didn't really understand them.
    Could you please tell me how I can fix this particular project? :)

  13. #13
    Spike_CT is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    Have you tried to set "Game" as your active/current project? Currently Netbeans asumes that "Sprites" is the active project to be compiled, debuged and run.

    The option to set "Game" as your current project can be found by right clicking "Game" in you're project explorer.

    Hope this solves the problem.

  14. #14
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Spike_CT View Post
    Have you tried to set "Game" as your active/current project? Currently Netbeans asumes that "Sprites" is the active project to be compiled, debuged and run.

    The option to set "Game" as your current project can be found by right clicking "Game" in you're project explorer.

    Hope this solves the problem.
    Nope it didn't work.
    Thanks tho
    It must be something really obvious I'm doing wrong :/

  15. #15
    Spike_CT is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    Packages are not exactly the same thing as folders.

    Netbeans starts a project with its relative path starting at the projects root folder (in you're case a folder named "game"). The source files are located in a file called "src" and you're images are probably located in "src\images\.."

    try prefixing "src\" before the pathnames

    Another solution would be to change the location of you're relative path:

    * right click on "game"
    * choose "Properties"
    * choose "Run"
    * click the button "Browse" next to "Working Directory"
    * select you're relative path (the folder containing the image folder)
    Last edited by Spike_CT; 08-12-2010 at 05:23 PM.

Similar Threads

  1. how can we remove blank lines from a .txt
    By Camden in forum New To Java
    Replies: 12
    Last Post: 07-29-2011, 01:38 PM
  2. Blank Applet on JCreator
    By mgm2010 in forum New To Java
    Replies: 0
    Last Post: 05-05-2009, 03:21 PM
  3. Blank space
    By sandy1028 in forum New To Java
    Replies: 1
    Last Post: 04-21-2009, 10:00 AM
  4. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 05:03 PM
  5. Print a blank space
    By susan in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •