Is it a bad practice to use swing and awt classes for my non-graphics classes in my programs?

So let's say I have a game like pong. Obviously, you don’t want to mix game logic with graphic classes at all, so the class for Ball or Paddle is the sepearte from JPanel, which actually draws them. The ball has movement logic for the ball, its current location, hit detection, etc. However, is it bad practice for me to use the graphic classes from Swing and awt in my Ball class? For example, if I used java.awt.Rectangle to define a hitbox. Although I do not draw it in this class, I use it. Or, if I use Java.awt.Point to store coordinates.

By the way, the reason I am asking about is that I was repeatedly told on this site so as not to mix the graphics with other parts.

Using a Rectangle in a class without graphics: (Is this bad practice?)

public class Ball { static Rectangle hitbox = new Rectangle(0,10,20,20); static void checkHit() { if(hitbox.intersects(Paddle.hitbox) //do something } } 

My Graphics class:

 public class DrawMyStuff extends JPanel { void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.BLACK); Graphics2D g2d = (Graphics2D) g; g2d.draw(Ball.hitbox); } } 
+6
source share
1 answer

I would say that in most cases you should not do this.

Exceptions to the Rule

However, there is a (small) set of helper classes in this package that are actually only good-type data holders. Why reinvent the wheels when you have already received them? That would be β€œgood use” in my world.

  • The size
  • Inserts
  • Point
  • Rectangle (and all other Shape , such as Polygon, Area, etc.)

They can be used to describe graphical problems as long as they are not directly related to screen resources.

+6
source

All Articles