Java Project – Logo Maker Application

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 :

  • IDE Used: Visual Studio Code (you can use any IDE)
  • Java should be installed appropriately on the machine.
  • Your concepts of Java should be clear.
  • Java provides by default packages such as Abstract Window Toolkit (AWT) & Swing packages to create a graphical user interface (GUI)
  • You should have a little knowledge of Maven.

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:

  • The LogoGenerator class, which extends JFrame, is in the com.example package of Maven and imports necessary Java Swing and AWT classes for GUI and image handling.
  • We define the GUI components and declare a map named FONT_STYLES to store font style names with their corresponding font file names.
  • With the help of the LogoGenerator Constructor, we set up the JFrame window properties like title and size.
  • We initialise GUI components and add them to the JFrame (inputPanel for text input and button, imageLabel for displaying generated logos).
  • We apply an Action Listener to the generateButton() method to handle clicks. It calls generateAllLogos() to generate logos based on the input.
  • generateAllLogos() generates a combined image containing logos for all font styles specified in FONT_STYLES. It uses generateLogo() to create individual logos and draws them on combinedImage.
  • generateLogo() is used to generate a single logo image with the specified text, using the font loaded from fontFile.
  • It centers the text in the image and adds some shadow effect on the text in teal color.
  • At last, we initialise and make the LogoGenerator JFrame visible on the Event Dispatch Thread using SwingUtilities.invokeLater().

Java Logo Maker Application Output

java logo maker opening screen

java logo maker 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.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *