• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

set pixel in frame

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there!

i have a class that extends JFrame, i was wondering if it is possible to set (draw) individual pixels within the frame, i.e. something that looks like:



at the moment i'm considering (which is probably inefficient):




thanks,
tian.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't tried drawing a line like that for some time, but as far as I can remember, you don't even try it on a JFrame.
You add a JPanel to the JFrame, and put such drawing into the paintComponent(Graphics g) method.
Two warnings
  • You have to put super.paintComponent(); as the first line of your method.
  • There is also a paintComponents() method, so don't get confused with it.
  • Then you get a Graphics object in the method, which makes it easy to write g.drawLine(x1, y1, x2, y2);
    [ September 19, 2006: Message edited by: Campbell Ritchie ]
     
    Tian Zhang
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks for your reply!

    i currently have what you suggested in my code - but ommitted them from the code listing for simplicity. is there a method in the Graphics class to paint individual pixels, or will i need to paint lines with length 1?

    tian
     
    Bartender
    Posts: 4121
    IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Nope - no method in the graphics class to draw a pixel - you have to draw a line of length 1.
     
    Tian Zhang
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks,

    i had feared as much. perhaps the overhead of drawing length 1 lines isnt as bad as i thought.


    tian
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Lines of length 0, surely?
     
    Tian Zhang
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi there!

    the code will be something like:


    which will draw pixel at (x, y) so i guess it is length zero (length:=sqrt((x1 - x2)^2 + (y1 - y2)^2) - as defined by the eucledian metric).

    tian
    [ September 21, 2006: Message edited by: Tian Zhang ]
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic