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)
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); } }
source share