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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:04:58+00:00 2026-05-28T06:04:58+00:00

In connection with question Resizing a component without repainting is my question how to

  • 0

In connection with question Resizing a component without repainting is my question how to create resiziable custom Graphics2d in form

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

public class ZoomWithSelectionInViewport implements MouseWheelListener {

    private JComponent b;
    private int hexSize = 3;
    private int zoom = 80;
    private JScrollPane view;

    public ZoomWithSelectionInViewport() throws Exception {
        b = new JComponent() {

            private static final long serialVersionUID = 1L;

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(700, 700);
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = ((Graphics2D) g);
                int vertOffsetX, vertOffsetY, horizOffsetX, horizOffsetY;
                vertOffsetX = (int) ((double) hexSize * Math.sqrt(3.0f));
                vertOffsetY = (int) ((double) -hexSize - 1 * Math.sqrt(3.0f) / 2.0f);
                horizOffsetX = (int) ((double) hexSize * Math.sqrt(3.0f));
                horizOffsetY = (int) ((double) hexSize + 1 * Math.sqrt(3.0f) / 2.0f);
                for (int x = 0; x < 50; x++) {
                    for (int y = 0; y < 50; y++) {
                        int[] xcoords = new int[6];
                        int[] ycoords = new int[6];
                        for (int i = 0; i < 6; i++) {
                            xcoords[i] = (int) ((hexSize + x * horizOffsetX + y * vertOffsetX)
                                    + (double) hexSize * Math.cos(i * 2 * Math.PI / 6));
                            ycoords[i] = (int) (((getSize().height / 2) + x * horizOffsetY
                                    + y * vertOffsetY) + (double) hexSize * Math.sin(i * 2 * Math.PI / 6));
                        }
                        g2d.setStroke(new BasicStroke(hexSize / 2.5f));
                        g2d.setColor(Color.GRAY);
                        g2d.drawPolygon(xcoords, ycoords, 6);
                    }
                }
            }
        };
        view = new JScrollPane(b);
        b.addMouseWheelListener(this);
        JFrame f = new JFrame();
        f.setLocation(10, 10);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(view);
        f.setPreferredSize(b.getPreferredSize());
        f.pack();
        f.setVisible(true);
    }

    public void mouseWheelMoved(MouseWheelEvent e) {
        zoom = 100 * -Integer.signum(e.getWheelRotation());
        if (hexSize - Integer.signum(e.getWheelRotation()) > 0) {
            hexSize -= Integer.signum(e.getWheelRotation());
        }
        Dimension targetSize = new Dimension(b.getWidth() + zoom, b.getHeight() + zoom);
        b.setPreferredSize(targetSize);
        b.setSize(targetSize);
        b.revalidate();
        b.repaint();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    ZoomWithSelectionInViewport example = new ZoomWithSelectionInViewport();
                } catch (Exception ex) {
                    //
                }
            }
        });
    }
}
  • 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-28T06:04:59+00:00Added an answer on May 28, 2026 at 6:04 am

    If I understand correctly, you want the scroll pane’s scroll bars to reflect the current zoom state. I see two alternatives:

    • Don’t override getPreferredSize() in the component, and adjust the preferred size in the mouse listener to include the zoomed image; it appears slightly truncated on the right.

    • Do override getPreferredSize() in the component, and adjust the returned Dimension (now a constant) to include the zoomed boundary implicit in paintComponent().

    I’d prefer the latter. I’ve also found it helpful to write explicit transformation functions to convert zoomed and un-zoomed coordinates, as shown here. An inverse AffineTransform, shown here, is also possible.

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

Sidebar

Related Questions

all is in the question.. I would know how to create a connection between
I wasn't sure how to title this question. I create a UDP connection using
To rephrase the question: should I avoid sharing instances of classes which implement java.sql.Connection
According to JAVA documentation , Connection#commit() can throw SQLException . My question is whether
The question is if a database connection should be passed in by reference or
My question is concerning SQL connection status, load, etc. based on the following code:
Brief question What command can I use to make my DataSet refresh it's connection
Simple Question: How do I grab the MAC address of the active Ethernet connection
this is a different question concerning: add a connection to database not working, asp.net
I have two tables userdetails and blog question The schema is UserDetails: connection: doctrine

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.