• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Is it possible to make graphics such as "fillRect..." into a variable?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

I want to do a simple background which is only rendered once onto my screen. I want to use

and make the whole background black, but only once. I wanted to try out making the fillRect into a variable so I can do:

The point is, I want G.fillRect(0, 0, getWidth(), getHeight()); to be a variable, even if this is the wrong way to approach what I want to accomplish, is it possible to make this line of code into one variable? The rest of my code if you need info:


Thanks!
 
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create an interface (or use an existing one) and create anonymous implementations (Java 7 and before) or lambda's (Java 8) for them. For example, in Java 7:
Java 8 makes it a bit easier, also because there are already a lot of matching existing interfaces. For instance, using java.util.function.Consumer:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!