So, lets say I have some game, take Pong for example. Obviously, you generally speaking don't want to mix game-logic into graphics classes, so the class for Ball or Paddle is sepearte from the JPanel which actually draws them. Ball has the movement logic for the ball, its current location, hit detection, etc. However, is it bad practice for me to use graphics classes from Swing and awt in my Ball class? For example, if I were to use a java.awt.Rectangle to determine the hitbox. Even though I am not drawing it in this class, I am using it. Or if I were to use Java.awt.Point to store coordinates.
By the way, the reason I am asking is because I have been told many times on this site not to mix graphics with other parts.
Using Rectangle in non-graphics class: (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);
}
}
java.awt.Rectangle
class.. but if you are planning to do something else with it, you need to keep in mind fact wven if you use your code for android app, you will stilll need to provide swing/awt jar files. and adding complex libary to the project only for one class or even one method it is overkill – Biauriculate