Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Graphics, Canvas Unreachable statement

  1. #1
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Graphics, Canvas Unreachable statement

    Hey, I am trying to follow this tutorial: https://www.youtube.com/watch?v=dwu4DAoac-I (or you can look on yt yourself, TheChernoProject: game programming episode 6), but perhaps I made an error along the way, since it says unreachable statement at this part:
    Graphics g = bs.getDrawGraphics();

    If anyone has some time to spare to enlighten me as to what I did wrong, I would very much appreciate it.

    Thank you in advance!




    Full code:
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
     
    import javax.swing.JFrame;
     
    public class Game extends Canvas implements Runnable {
        private static final long serialVersionUID = 1L;
        public static int width = 300;
        public static int height = width / 16 * 9;
        public static int scale = 3;
     
        private Thread thread;
        private JFrame frame;
        private boolean running = false;
     
        public Game() {
            Dimension size = new Dimension (width * scale, height * scale);
            setPreferredSize(size);
     
            frame = new JFrame();
        }
     
        public synchronized void start () {
            running = true;
            thread = new Thread (this,"Game");
            thread.start ();
        }     
     
        public synchronized void stop() {
            running = false;
            try {
                thread.join();  
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }  
     
        public void run() {
            while (running) {
                update();
                render();
            }
        }
     
        public void update () {
        }
     
        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) {
            Game game = new Game();
            game.frame.setResizable(true);
            game.frame.add(game);
            game.frame.pack();
            game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            game.frame.setVisible(true);
     
            game.start();
        }
     
    }
    English is not my native language (Typo alert).


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Graphics, Canvas Unreachable statement

    It says that because your program will always terminate before the line has been reached. It will terminate because you have a return-statement right before that line.
    The problem comes with the way you wrote your if-then block. Look at it very closely and maybe you will see something that doesnt belong there.

  3. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    GregBrannon (August 16th, 2014), Time4Java (August 16th, 2014)

  4. #3
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Graphics, Canvas Unreachable statement

    Feel so stupid now :0

    Love you
    English is not my native language (Typo alert).

Similar Threads

  1. 46: Unreachable Statement
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 7th, 2011, 07:42 AM
  2. Methods Help - Unreachable statement error
    By jessica202 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 10:24 AM
  3. another unreachable statement..
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2009, 12:15 PM
  4. unreachable statement Again?
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 1st, 2009, 10:23 AM
  5. unreachable statement?
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 11th, 2009, 10:06 AM

Tags for this Thread