Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import java.awt.image.BufferedImage;

public class Main {
    public static void drawImage(BufferedImage image, Graphics g, Component parent) {
        Dimension size = parent.getSize();
        Color background = parent.getBackground();
        if (image != null && size.width > 0) {
            double ratio = (double) image.getHeight(null) / image.getWidth(null);

            int effectiveWidth = 1;
            int effectiveHeight = (int) ratio;

            while (effectiveHeight < size.height && effectiveWidth < size.width) {
                effectiveWidth++;
                effectiveHeight = (int) (ratio * effectiveWidth);
            }

            g.setColor(background);
            g.fillRect(0, 0, size.width, size.height);

            int cornerx = Math.abs((size.width - effectiveWidth) / 2);
            int cornery = Math.abs((size.height - effectiveHeight) / 2);
            g.drawImage(image, cornerx, cornery, effectiveWidth + cornerx, effectiveHeight + cornery, 0, 0,
                    image.getWidth(null), image.getHeight(null), null);
        }
    }
}