Java Fractal Explorer [3/8]

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

Key Takeaways

This video demonstrates how to create a custom Canvas class in Java, extending the JPanel class, and override its getPreferredSize and paintComponent methods to display a fractal image on the screen. The video uses Sublime text as the code editor and utilizes Java's built-in classes such as BufferedImage and Graphics.

Full Transcript

in this video we are going to look at how to put a canvas on top fire jframe the reason we want canvas is because we want to actually be able to manipulate the pixels and change their colors and some have something dynamic that's responsive and not just a frame which we actually can't change the color of the pixels for so we are going to need two things we're going to need canvas oops campus and we're also going to need something to draw on our campus and that's going to be a buffered image which I'm era called a fractal image canvas unfortunately is not a built in class in Java so we are going to have to create it ourselves so let's do that just down here and we're going to make it a private inner class because we want it to be able to access some of our methods and the fractal Explorer and our canvas is going to extended the jpanel because we want with a jpanel on top of the jframe which is our fractal explorer program our canvas is going to override two methods get preferred size and paint component which is going to tell our canvas how to behave so to make sure we're overriding put the app override sign this is optional public I mention it bird oh sighs and get preferred size should return fruit size or canvas which we already know what this is because we know what the dimension of the jframe is it's going to be the same let's just going to deal with and height the next method we're going to override is a more important paint component and it takes a graphics object as an argument and this graphics object is going to be the object that's going to draw our fractal image onto the canvas itself so i'm going to call this the drawing object and the drawing object is just going to call one method called draw image and this is going to be the method which is going to be responsible for drawing that fractal image on the screen we're going to make the fractal image be 600 x 600 or width x height so we're just going to place it at the top left corner of the screen so these are XY coordinates and the last argument is for an observer we don't have an observer so put null right there so the last thing we need to do is actually initialize the canvas and the frankie lymon and display them on the screen to do that let's add another method called add canvas so private void add canvas and we're just going to initialize the canvas say new canvas and lets it should initialize the fractal image so create new bufferedimage and this cases arguments the size and the type of bufferedimage we want a bufferedimage types are constants in the bufferedimage class so so we can specify the type of one and we just want type called type and oops RGB which is the standard type for a red green blue so next we are going to set the canvases property to be visible and lastly we are going to add them to the frame so canvas and we're going to put this in the center so I borderlayout dot center oh I can see I have a spelling mistake right here and get preferred size so this is would throw an error because at override would say hey this isn't actually a method we're overriding because it's not spelled correctly so let's fix that it and now let's actually run this program and see if we have successfully added the canvas to the screen let's go to the terminal and do pilus and run it and as we can see instead of having a frame with a white background now have a frame with a black background and the black simply means that we have successfully added the canvas on top of the frame so guys thank you for watching and I'll see you in the next video

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 · 21 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
14 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
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 create a custom Canvas class in Java to display a fractal image on the screen. It covers the creation of a private inner class, overriding methods, and using Java's built-in graphics classes.

Key Takeaways
  1. Create a new private inner class extending JPanel
  2. Override the getPreferredSize method
  3. Override the paintComponent method
  4. Initialize the canvas and fractal image
  5. Add the canvas to the frame
  6. Set the canvas property to visible
💡 To display a fractal image on the screen, you need to create a custom Canvas class and override its methods to handle graphics rendering.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →