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 7520727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:07:07+00:00 2026-05-30T02:07:07+00:00

I have this basic Java application in witch dim_x and dim_y represent the dimensions

  • 0

I have this basic Java application in witch dim_x and dim_y represent the dimensions of the window and the canvas within it. How can I get these values to change as the user changes the size of the window so that what is drawn on the canvas shrinks/expands accordingly?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MLM extends Canvas {
    static int dim_x = 720;
    static int dim_y = 480;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Canvas canvas = new MLM();
        canvas.setSize(dim_x, dim_y);
        frame.getContentPane().add(canvas);

        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g) {
        // some stuff is drawn here using dim_x and dim_y
    }
}

EDIT:
following Binyamin’s answer I’ve tried adding this which works, but is there a better way to do it? As in, with not making canvas static, maybe?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MLM extends Canvas {
    static int dim_x = 720;
    static int dim_y = 480;
    static Canvas canvas;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        canvas = new MLM();
        canvas.setSize(dim_x, dim_y);
        frame.getContentPane().add(canvas);

        frame.pack();
        frame.setVisible(true);

        frame.addComponentListener(new ComponentListener(){
            public void componentResized(ComponentEvent e) {
                Dimension d = canvas.getSize();
                dim_x = d.width;
                dim_y = d.height;
            }
            public void componentHidden(ComponentEvent e) {}
            public void componentMoved(ComponentEvent e) {}
            public void componentShown(ComponentEvent e) {}
        });
    }

    public void paint(Graphics g) {
        // some stuff is drawn here using dim_x and dim_y
    }
}
  • 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-05-30T02:07:08+00:00Added an answer on May 30, 2026 at 2:07 am

    Add a component listener, and implement componentResized. Look here.

    frame.addComponentListener(new ComponentListener(){
        @Override
        public void componentResized(ComponentEvent e) {
            //Get size of frame and do cool stuff with it   
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this seems pretty basic but I can't get it to work. I have
I have developed a basic Chat application in Java. It consists of a server
We have a Java web application and we'd like to set up some basic
Following this example in the Java(tm) tutorials, I have implemented this basic one producer
Sorry for english this might be very basic Question, I have an Application where
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
I realize this is a basic question but I have searched online, been to
I'm a noob so this is probably basic stuff, but I have tried to
I have some basic idea on how to do this task, but I'm not
I have a basic website nav layout that looks like this: <li class=folder parent_folder>

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.