Java Fractal Explorer [6/8]
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
2
3
4
5
6
7
8
9
10
11
12
13
▶
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
Related Reads
📰
📰
📰
📰
Morris Pre Order Traversal
Dev.to · Jaspreet singh
Advanced Stack ApplicationsData Structures and Algorithms Deep‑Dive — Advanced Stack Applications…
Medium · Programming
The Minecraft anvil is a tree-cost optimization problem in disguise
Dev.to · Mark
KMP Algorithm (Knuth-Morris-Pratt): The Smart Way to Perform String Matching in O(N)
Dev.to · Jaspreet singh
🎓
Tutor Explanation
DeepCamp AI