Page 1 of 2 12 LastLast
Results 1 to 20 of 26
Like Tree2Likes

Thread: Java 2D Game, use Canvas without extending it

  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    115
    Rep Power
    0

    Default Java 2D Game, use Canvas without extending it

    Okay, I had this by default:

    Java Code:
    public class Game extends Canvas implements Runnable, KeyListener {
    	private static final long serialVersionUID = 1L;
    	
    	public Game() {
    		setMinimumSize(new Dimension(width, height));
    		setMaximumSize(new Dimension(width, height));
    		setPreferredSize(new Dimension(width, height));
    	}
    	
    	public synchronized void start() {
    		
    	}
    	
    	public synchronized void stop() {
    		
    	}
    	
    	public void run() {
    		
    	}
    	
    	public void keyPressed(KeyEvent arg0) {
    		
    	}
    
    	public void keyReleased(KeyEvent arg0) {
    		
    	}
    
    	public void keyTyped(KeyEvent arg0) {
    		
    	}
    }
    I want to remove the extends Canvas from the top line and when I use JFrame to add canvas, I don't do add.(this, but instead I do add a canvas variable I make. So instead of extending I want to make a variable. But then how would I do the start, stop and run?

    EDIT: I want to use Graphics, Canvas (Not Jpanel because I want to use BufferedImage) & JFrame. I don't want to extend Canvas, how could I use Canvas identically to shown above but instead of extending using it as a variable? or, how could I do this?

    Thanks
    Augustas
    Last edited by augustas656; 04-10-2014 at 03:55 AM.

  2. #2
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    115
    Rep Power
    0

    Default Can I use Canvas without extending it?

    Yeah, what the title says, so like Canvas, JFrame and Graphics, but I won't class name extends Canvas, rather maybe I'd create a variable of Canvas, possible?

    Thanks
    Augustas

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Can I use Canvas without extending it?

    I presume it is like any other Component. You can use composition if you don't need to override any of its methods. Using an instance of Canvas or JPanel can make it cumbersome to do painting because then you need to create an anonymous class to override the appropriate painting methods. So I guess it depends on what you want to do with Canvas. Others may have a different perspective.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Java 2D Game, use Canvas without extending it

    This is very close to a more recent thread of the same topic. --> http://www.java-forums.org/advanced-...extending.html.
    It is frowned upon to double post in this forum. IMHO, those two threads could be merged.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    115
    Rep Power
    0

    Default Re: Can I use Canvas without extending it?

    Doing Canvas name = new Canvas()
    name.method doesn't work. P:

  6. #6
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    115
    Rep Power
    0

    Default Re: Java 2D Game, use Canvas without extending it

    Since nobody replied to it, I believed this was a bad place to ask this kind of question, therefore I asked it in the advanced java forums, and it isn't more recent, I posted it after I posted this, that's why I said, since nobody replied I believed this was a bad place to post it. If I did something wrong, had to remove this by reporting it? Then someone can say that, but otherwise that's the reason.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default Re: Can I use Canvas without extending it?

    What does "doesn't work" mean?
    Does your computer explode?
    jim829 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,955
    Rep Power
    8

    Default Re: Java 2D Game, use Canvas without extending it

    The threads have been merged.

    You can use a Canvas without extending it. You can use a variable to refer to an instance of a class that extends Canvas. I'm not sure what you think the difference is. If you want to do custom painting, you have to extend something.

    You can use a BufferedImage with a JPanel. There really isn't a reason to use Canvas.

    It's very unclear what you're asking, and I think you're operating under false assumptions. Here is a basic tutorial on custom painting in Swing: Custom Painting - Tutorials - Static Void Games

    And here is another one: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    115
    Rep Power
    0

    Default Re: Java 2D Game, use Canvas without extending it

    Okay, wait. I'll tell you the error that I get and the code that got it.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Java 2D Game, use Canvas without extending it

    It's also probably not a good idea to mix AWT and Swing components. Just an opinion.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    115
    Rep Power
    0

    Default Re: Java 2D Game, use Canvas without extending it

    Now it works, anyway, P:, wierd, thanks. Wierd... P:

  12. #12
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    115
    Rep Power
    0

    Default Re: Java 2D Game, use Canvas without extending it

    I've meant BufferStrategy, I've read somewhere that using Canvas allows you to use BufferStrategy unlike JPanel.
    About custom painting, couldn't you just do something like canvas.super(graphics), or Idk. Is custom painting really that useful? I'm making my game without using it at all P:
    Last edited by augustas656; 04-10-2014 at 04:25 PM.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Java 2D Game, use Canvas without extending it

    Uh, oh. I can't resist.

    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

    Regards,
    Jim


    KevinWorkman likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,972
    Rep Power
    5

    Default Re: Java 2D Game, use Canvas without extending it

    Quote Originally Posted by augustas656 View Post
    I've meant BufferStrategy, I've read somewhere that using Canvas allows you to use BufferStrategy unlike JPanel.
    Correct. There wouldn't be much use for it when using a JPanel, because when you use a JPanel you use Swing and Swing already has double buffering support built in. In that respect if you use a Canvas, combine it with an AWT Window and not a Swing JFrame.

    Is custom painting really that useful? I'm making my game without using it at all P:
    As you'll at one point in time find out, doing games using AWT/Swing is not "useful" in its entirety. You may get something to work properly on your computer, but as soon as you give it to someone else all of a sudden things don't work properly anymore.

    But for learning purposes its fine.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Java 2D Game, use Canvas without extending it

    Quote Originally Posted by augustas656 View Post
    I've meant BufferStrategy, I've read somewhere that using Canvas allows you to use BufferStrategy unlike JPanel.
    About custom painting, couldn't you just do something like canvas.super(graphics), or Idk. Is custom painting really that useful? I'm making my game without using it at all P:
    Uh, do you know what custom painting is? Whenever you override the appropriate paint method and use the supplied graphics context, you are doing custom painting. Now of course, there are varying degrees of complexity of what and how you are painting.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    115
    Rep Power
    0

    Default Re: Java 2D Game, use Canvas without extending it

    Quote Originally Posted by gimbal2 View Post
    Correct. There wouldn't be much use for it when using a JPanel, because when you use a JPanel you use Swing and Swing already has double buffering support built in. In that respect if you use a Canvas, combine it with an AWT Window and not a Swing JFrame.



    As you'll at one point in time find out, doing games using AWT/Swing is not "useful" in its entirety. You may get something to work properly on your computer, but as soon as you give it to someone else all of a sudden things don't work properly anymore.

    But for learning purposes its fine.
    I'm using Triple Buffering, how could I use that with JPanel?
    My comp can run quadriple aswell, and the higher the better.

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,972
    Rep Power
    5

    Default Re: Java 2D Game, use Canvas without extending it

    Quote Originally Posted by augustas656 View Post
    I'm using Triple Buffering, how could I use that with JPanel?
    You can't. And no, not the higher the better - that's only what someone who has no clue how it actually works would say.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,955
    Rep Power
    8

    Default Re: Java 2D Game, use Canvas without extending it

    Quote Originally Posted by augustas656 View Post
    I've meant BufferStrategy, I've read somewhere that using Canvas allows you to use BufferStrategy unlike JPanel.
    About custom painting, couldn't you just do something like canvas.super(graphics), or Idk. Is custom painting really that useful? I'm making my game without using it at all P:
    You can have a game without custom painting. But most games are almost entirely custom painting.

    Why do you think you need to use BufferStrategy directly? Especially if you're not doing custom painting?

    What are you talking about when you ask if you could do canvas.super(graphics)?

    Like I said, it really seems like you're biting off more than you can chew. You need to take a step back, read some tutorials, and start much smaller.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    115
    Rep Power
    0

    Default Re: Java 2D Game, use Canvas without extending it

    Quote Originally Posted by gimbal2 View Post
    You can't. And no, not the higher the better - that's only what someone who has no clue how it actually works would say.
    I watched tutorials and the person said the higher the better, and he seemed to have java 2D game creation as second nature, like really easy for him. Although I do admit his tutorials were very messy and he did use canvas, jframe and graphics. He said higher the less teracing there would be in the image, but it needs better computers to run higher buffering.

  20. #20
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,972
    Rep Power
    5

    Default Re: Java 2D Game, use Canvas without extending it

    Yes triple buffering can be better than double buffering. And what would quadruple buffering add for benefit according to you?

    You have to understand how it works, just parroting what someone else said, in this case highly likely misquoting that person too, won't help you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 1 of 2 12 LastLast

Similar Threads

  1. Java canvas with palette
    By rcbsports in forum New To Java
    Replies: 1
    Last Post: 11-18-2012, 01:36 PM
  2. Replies: 16
    Last Post: 01-30-2012, 05:36 AM
  3. Extending java.net.Socket
    By DC200 in forum Networking
    Replies: 3
    Last Post: 06-10-2010, 04:23 PM
  4. Issues with updating a canvas in Java
    By Sandworm in forum Advanced Java
    Replies: 1
    Last Post: 11-19-2009, 08:58 AM
  5. Replies: 0
    Last Post: 07-05-2009, 12:31 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
  •