Given a variable with type Graphics
,
how do I cast it to Graphics2D
in Scala?
Asked
Active
Viewed 9.5k times
200

USB
- 6,019
- 15
- 62
- 93

Eugene Yokota
- 94,654
- 45
- 215
- 319
2 Answers
284
The preferred technique is to use pattern matching. This allows you to gracefully handle the case that the value in question is not of the given type:
g match {
case g2: Graphics2D => g2
case _ => throw new ClassCastException
}
This block replicates the semantics of the asInstanceOf[Graphics2D]
method, but with greater flexibility. For example, you could provide different branches for various types, effectively performing multiple conditional casts at the same time. Finally, you don't really need to throw an exception in the catch-all area, you could also return null
(or preferably, None
), or you could enter some fallback branch which works without Graphics2D
.
In short, this is really the way to go. It's a little more syntactically bulky than asInstanceOf
, but the added flexibility is almost always worth it.

Daniel Spiewak
- 54,515
- 14
- 108
- 120
227
g.asInstanceOf[Graphics2D];

Eugene Yokota
- 94,654
- 45
- 215
- 319
-
37Once I got used to Scala, I learnt not to use `asInstanceOf`, since it defeats the purpose of having static type system and feels yucky. – Eugene Yokota Feb 26 '11 at 22:51
-
27Unfortunately, this a common operation when using Swing. For custom painting operations, you need to override the 'public void paintComponent(Graphics g)' method. The Graphics parameter is actually a Graphics2D instance, but a cast is needed. The pattern matching version is probably more verbosity than warranted. Remember: Sedulously eschew obfuscatory hyperverbosity and prolixity! – hohonuuli Oct 16 '11 at 06:19
-
7@hohonuuli I think the cast is fine in that specific case, but if you use `scala-swing` components, paintComponent's parameter is already Graphics2D so no cast required – Luigi Plinge Oct 16 '11 at 08:22
-
2Why its so long? Why "asInstanceOf when can be only be "as" or "asof" keyword or method? Or why they did not just adopt the C++ and Java way as an option because that is the conventional and there is no big problem with that? – LEMUEL ADANE Nov 25 '14 at 10:01
-
16@LemuelAdane The fact that you're using casts at all is a code smell, it makes no sense to make them easier. – Daenyth Dec 04 '14 at 20:30
base match { case MyConcrete(value) => base.asInstanceOf[MyConcrete].something(value) }
, is there a way to get 'base' casted to MyConcrete even if want to extract "value" by exploiting the 'unapply' call performed by "case MyConcrete(value)" ? – mkm Dec 11 '10 at 17:52