mleonhard / www-tamale-net

0 stars 1 forks source link

[Question about card.java] Why card class extends with jframe? #1

Open karaposu opened 2 years ago

karaposu commented 2 years ago

Hello, I just started to learn programming. And somehow I come across to your code. I tried to implemented it by myself but my code became spaghetti in no time. So now I started reading about design patterns and also trying to understand your implementation

Why did you extended card class with jframe? Was is it so that updating card images (turning on front or back images) will directly effect jframe and you do not need to check for updates?

mleonhard commented 2 years ago

Are you referring to https://github.com/mleonhard/www-tamale-net/blob/main/coursework/cs340/mp2/Card.java ? That project uses Java's Swing library to draw a user interface. Swing provides the JLabel class which can display text or an icon. We need the JLabel to respond to mouse events, so it must implement MouseListener. Since JLabel itself doesn't implement MouseListener, we make a sub-class of JLabel which implements MouseListener. Swing calls the mouseClicked method when the user clicks on the JLabel. Our implementation of mouseClicked checks if the click is in the target area and then tries to turn the card up.

I wrote that code during university. It's not very good. It has mistakes. For example, Card's mousePressed and mouseReleased methods don't need to call mouseClicked, since Swing does that automatically.

Swing is imperative. When we call this.setIcon() on the JLabel, the label updates the screen. I think the containing JPanel and JFrame are only responsible for positioning the JLabel and passing mouse events to it.

karaposu commented 2 years ago

I am coming from embedded systems(ES) engineering background. In ES when we need to update something or check if a button clicked we either create software interrupts which will detect an external event and go and run an update() function or we hardcode the update() function to be updated by timers every x milliseconds.

I am guessing inside of Jlabel there must be same logic hidden and Jlabel is a good abstraction to such things.

Right now I am designing same game but card objects are made of Jbuttons. I tried to draw class uml diagram. However I am stuck in 2 issues. Should card class extend jbuttons? or simple aggrevation is enough? And second issue is since Jlable is how we update visuals, should card class also extend Jlabel?

mleonhard commented 2 years ago

If you're stuck, just try both ways. You need to get familiar with event-driven code. Nearly all GUI software on strong machines (not micro-controllers) uses a thread and an "event handler" loop which reads a queue of events from the OS and other processes. The event handler reads a mouse event and calls a method on the top-level window object. The window object inspects the offset of the event, iterates through its list of child widgets to find the top-most widget enclosing that screen position, and calls a method on that object. That object does the same, and eventually your JLabel is called. When your code updates the content of the widget, the JLabel code calls graphics methods which update the window frame buffer and the changes end up on the monitor. That's essentially how it works.

I think that UML is not particularly useful.