Results 1 to 10 of 10
  1. #1
    pk rv is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default coding from a book should work :/

    i recently bought a book on java game development and i copied everything it said but it still doesnt work everything is copied exactly i use eclipse se for a coder but it say error line 24 which is : public static void main(String[] args) {
    please its not the only program in the book that hasnt work im starting to lose faith
    Java Code:
    package jframedemo;
    import javax.swing.*;
    import java.awt.*;
    public class JFrameDemo extends JFrame
    {
    
    	public JFrameDemo()
    	{
    		super("JFrameDemo");
    		setSize(400,400);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    		g.setColor(Color.WHITE);
    		g.fillRect(0,0,400,400);
    		g.setColor(Color.ORANGE);
    		g.setFont(newFont("arial",Font.BOLD,18));
    		g.drawString("Doing graphics with a Jframe!",60,200);
    	}
    
    	private Font newFont(String string, int bold, int i) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    Last edited by Norm; 05-07-2012 at 06:25 PM. Reason: added code tags

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

    Default Re: coding from a book should work :/

    Please post the full text of the compiler's error messages.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pk rv is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: coding from a book should work :/

    Description Resource Path Location Type
    Constructor call must be the first statement in a constructor DataTypes.java /MyProject line 5 Java Problem
    Return type for the method is missing DataTypes.java /MyProject line 4 Java Problem
    string cannot be resolved to a type tuna.java /marc/src line 4 Java Problem
    The serializable class DataTypes does not declare a static final serialVersionUID field of type long DataTypes.java /MyProject line 3 Java Problem
    The serializable class JFrameDemo does not declare a static final serialVersionUID field of type long JFrameDemo.java /MyProject/jframedemo line 4 Java Problem
    This method has a constructor name main.java /game/src/game line 6 Java Problem

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

    Default Re: coding from a book should work :/

    Can you copy and paste the error messages? What you have posted leaves out a lot of info
    Please copy full text of error message and paste it here. Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    pk rv is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: coding from a book should work :/

    i dont if this i what you mean but when i try to run this as a java applet it comes up with a problem
    Java Code:
    package jframedemo;
    import javax.swing.*;
    import java.awt.*;
    public class JFrameDemo extends JFrame
    {
    
    	public JFrameDemo()
    	{
    		super("JFrameDemo");
    		setSize(400,400);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    		g.setColor(Color.WHITE);
    		g.fillRect(0,0,400,400);
    		g.setColor(Color.ORANGE);
    		g.setFont(newFont("arial",Font.BOLD,18));
    		g.drawString("Doing graphics with a Jframe!",60,200);
    	}
    	public static void main(String[] args) {
      new JFrameDemo;
    	}
    
    }
    
    
    
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Syntax error on token "new", assert expected
    	JFrameDemo cannot be resolved to a variable
    
    	at jframedemo.JFrameDemo.main(JFrameDemo.java:24)
    Last edited by sunde887; 05-08-2012 at 01:05 AM.

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

    Default Re: coding from a book should work :/

    try to run this as a java applet
    The code you posted is not a java applet and can not be run as an applet. Its an application that would be executed using the java.exe command.

    Does the code compile without errors?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: coding from a book should work :/

    Any book that gives an example code with an override of paint(...) in a top level window isn't worth the paper it's printed on.

    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

    edit Constructing a new Font object every time through a painting method is also bad.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: coding from a book should work :/

    Error is most likely thath you have just
    Java Code:
    new JFrameDemo;
    It should be
    Java Code:
    new JFrameDemo();
    Are you fairly new to java? I don't know how I feel about a book starting with Swing so early. Which book is it?

  9. #9
    pk rv is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: coding from a book should work :/

    yes im fairly new to java i had learnt some before i went into a book but not much the book is called java game development for beginners by john s harbour

  10. #10
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: coding from a book should work :/

    Does that book go over any Java basics? I'm guessing it doesn't, but that it requires some basic knowledge of Java; how the language itself and its API works. Something you're clearly lacking. You need to go through the tutorials before jumping to creating games.

Similar Threads

  1. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  2. Need help please with coding
    By avaiblas in forum NetBeans
    Replies: 2
    Last Post: 12-24-2010, 01:19 AM
  3. Replies: 4
    Last Post: 10-25-2010, 03:49 PM
  4. Need help coding
    By ace_03 in forum New To Java
    Replies: 2
    Last Post: 11-25-2009, 05:16 PM
  5. Better coding
    By tomiu in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 07:19 PM

Tags for this Thread

Posting Permissions

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