Java Fractal Explorer [6/8]

WilliamFiset · Intermediate ·⚡ Algorithms & Data Structures ·11y ago

Key Takeaways

The video demonstrates implementing a mouse listener in Java to enable zooming in and out of a fractal explorer application, utilizing the MouseListener interface and adjusting the zoom factor based on mouse click events.

Full Transcript

i said we were going to look at the make color method in this video but then i realized that doesn't make sense since we can't zoom into the fractal and see the changes in color yet so instead we're going to work on zooming in this video also note that i changed some of the formatting for this application i added divider lines to make our methods uh clearer and i also added a few comments here and there so let's go to the canvas and this is where go where we're going to implement the mouse listener so we can actually respond to mouse click events i decided to do the mouse listener on the canvas and not on the fractal explorer j frame the reason is simple is because if we did it on the j frame then we'd have an extra spacing right here for this little gray line which is about 20 to 23 pixels thick well if we just do it on the canvas then we don't have to worry about that difference which makes things a whole lot easier so add implements oops spelt right and blow vents mouse listener and when you implement the mouse listener you're going to need to override five methods so let's do that so these are the five methods you need to override for the mouse listener interface but we're actually only going to use some code or write some code in one of them so that's going to be the mouse pressed so the first thing we're going to do with this mouse event object is get the x coordinate and the y coordinate of where the mouse clicked so let's do that x there's a neat method called get x and similarly for y it's called get y and this will get the x and y coordinate of the mouse relative to the canvas which is exactly what we want next we're going to say mouse dot gets button and this will determine whether we press the left mouse button or the right mouse button so let's start the left one so that's the case that our switch statement is button one and let's put break for now and the right is the case where mouse event is button three button two is reserved for um some mouses have a scroll in the middle so that's what button two would be for and when we co when when we click on the left we're going to want to call a method we're about to create called adjust zoom and we're going to adjust the zoom relative to x and y and the only thing we're going to change here is we're going to say zoom factor multiplied by 2. and same thing for right except it's going to divide by two so so the left button is going to be zooming in that's why the zoom factor is increasing and the right mouse button is going to be to zoom out that's why the zoom factor is decreasing or that's why we're dividing by two if you want a more intensive zoom then feel free to modify this so now let's create actually wait we need to add one more thing we need to call a method on our canvas so in the constructor we actually need to say add mouse listener on this and this should initialize the fact that we have a mouse moving around on canvas and now to create adjust zoom all right it's going to take new x and double new y for double new zoom factor good and here we're going to modify what our top left x is what our top left y is and what our new zoom factor should be as well as call update fractal so so the top left x is going to be the new x divided by the zoom factor while top left y is going to be new y divided by the zoom factor and our zoom factor is going to equal the new zoom factor right here and then we're going to perform a shift once again so top left x minus equals the width divided by 2 divided by the zoom factor top left y plus equals don't forget to change that the height divided by 2 divided by the zoom factor and now one last call to update fractal now what does this all mean and what does this do the first two lines zoom into the fractal essentially by shifting top left x and top left y one way and then we're saying zoom factor equal to this new zoom factor and we shifted top left x and top left y one way but they were off centered so we need to re-center them with these two lines right here and then we can update our fractal and we're zoomed in right where we clicked so let's see if this works now so open up the terminal and go to wherever your your fractal explorer is so now if i click on the screen click here you can see we're actually zooming into the fractal however the color is not changing and that's what we will be focusing on the next video but check out how cool this is you can theoretically zoom in forever although although the the precision of a double on this computer does limit us to how far we can zoom in so here you can see i'm approaching the maximum and soon you can start making out the details of the pixels see those are this is as far as you can get and if you right click you can zoom out as much as you want and if you're having performance issues zooming in you can reduce the number of max iterations that you do although you will get a less precise fractal that's the only thing i am warning you about so and also one more thing if you click on where there's a lot of black that means you're doing more iterations because all these black points mean that the inter count is equal to max inter meaning each of these black points needed to do at least uh however much max it there was i think mine was 200 if i if i check quickly uh yeah yes it was so each of these black points did 200 iterations meaning it's fairly slow to zoom in where there is some black on the screen well if i zoom into where it's blue then it's there's a faster performance guys thank you for watching and we're going to keep working on this fractal explorer and the next video see you then

Original Description

Git video snapshots: https://github.com/william-fiset/FractalExplorer/commits/master =============================================================================== Developer tools I used in the creation/testing of the content in these videos: 1) Sublime text, my favorite lightweight code editor (https://www.sublimetext.com). NOTE: I'm often asked about the color scheme I use, find it here: https://github.com/williamfiset/dotfiles/tree/master/sublime 2) Kite, a free AI-powered coding assistant that provides smart code completions while typing: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=williamfiset&utm_content=description-only ===============================================================================
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from WilliamFiset · WilliamFiset · 14 of 60

1 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

This video teaches how to implement a mouse listener in Java to enable zooming in and out of a fractal explorer application. The mouse listener is used to respond to mouse click events and adjust the zoom factor accordingly.

Key Takeaways
  1. Implement the MouseListener interface
  2. Override the necessary methods
  3. Get the x and y coordinates of the mouse click event
  4. Determine which mouse button was clicked
  5. Adjust the zoom factor based on the mouse button clicked
  6. Call the updateFractal method to update the fractal display
💡 The mouse listener is used to respond to mouse click events and adjust the zoom factor accordingly, allowing for interactive zooming in and out of the fractal explorer application.

Related Reads

Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →