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 4 of 4

Thread: Why does this code now work?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Why does this code now work?

    I have been following a video tutorial and need some questions answered about some of the things he types youtu.be/VE7ezYCTPe4?t=10m

    Starting at 10:00 in the video he mentions that this BorderLayout is not something we need to know about. I am now getting errors with this code and I am wondering EXACTLY what this does and what perimeters I am not meeting.

    In the tutorial he names his frame NAME but I chose a name like he mentioned we could. Here is my code and I get a red line under BorderLayout (I marker this in bold)

    frame = new JFrame("D&A");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLayout(new BorderLayout());

    frame.add(this.BorderLayout.CENTER);
    frame.pack();

    frame.setResizable(false);
    frame.setLocationRelativeTo(null);;
    frame.setVisible(true);


    Can anyone help? I have tried Ctrl + Shift + O and imported everything I have been told to.












    [My Full Code So Far]








    package ca.vanzeben.game;

    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;

    import javax.swing.JFrame;

    public class game extends Canvas implements Runnable {


    private static final long serialVersionUID = 1L;

    public static final int WIDTH = 160;
    public static final int HEIGHT = WIDTH/12*9;
    public static final int SCALE = 3;
    public static final String NAME = "D&A";


    private JFrame frame;

    public boolean running = true;
    public int tickCount = 0;

    private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ RGB);
    private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    public game() {
    setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
    setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
    setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));

    frame = new JFrame("D&A");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLayout(new BorderLayout());

    frame.add(this.BorderLayout.CENTER);
    frame.pack();

    frame.setResizable(false);
    frame.setLocationRelativeTo(null);;
    frame.setVisible(true);

    }

    public synchronized void start() {
    new Thread(this).start();
    running = true;
    }

    public synchronized void stop() {
    running = false;
    }

    public void run() {
    long lastTime = System.nanoTime();
    double nsPerTick = 1000000000D/60D;

    int frames = 0;
    int ticks = 0;

    long lastTimer = System.currentTimeMillis();
    double delta = 0; //How many nanoseconds have gone by so far

    while(running) {
    long now = System.nanoTime();
    delta += (now - lastTime)/ nsPerTick;
    lastTime = now;
    boolean shouldRender = true;

    while(delta >= 1) {
    ticks++;
    tick();
    delta +=1;
    shouldRender = true;
    }

    try{
    Thread.sleep(2);
    }

    catch (InterruptedException e) {
    e.printStackTrace();
    }

    if (shouldRender) {
    frames++;
    render();
    }

    if (System.currentTimeMillis() - lastTime >= 1000) {
    lastTime += 1000;
    frames = 0;
    ticks = 0;
    }
    }
    }
    public void tick() {
    tickCount++;
    }
    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) {
    new game().start();
    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Why does this code now work?

    When posting code, make sure you use the highlight tags to preserve formatting.

    You should also try to boil your problem down to an MCVE instead of your whole project.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does this code now work?

    Sorry for formatting issues I'm new to this Forum and Java in general but do you have a possible solution to my problem I would be very greatful

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Why does this code now work?

    Sure, I'll be happy to try to help as soon as you edit your post to use the highlight tags and proper formatting.

    You'll also get help sooner if you narrow your problem down to an MCVE. It's up to you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Code doesn't work
    By Nicken99 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2014, 05:24 AM
  2. Help me I am trying to get this code to work.
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 26th, 2013, 07:41 PM
  3. HI, could someone please tell me why my code doesnt work?
    By joelmeler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 3rd, 2011, 02:37 AM
  4. Timer code does not work!
    By mariapatrawala in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 10:03 AM
  5. please tell me why this code does not work
    By amr in forum Java Theory & Questions
    Replies: 9
    Last Post: December 6th, 2010, 06:46 PM