Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:46:50+00:00 2026-06-16T13:46:50+00:00

I am trying to write a simple Java Applet/Application that will resize itself depending

  • 0

I am trying to write a simple Java Applet/Application that will resize itself depending on how big it is. I have it basically working except when I paint something to the JFrame grpahics (jframe.getGraphics()), at the point 0,0, it seems to start at the upper left hand corner outside the boundaries of the window. having the first 30 or so pixels under the top bar of the window. Also jframe.getWidth() and jframe.getHeight() return to me the width and height of the window, not the visible part inside the window.

This is an excerpt of the code that I am using that demonstrates my problem:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class ImageHandler {
    private boolean debug;
    private int rememberWidth, rememberHeight, contractedImageX,
            contractedImageY, imageTranslationX, imageTranslationY, realWidth,
            realHeight, fixedWidth, fixedHeight;
    private BufferedImage bImage1, bImage2;
    private Graphics2D bufferedGraphics1, bufferedGraphics2;
    private boolean resizeNext, isApplet;

    public ImageHandler(ObjectHandler objH) {
        debug = objH.isDebug();
        objH.setImageHandler(this);
        objectHandler = objH;
        isApplet = objectHandler.isApplet();
        fixedWidth = objectHandler.getScreenWidth();
        fixedHeight = objectHandler.getScreenHeight();
    }

    public void paint(Paintable p) {
        /*
         * If the screen is not the same size at remembered, then re-run the
         * image transformations
         */
        realWidth = getRealWidth();
        realHeight = getRealHeight();
        if (objectHandler.isFocused() == true
                || objectHandler.isApplet() == true) {
            if (!(rememberWidth == realWidth && rememberHeight == realHeight)
                    || resizeNext) {
                resizeNext = false;
                if (debug)
                    System.out.println("Re-sizing");
                /* Create New Images */
                bImage1 = new BufferedImage(realWidth, realHeight,
                        BufferedImage.TYPE_INT_ARGB);
                bImage2 = new BufferedImage(fixedWidth, fixedHeight,
                        BufferedImage.TYPE_INT_ARGB);
                bufferedGraphics1 = (Graphics2D) bImage1.getGraphics();
                bufferedGraphics1.setColor(Color.black);
                bufferedGraphics1.fillRect(0, 0, realWidth, realHeight);
                bufferedGraphics2 = (Graphics2D) bImage2.getGraphics();
                /*
                 * Remember The current Height and width, so that it can check
                 * if the height has changed before running this again
                 */
                rememberWidth = realWidth;
                rememberHeight = realHeight;
                /*
                 * Define contractedImageX and y depending on the height of the
                 * screen
                 */
                contractedImageY = realHeight;
                contractedImageX = (int) ((double) contractedImageY
                        / (double) fixedHeight * fixedWidth);
                /*
                 * If the graphics defined by using the height make it go off
                 * the sides of the screen, redefine with the width
                 */
                if (debug) {
                    System.out.println("1Real Height:" + realHeight + " Width:"
                            + realWidth + " contractedHeight:"
                            + contractedImageY + " contractedWidth:"
                            + contractedImageX);
                }
                if (contractedImageX > realWidth) {
                    contractedImageX = realWidth;
                    contractedImageY = (int) ((double) contractedImageX
                            / (double) fixedWidth * fixedHeight);
                }
                if (debug) {
                    System.out.println("2Real Height:" + realHeight + " Width:"
                            + realWidth + " contractedHeight:"
                            + contractedImageY + " contractedWidth:"
                            + contractedImageX);
                }
                /*
                 * Re Calculate Image Translations so that they position the
                 * image correctly
                 */
                imageTranslationX = (realWidth - contractedImageX) / 2;
                imageTranslationY = (realHeight - contractedImageY) / 2;
                if (debug) {
                    System.out.println("X: " + imageTranslationX + " Y: "
                            + imageTranslationY);
                }

            }
            // clears the screen
            bufferedGraphics2.setColor(Color.black);
            bufferedGraphics2.fillRect(0, 0, fixedWidth, fixedHeight);
            p.paint(bufferedGraphics2);
            bufferedGraphics1.setColor(Color.black);
            bufferedGraphics1.fillRect(0, 0, realWidth, realHeight);
            bufferedGraphics1.drawImage(bImage2, imageTranslationX,
                    imageTranslationY, contractedImageX + imageTranslationX,
                    contractedImageY + imageTranslationY, 0, 0, fixedWidth,
                    fixedHeight, null);
            drawFinalImage(bImage1);
        }
    }

    private void drawFinalImage(Image img) {
        if (isApplet) {
            if (objectHandler.getjApplet() != null) {
                objectHandler.getjApplet().getGraphics()
                        .drawImage(img, 0, 0, null);
            }
        } else {
            if (objectHandler.getjFrame() != null) {
                objectHandler.getjFrame().getGraphics()
                        .drawImage(img, 0, 0, null);
            }
        }
    }

    private int getRealWidth() {
        if (objectHandler.isApplet()) {
            if (objectHandler.getjApplet() != null) {
                Integer w = objectHandler.getjApplet().getWidth();
                if (w != null && w > 0) {
                    return w;
                }
            }
        } else {
            if (objectHandler.getjFrame() != null) {
                Integer w = objectHandler.getjFrame().getWidth();
                if (w != null && w > 0) {
                    return w;
                }
            }
        }
        return fixedWidth;
    }

    private int getRealHeight() {
        if (objectHandler.isApplet()) {
            if (objectHandler.getjApplet() != null) {
                Integer h = objectHandler.getjApplet().getHeight();
                if (h != null && h > 0) {
                    return h;
                }
            }
        } else {
            if (objectHandler.getjFrame() != null) {
                Integer h = objectHandler.getjFrame().getHeight();
                if (h != null && h > 0) {
                    return h;
                }
            }
        }
        return fixedHeight;
    }
}

That code isn’t my entire program, just the part that re sizes the window and paints the graphics.
Paintable is an interface I created that has the method paint(Graphcis g).

I am looking for a way to find what the width and height of the visible section is, and the
offset I need to find the point at the upper left hand corner of the visible section.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:46:51+00:00Added an answer on June 16, 2026 at 1:46 pm

    You must add a custom JPanel and override getPreferredSize, as a JFrame takes decorations (title bar, etc.) into account.

    private class MyPan extends JPanel {
        public Dimension getPreferredSize() { return new Dimension(600, 600); }
        public void paintComponent(Graphics g) { /* do stuff */ }
    }
    

    Then just add the panel to the frame and pack(); the frame.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a simple Java function that will take a list
I'm trying to write a simple java applet program, but it seems that I'm
I'm trying to write a simple game in Java that uses Processing to render
I am trying to write a simple Http client application in Java and am
I am trying to write a simple proxy application on Android in Java to
I'm trying to write a simple paint applet with Java, but I'm having trouble
I'm trying to write a simple Java game. I have a NPC class, with
I'm trying to write a simple java program in Eclipse that prints these four
I'm trying to write a simple client/server chat application in 2 languages - Java
I'm trying to write a simple procedure that will add to a List every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.