Python Tkinter Sonic Wave Animation

How to Create a Sonic Wave Animation in Python Tkinter

How to Create a Sonic Wave Animation in Python Tkinter


In this Python Tutorial we will see How to Create a Sonic Wave animation that features animated bars with dynamic colors, glow effects, and smooth sine wave motion Using Python Tkinter.

What We Are Gonna Use In This Project:

- Python Programming Language.
- Tkinter (GUI).
- VS Editor.






Project Source Code:




import tkinter as tk
import math
import time
import colorsys

class SonicWave(tk.Canvas):

def __init__(self, master=None, **kwargs):
# Configuration
self.BAR_COUNT = 7
self.BAR_WIDTH = 5
self.BAR_GAP = 10
self.BAR_HEIGHT = 65
self.ANIMATION_DELAY = 1500 # milliseconds for full cycle

# Dark background color
bg_color = "#111122"

# Set canvas dimensions
width = self.BAR_COUNT * (self.BAR_WIDTH + self.BAR_GAP) + 40
height = self.BAR_HEIGHT + 60

# Initialize the canvas with our background color
super().__init__(master, width=width, height=height, bg=bg_color,
highlightthickness=0, **kwargs)

# Define colors
self.colors = [
"#00eeff", # blue
"#b300ff", # purple
"#ff00ff", # pink
"#0aff0a", # green
"#ff3300" # orange
]

# Initialize bar heights and animation state
self.bar_heights = [0.2] * self.BAR_COUNT
self.hue_shift = 0.0

# Start the animation
self.is_running = True
self.animate()


def get_color_with_shift(self, base_color, offset=0):
"""Apply a hue shift to a color for dynamic color effects"""
# Convert hex to RGB
r = int(base_color[1:3], 16) / 255.0
g = int(base_color[3:5], 16) / 255.0
b = int(base_color[5:7], 16) / 255.0

# Convert RGB to HSV
h, s, v = colorsys.rgb_to_hsv(r, g, b)
# Apply the hue shift
h = (h + self.hue_shift + offset) % 1.0
# Convert back to RGB
r, g, b = colorsys.hsv_to_rgb(h, s, v)

# Convert to hex
return f"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}"

def animate(self):
"""Main animation loop"""
if not self.is_running:
return

self.update_bar_heights()
self.draw_spinner()

# Schedule the next animation frame
self.after(30, self.animate)


def update_bar_heights(self):
"""Update the heights of each bar based on a sine wave function"""
current_time = time.time() * 1000 # Convert to milliseconds

# Update each bar's height based on sine wave function
for i in range(self.BAR_COUNT):
# Calculate phase offset for each bar
phase_offset = i * (math.pi / 8)

# Use sine wave to animate heights (0.2 to 1.0 range)
progress = (current_time % self.ANIMATION_DELAY) / self.ANIMATION_DELAY
angle = 2 * math.pi * progress + phase_offset
self.bar_heights[i] = 0.2 + 0.8 * abs(math.sin(angle))

# Gradually shift the hue
self.hue_shift = (self.hue_shift + 0.005) % 1.0




def create_text_shadow(self, x, y, text, font_config):
# Create shadow text (slight offset, darker color)
shadow_color = "#555555"
self.create_text(x+2, y+2, text=text, fill=shadow_color, font=font_config)

# Create main text with a fixed color
main_color = "#FFFFFF" # Use white color
self.create_text(x, y, text=text, fill=main_color, font=font_config)



def draw_spinner(self):
"""Draw the spinner with visual elements"""
# Clear the canvas
self.delete("all")

# Calculate layout dimensions
total_width = self.BAR_COUNT * (self.BAR_WIDTH + self.BAR_GAP) - self.BAR_GAP
start_x = (self.winfo_width() - total_width) // 2
base_y = (self.winfo_height() + self.BAR_HEIGHT) // 2 - 10

# Draw "SONIC WAVE" text label
# Ensure the text is positioned in a visible area
text_y = min(base_y + self.BAR_HEIGHT // 2 + 20, self.winfo_height() - 20)
self.create_text_shadow(
self.winfo_width() // 2,
text_y,
"SONIC WAVE",
("Arial", 13, "bold")
)

# Draw bars
for i in range(self.BAR_COUNT):
x = start_x + i * (self.BAR_WIDTH + self.BAR_GAP)
height = int(self.bar_heights[i] * self.BAR_HEIGHT)
y = base_y - height

# Get color based on current animation state with dynamic hue shifting
bar_color = self.get_color_with_shift(self.colors[i % len(self.colors)],
i * 0.05)

# Draw the bar
self.create_rectangle(
x, y, x + self.BAR_WIDTH, base_y,
fill=bar_color, outline="", width=0
)
# Create a glow effect by drawing multiple rectangles
for j in range(1, 5):
# Create outlines for glow effect
self.create_rectangle(
x - j, y - j, x + self.BAR_WIDTH + j, base_y + j,
outline=bar_color, width=1
)

def stop_animation(self):
"""Method to stop the animation when no longer needed"""
self.is_running = False



class AppFrame(tk.Frame):
"""A frame to hold the spinner with UI elements"""

def __init__(self, master=None, **kwargs):
super().__init__(master, **kwargs)
self.config(bg="#0d0d18", padx=25, pady=25)
self.pack(fill=tk.BOTH, expand=True)

# Create a background
self.create_background()

# Add the spinner
self.spinner = SonicWave(self)
self.spinner.pack(pady=10)

# Add a version or title or any text label
version_label = tk.Label(
self,
text="v2.0",
bg="#0d0d18",
fg="#444444",
font=("Arial", 8)
)
version_label.pack(side=tk.BOTTOM, anchor=tk.SE, padx=5, pady=5)

def create_background(self):
"""Create a background frame"""
gradient_frame = tk.Frame(self, width=300, height=250, bg="#0d0d18")
gradient_frame.place(x=0, y=0, relwidth=1, relheight=1)





def main():
root = tk.Tk()
root.title("Sonic Wave")

# Set dark theme for the window
root.configure(bg="#0a0a14")

# Create our app frame
app_frame = AppFrame(root)

# Set window size and position
root.geometry("300x250")
root.eval('tk::PlaceWindow . center') # Center the window

# Make the window non-resizable for consistent layout
root.resizable(False, False)

# Handle window close event
def on_closing():
app_frame.spinner.stop_animation()
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)

# Start the main event loop
root.mainloop()


if __name__ == "__main__":
main()



The Final Result:

Python Tkinter Sonic Wave Animation 1

Python Tkinter Sonic Wave Animation 2

Python Tkinter Sonic Wave Animation 3









Java Combobox With Images

How to Add Images to a Combobox In Java Netbeans

How to Add Images to a Combobox In Java Netbeans


In this Java Tutorial we will see How To Create a JCombobox With Images Inside It In Java Using Netbeans.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package new_tutorials;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

/**
 *
 * @author 1BestCsharp
 */
public class ComboboxWithImagesFrame {
    
    // Custom renderer for displaying images in the combo box
    static class ImageComboboxRenderer extends DefaultListCellRenderer{
        
        
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean hasFocus){
            
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
            
            if(value instanceof ImageItem item){
                label.setText(item.getText());
                label.setIcon(item.getImageIcon());
            }
            
            return  label;  
        }
    }
    
    
    // Method to load images
    private static ImageIcon loadImage(String imageName){
        // Load image from the "images" package
        String imagePath = "images/" + imageName;
        return new ImageIcon(ComboboxWithImagesFrame.class.getResource(imagePath));
        
    }
    
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Combobox With Images");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,250);
        frame.setLocationRelativeTo(null);
        
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
        
        List<ImageItem> items = new ArrayList<>();
        items.add(new ImageItem("analyst", loadImage("analyst.png")));
        items.add(new ImageItem("designer", loadImage("designer.png")));
        items.add(new ImageItem("developer", loadImage("developer.png")));
        items.add(new ImageItem("manager", loadImage("manager.png")));
        
        JComboBox<ImageItem> comboBox = new JComboBox<>(items.toArray(ImageItem[] :: new));
        comboBox.setRenderer(new ImageComboboxRenderer());
        comboBox.setBackground(Color.WHITE);
        comboBox.setPreferredSize(new Dimension(150, 50));
        
        panel.add(comboBox);
        frame.add(panel);
        frame.setVisible(true);
        
    }
    
 
}


    // Define a simple data class to represent items in the combo box
    class ImageItem{
        
        private final String text;
        private final ImageIcon imageIcon;
        
        // Constructor
        public ImageItem(String text, ImageIcon icon){
            this.text = text;
            this.imageIcon = icon;
        }
        
        // Getters
        public String getText(){ return text; }
        
        public ImageIcon getImageIcon(){ return imageIcon; }
            
    }
    


The Final Result:

Java Combobox With Images




Java - Create Rounded Buttons

How to Create and Design Rounded Button In Java Netbeans

How to Create a Rounded JButton Using Java Swing


In this Java Tutorial we will see How To Create two rounded Jbuttons, and clicking each button will trigger a message dialog.
The rounded buttons have a gradient-colored background

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package new_tutorials;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 *
 * @author 1BestCsharp
 */
public class RoundedButtonFrame extends JFrame{

    public RoundedButtonFrame(){
        
        setTitle("Rounded Button Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,150);
        setLocationRelativeTo(null);
        initializeUI();
    }
    
    private void initializeUI(){
        
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.CENTER,20,40));
        
        RoundedButton button1 = new RoundedButton("Button 1");
        button1.setBackground(new Color(255,69,96));
        
        RoundedButton button2 = new RoundedButton("Button 2");
        button2.setBackground(new Color(70,130,180));
        
        button1.addActionListener((e) -> {
            JOptionPane.showMessageDialog(this, "Button 1 Clicked");
        });
        
        button2.addActionListener((e) -> {
            JOptionPane.showMessageDialog(this, "Button 2 Clicked");
        });
        
        panel.add(button1);
        panel.add(button2);
        
        add(panel);
        
    }
    
    public static void main(String[] args) {
        RoundedButtonFrame frame = new RoundedButtonFrame();
        frame.setVisible(true);
    }

}


// Create a custom JButton class for rounded buttons
class RoundedButton extends JButton{
    
    public RoundedButton(String text){
        
        super(text);
        setUI(new RoundedButtonUI());
        setFont(new Font("Arial",Font.BOLD, 16));
        setForeground(Color.WHITE);
        setCursor(new Cursor(Cursor.HAND_CURSOR));
        
    }
    
}


// Create a custom UI class for rendering rounded buttons
class RoundedButtonUI extends BasicButtonUI{
    
    @Override
    public void installUI(JComponent c){
        super.installUI(c);
        AbstractButton button = (AbstractButton) c;
        
        button.setOpaque(false);
        button.setBorderPainted(false);
    }
    
    
    @Override
    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text){
        super.paintText(g, c, textRect, text);
    }
    
    
    @Override
    public void paint(Graphics g, JComponent c){
        AbstractButton btn = (AbstractButton) c;
        // Paint the background with rounded corners
        paintBackground(g, btn, btn.getModel().isPressed() ? 2 : 0);
        super.paint(g, c);
    }
    
    // Method to paint the background with rounded corners
    private void paintBackground(Graphics g, JComponent c, int yOffset){
        
        Dimension size = c.getSize();
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
        // Fill a rounded rectangle with a darker color
        GradientPaint gradientPaint = new GradientPaint(0, yOffset, c.getBackground().brighter(), 0,size.height - yOffset,c.getBackground().darker());
        g2d.setPaint(gradientPaint);
        g2d.fillRoundRect(0, yOffset, size.width, size.height - yOffset, 25, 25);
        g2d.dispose();
    }
    
}



The Final Result:

Java Rounded Button




Java - Fonts Selector with Combobox

How to Create a Font Chooser ComboBox In Java Netbeans

How to Create a Font Chooser ComboBox In Java Netbeans


In this Java Tutorial we will see How To Create a graphical user interface for selecting and displaying different fonts using a JComboBox In Java Using Netbeans.
We Will Retrieves all the available font family names from the local graphics environment, and Creates a JComboBox (fontsComboBox) with the array of font family names.
When a font family is selected from the combo box, a JLabel in the frame is updated to use the selected font family.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package new_tutorials;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

/**
 *
 * @author 1BestCsharp
 */
public class FontsComboBox extends JFrame{
    
    private final JLabel label;
    
    
    public FontsComboBox(){
        
        setTitle("Fonts Combobox");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Create an array of font family names
        String[] fontFamilies = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        // Create a JComboBox with a custom renderer
        JComboBox<String> fontsComboBox = new JComboBox<>(fontFamilies);
        fontsComboBox.setRenderer(new FontComboBoxRenderer());
        
        fontsComboBox.addActionListener((e) -> {
           
            String selectedFontFamiliy = (String)fontsComboBox.getSelectedItem();
            updateLabelFont(selectedFontFamiliy);
            
        });
        
        label = new JLabel("Simple Text");
        label.setHorizontalAlignment(JLabel.CENTER);
        
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(fontsComboBox, BorderLayout.NORTH);
        panel.add(label, BorderLayout.CENTER);
        getContentPane().add(panel);
        setSize(400,300);
        setLocationRelativeTo(null);
        
    }
    
    
    private void updateLabelFont(String fontFamily){
        // Update the font of the label based on the selected font family
        Font font = new Font(fontFamily, Font.PLAIN, 18);
        label.setFont(font);
    }
    
    
    private static class FontComboBoxRenderer extends DefaultListCellRenderer{
        
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean hasFocus){
            // Use the default rendering for the item text
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
            
            // Set the font for the item text based on the font family
            String fontFamily = (String) value;
            Font font = new Font(fontFamily, Font.PLAIN, 24);
            label.setFont(font);
            return label;
        }
        
    }
    

    public static void main(String[] args) {
        
        FontsComboBox frame = new FontsComboBox();
        frame.setVisible(true);
        
    }
    
}


The Final Result:

Java - Fonts Selector with Combobox

How To Select And display Different Fonts Using a JComboBox In Java Using Netbeans

Select And display Different Fonts Using a JComboBox In Java Using Netbeans

How To Select And display Different Fonts Using a JComboBox In Java

Select And display Different Fonts Using a JComboBox In Java