Site icon DataFlair

Java Project – Logo Maker Application

java logo maker project

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

In this project, we will create a Logo Maker Application in Java using Swing, Java I/O classes, and the Abstract Window Toolkit. We have tried to add as many Fonts as possible to provide more distinct and attractive Logos.

About Java Logo Maker Application

The Logo Maker Project is a powerful tool to create professional-looking logos. Users can select shapes, add text, choose colors and arrange elements on a canvas to design their logos. You can also add your custom fonts to this project. For now, we have added many fonts to provide variety in our logos. Ensure you use the .ttf Format file for the Fonts.

Prerequisites for Java Logo Maker Application

Before starting with the DataFlair Logo Maker Project, ensure the following prerequisites are met :

Note: This project was created using Maven, allowing for easy addition of new features through the pom.xml file.

Download the Java Logo Maker Application Code

Please download the source code of the Java Logo Maker Application Project: Java Logo Maker Application Project Code

Code Implementation of Java Logo Maker Application

1. LogoGenerator.java

package com.example;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class LogoGenerator extends JFrame {

    private JTextField inputText;
    private JButton generateButton;
    private JLabel imageLabel;

    private static final Map<String, String> FONT_STYLES = new HashMap<>();
    static {
        FONT_STYLES.put("Childish", "ChildishFree 400.otf");
        FONT_STYLES.put("Smoother", "Smoother.otf");
        FONT_STYLES.put("Handwritten", "Honey Notes DEMO.ttf");
        FONT_STYLES.put("Rounded", "Candy Cane Personal Use.ttf");
        FONT_STYLES.put("Texture", "BrockScript.ttf");
        FONT_STYLES.put("Brush", "MilestoneBrush.ttf");
        FONT_STYLES.put("Taller", "taller.ttf");
        FONT_STYLES.put("Trendy", "Sergio Trendy.ttf");
        FONT_STYLES.put("Bolder", "GreatVibes-Regular.ttf");
    }

    public LogoGenerator() {
        setTitle("DataFlair Logo Generator");
        setSize(1000, 1000);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new FlowLayout());

        inputText = new JTextField(20);
        generateButton = new JButton("Generate Logos");

        inputPanel.add(new JLabel("Enter Text:"));
        inputPanel.add(inputText);
        inputPanel.add(generateButton);

        add(inputPanel, BorderLayout.NORTH);

        imageLabel = new JLabel();
        add(new JScrollPane(imageLabel), BorderLayout.CENTER);

        generateButton.addActionListener(e -> {
            String text = inputText.getText();
            if (!text.isEmpty()) {
                try {
                    BufferedImage combinedImage = generateAllLogos(text);
                    ImageIcon icon = new ImageIcon(combinedImage);
                    imageLabel.setIcon(icon);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Error generating the logos: " + ex.getMessage());
                }
            } else {
                JOptionPane.showMessageDialog(null, "Please enter some text to generate the logos.");
            }
        });
    }

    private BufferedImage generateAllLogos(String text) throws IOException, FontFormatException {
        int logoWidth = 300;
        int logoHeight = 150;
        int margin = 20;
        int numColumns = 3;
        int numRows = (FONT_STYLES.size() + numColumns - 1) / numColumns;

        BufferedImage combinedImage = new BufferedImage(
                logoWidth * numColumns + margin * (numColumns + 1),
                logoHeight * numRows + margin * (numRows + 1),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = combinedImage.createGraphics();

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, combinedImage.getWidth(), combinedImage.getHeight());

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        int xOffset = margin;
        int yOffset = margin;

        for (Map.Entry<String, String> entry : FONT_STYLES.entrySet()) {
            String fontStyle = entry.getKey();
            String fontFile = entry.getValue();
            try {
                BufferedImage logoImage = generateLogo(text, fontFile, logoWidth, logoHeight);
                g2d.drawImage(logoImage, xOffset, yOffset, null);
            } catch (IOException | FontFormatException ex) {
                ex.printStackTrace();
                System.out.println("Error generating logo for font: " + fontStyle);
            }

            xOffset += logoWidth + margin;
            if ((xOffset + logoWidth + margin) > combinedImage.getWidth()) {
                xOffset = margin;
                yOffset += logoHeight + margin;
            }
        }

        g2d.dispose();
        return combinedImage;
    }

    private BufferedImage generateLogo(String text, String fontFile, int width, int height)
            throws IOException, FontFormatException {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);

        File file = new File("src/main/java/com/example/" + fontFile);
        if (!file.exists()) {
            System.out.println("Font file not found: " + file.getAbsolutePath());
            throw new IOException("Font file not found: " + fontFile);
        }

        Font font = Font.createFont(Font.TRUETYPE_FONT, file).deriveFont(Font.BOLD, 48);
        g2d.setFont(font);

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        FontMetrics fm = g2d.getFontMetrics();
        int textWidth = fm.stringWidth(text);
        int x = (width - textWidth) / 2;
        int y = (height - fm.getHeight()) / 2 + fm.getAscent();

        Color tealAccent = new Color(0, 128, 128);

        g2d.setColor(tealAccent.darker());
        g2d.drawString(text, x + 2, y + 2);
        g2d.setColor(tealAccent);
        g2d.drawString(text, x, y);

        g2d.dispose();
        return image;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new LogoGenerator().setVisible(true));
    }
}


Explanation:

Java Logo Maker Application Output

Conclusion

We have completed our objective of creating a DataFlair Logo Maker Project in Java. We implemented this project in Maven with the help of fonts downloaded from the web, but you can also add your custom Fonts. You can also utilise an API for more creative logo designs.

I hope you enjoyed this project. Thank you.

Exit mobile version