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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:22:52+00:00 2026-06-11T21:22:52+00:00

I’m programming a java applet. In this applet I need to draw on an

  • 0

I’m programming a java applet.

In this applet I need to draw on an image some markers (as a red circle) and some lines.

I have succesfully implemented the markes, as an extension of JComponent, and I also put on this some mouse listeners.

I’m having big problems with the line object. I created another object extending JComponent, and, aside that I’m having some problems with the coordinates system, the setDimension creates troubles. For example it intercepts all the marker’s click.

It isn’t a method to make an object “dimension” more tight to the line, because I can’t draw only verical or horizontal lines…

Thank to all of you.

EDIT

public class Path extends JComponent {
...
    // stroke of the line
    private Stroke spessore =  new BasicStroke(SPESSORE);

    // coordinates
    private double x, y, x_2, y_2;

// ZoomManager is an object. In this project I can zoom in and zoom out the
    // image, so this object convert coordinates get on the superior JPanel in 
    // coordinates on the image real-sized.
    public Path(double x, double y, ZoomManager zoom) {//, double x_2, double y_2, ZoomManager zoom) {
            super();

            // this function return the coordinates on the real-sized image
            Point a = DrawableObjects.getScaledCoordinates(x, y, zoom);
            this.x = a.x;
            this.y = a.y;

            this.x_2 = a.x;
            this.y_2 = a.y;

            updateBoundsAndSize(zoom);

            // this was only for test...
            this.addMouseListener(new MouseListener(){

        @Override
        public void mouseClicked(MouseEvent arg0) {
            System.out.println("CLICK!");
                    }
                    ...
            });
    }

    // this function is called during the mouse dragging for drow the line.
    // it gets the coordinates, convert them, save them and update the bounds and 
    // size of the object
    public void setArrivePoint(Point a, ZoomManager zoom) {
            Point p = DrawableObjects.getScaledCoordinates(a.x, a.y, zoom);
            this.x_2 = p.x;
            this.y_2 = p.y;
            updateBoundsAndSize(zoom);
    }

    // update the bounds of the object, the origin point of the rectangle is the
    // top-left coordinate build with the original coordinates. The width and height of the rectangle are obtained by subtraction. 
    private void updateBoundsAndSize(ZoomManager zoom) {

            Point p = DrawableObjects.getPanelCoordinates(x, y, zoom);
            Point a = DrawableObjects.getPanelCoordinates(x_2, y_2, zoom);

            int min_x = (int)Math.min(p.x, a.x) - SPESSORE;
            int min_y = (int)Math.min(p.y, a.y) - SPESSORE;

            if (min_x < 0)
                    min_x =0;

            if (min_y < 0)
                    min_y = 0;

            int w = (int) (Math.max(a.x, p.x) - min_x) + SPESSORE;
            int h = (int) (Math.max(a.y, p.y) - min_y) + SPESSORE;

            setBounds(new Rectangle(min_x, min_y, w, h));
            repaint();
    }

    // drawing function   
    @Override
    protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D antiAlias = (Graphics2D) g;
            antiAlias.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // get ZoomManager from the superior object
            ZoomManager zoom = ((JPanelImmagine)this.getParent()).zoom;

            antiAlias.setColor(DEFAULT_COLOR);
            antiAlias.setStroke(spessore);

            Point[] coordinates = updateCoordinates(zoom);

            Line2D line = new Line2D.Double(coordinates[0], coordinates[1]);

            antiAlias.draw(line);

    }

    // translate coordinates from superior jpanel to this object
    private Point[] updateCoordinates(ZoomManager zoom) {

            Point[] output = new Point[2];

            Point p = DrawableObjects.getScaledCoordinates(x, y, zoom);
            Point a = DrawableObjects.getScaledCoordinates(x_2, y_2, zoom);

            double o_x = this.getBounds().getCenterX();
            double o_y = this.getBounds().getCenterY();
            Point origin = new Point ((int)o_x, (int)o_y);

            output[0] = calculateCoordinates(p, origin);
            output[1] = calculateCoordinates(a, origin);

            return output;
    }

    private Point calculateCoordinates(Point p, Point origin) {

            double new_x = p.x - origin.x;
            double new_y = p.y - origin.y;

            return new Point((int)new_x, (int)new_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-06-11T21:22:54+00:00Added an answer on June 11, 2026 at 9:22 pm

    Resolved using this method!

    I had to change totally approach to the problem.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.