Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Drawing in a canvas

Drawing in a canvas

Scheduled Pinned Locked Moved Java
graphicsjavatutorial
13 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R rareseu

    i hear what you're saying, but i can't instace it like

    g=new Graphics();

    because it's abstract, the null thing is automated correction from netbeans, it's the only way it will let me use g the problem i have with sun's exmaple is that i see no point in defining an object that inherits the Canvas to do nothing different that what canvas is supossed to do, it would also render my canvas objects useless and the ability to drag and drop canvas stuff from the IDE pointless

    J Offline
    J Offline
    Jimmanuel
    wrote on last edited by
    #4

    In my limited time as a Java developer (the last 15 minutes) it sounds like extending the Canvas class in order to draw things on it is it's intended usage. If the IDE has defined a new class for you that extends from it then shouldn't it be as simple as overriding it's paint method?

    :Badger:

    R 1 Reply Last reply
    0
    • R rareseu

      Hy guys ! i've been using netbeans to make a GUI, and i'm having rpoblems figuring out how to draw stuff using the canvas object, i've created a canvas object but it needs a graphics object in order to draw stuff, and that's where my problems start, for the life of me i can not understand how to use one of these this is what i've tried so far :

      Graphics g=null;
      g.setColor(new Color(255,0,0));
      canvas1.paint(g);

      when i run this java flares up with null pointer exceptions altho the canvas itself has been instantiated i've looked on the sun homepage for guidance but there is no hint on how to use an instance of a canvas object, they way they show is by creating a class that inherits Canvas and overwriting it's paint method, but that's no use to me

      F Offline
      F Offline
      fly904
      wrote on last edited by
      #5

      What I normally do is to override the default paint method in the Canvas class. Have a look at this[^]. The SweepCanvas class is the basic principle, just remove the threading obviously.

      My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

      R 1 Reply Last reply
      0
      • J Jimmanuel

        In my limited time as a Java developer (the last 15 minutes) it sounds like extending the Canvas class in order to draw things on it is it's intended usage. If the IDE has defined a new class for you that extends from it then shouldn't it be as simple as overriding it's paint method?

        :Badger:

        R Offline
        R Offline
        rareseu
        wrote on last edited by
        #6

        [quote]If the IDE has defined a new class for you that extends from it then shouldn't it be as simple as overriding it's paint method? [/quote] the IDE creates an instance of the class Canvas, the class that extends Canvas part was in the sun example

        J 1 Reply Last reply
        0
        • R rareseu

          [quote]If the IDE has defined a new class for you that extends from it then shouldn't it be as simple as overriding it's paint method? [/quote] the IDE creates an instance of the class Canvas, the class that extends Canvas part was in the sun example

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #7

          I found in Netbeans in the "Inspector" you can select a GUI element, right click on it and go to "Customize Code". That allows you to change the variable from a standard Canvas to your custom type that extends from Canvas that does the drawing.

          R 1 Reply Last reply
          0
          • J Jimmanuel

            I found in Netbeans in the "Inspector" you can select a GUI element, right click on it and go to "Customize Code". That allows you to change the variable from a standard Canvas to your custom type that extends from Canvas that does the drawing.

            R Offline
            R Offline
            rareseu
            wrote on last edited by
            #8

            That's an awesome tip Jimmanuel thanks :)

            1 Reply Last reply
            0
            • F fly904

              What I normally do is to override the default paint method in the Canvas class. Have a look at this[^]. The SweepCanvas class is the basic principle, just remove the threading obviously.

              My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

              R Offline
              R Offline
              rareseu
              wrote on last edited by
              #9

              [quote]What I normally do is to override the default paint method in the Canvas class. Have a look at this[^]. The SweepCanvas class is the basic principle, just remove the threading obviously. [/quote] good tip :) thank you, what i don't understand is where the run method gets invoked ( and as a consequence the repaint method), If anyone could explain this i think i could get the hang of how this class works. I see that the trick to making this work is calling the paint method using repaint() which saves you from making a Graphics instance( not the most obvious solution but i cant afford to be picky right now).

              F 1 Reply Last reply
              0
              • R rareseu

                [quote]What I normally do is to override the default paint method in the Canvas class. Have a look at this[^]. The SweepCanvas class is the basic principle, just remove the threading obviously. [/quote] good tip :) thank you, what i don't understand is where the run method gets invoked ( and as a consequence the repaint method), If anyone could explain this i think i could get the hang of how this class works. I see that the trick to making this work is calling the paint method using repaint() which saves you from making a Graphics instance( not the most obvious solution but i cant afford to be picky right now).

                F Offline
                F Offline
                fly904
                wrote on last edited by
                #10

                I'm not great at explaining things, but have a look at this[^] and this[^].

                My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

                R 1 Reply Last reply
                0
                • F fly904

                  I'm not great at explaining things, but have a look at this[^] and this[^].

                  My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

                  R Offline
                  R Offline
                  rareseu
                  wrote on last edited by
                  #11

                  Aaah i see now , but if i don't implement the class with threading(which i'm trying to avoid just because it would add more complexity ), what would be a good mechanism to call the repaint method ? btw thanks for your patience, and sorry for all the noobie questions but my deadline is closing in fast and i haven't even gone into the difficult part of the project

                  F 1 Reply Last reply
                  0
                  • R rareseu

                    Aaah i see now , but if i don't implement the class with threading(which i'm trying to avoid just because it would add more complexity ), what would be a good mechanism to call the repaint method ? btw thanks for your patience, and sorry for all the noobie questions but my deadline is closing in fast and i haven't even gone into the difficult part of the project

                    F Offline
                    F Offline
                    fly904
                    wrote on last edited by
                    #12

                    If you don't want to use threading then simple use canasVariableName.repaint();. I think that's what you mean but I'm not sure. If you do want to use threading then this method is the simplest way.

                    My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

                    R 1 Reply Last reply
                    0
                    • F fly904

                      If you don't want to use threading then simple use canasVariableName.repaint();. I think that's what you mean but I'm not sure. If you do want to use threading then this method is the simplest way.

                      My failometer is detecting vast quantities of FAIL! "Its SQL - hardly programming..." (Caslen)

                      R Offline
                      R Offline
                      rareseu
                      wrote on last edited by
                      #13

                      Thank you soo much, you're a life saver :D

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups