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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:28:10+00:00 2026-06-01T15:28:10+00:00

My program involves drawing triangles where i click them. There are two classes, Ecad

  • 0

My program involves drawing triangles where i click them.

There are two classes, Ecad and Line class. Ecad is the main frame and Line class is for drawing lines.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Ecad extends JFrame implements MouseListener{
    ArrayList<Line2> lines=new ArrayList();
    public Ecad(){
        this.setVisible(true);
        this.setSize(600,400);
        this.addMouseListener(this);
    }
    public void mouseReleased(MouseEvent me){
        Point p1,p2,p3;
        int X=me.getX();
        int Y=me.getY();
        p1=new Point(X,Y);                                                       
        p2=new Point((int)(p1.getX()-100),(int)(p1.getY()+(1.732/2*200)));
        p3=new Point((int)(p1.getX()+100),(int)(p1.getY()+(1.732/2*200)));                                          
        Line2 l1=new Line2(p2,p1);
        Line2 l2=new Line2(p1,p3);
        Line2 l3=new Line2(p2,p3);
        lines.add(l1);
        lines.add(l2);
        lines.add(l3);
        this.repaint();
    }
     public void mouseClicked(MouseEvent me){

    }
    public void mouseExited(MouseEvent me){

    }
    public void mouseEntered(MouseEvent me){

    }
    public void mousePressed(MouseEvent me){
    }
    public void mouseMoved(MouseEvent me){

    }
    public static void main(String args[]){
        new Ecad();
    }
    public void paint(Graphics g){
        Graphics2D g2=(Graphics2D)g;
        super.paintComponents(g2);
        //g2.scale(0.5, 0.5);
        for(final Line2 r:lines){
            r.paint((Graphics2D)g2);
        }
    }
}

This is the Line class

import java.awt.*;
public class Line2 {
    Point start,end;
    public Line2(Point a,Point b){    
        start=a;
        end=b;   
    }
    public void paint(Graphics2D g){
        g.drawLine((int)start.getX(),(int)start.getY(),(int)end.getX(),(int)end.getY());
    }

}

In the Ecad class’s paint() method, if i use the scale option to zoom in or out, the mouse co-ordinates does not get transformed. So after it is zoomed, if i click at one point, the triangle gets placed at some other point. Is there a way to transform the mouse co-ordinates as well when i scale the Graphics component??

  • 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-01T15:28:13+00:00Added an answer on June 1, 2026 at 3:28 pm

    Again, you should translate your shape that you’re drawing using the scale and a fixed point (again here it appears that the fixed point of each shape would be the apex of the triangle, but it could be the center should you so decide. The translation is based on simple geometric principles and would be the fixedPoint.x * (1 – scale) / scale, and the same for the y translation.

    For example (and this one uses a JPanel as your example above should):

    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.*;
    import java.util.List;
    
    class EcadB extends JPanel {
       private static final int PREF_W = 800;
       private static final int PREF_H = 600;
       private List<MyShape> myShapes = new ArrayList<MyShape>();
       private double scale = 1.0;
       private JSlider slider = new JSlider(0, 200, 100);
    
       public EcadB() {
          addMouseListener(new MyMouseAdapter());
          setLayout(new BorderLayout());
    
          slider.setOpaque(false);
          slider.setMajorTickSpacing(20);
          slider.setMinorTickSpacing(10);
          slider.setPaintLabels(true);
          slider.setPaintTicks(true);
          slider.addChangeListener(new SliderChangeListener());
          add(slider, BorderLayout.SOUTH);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
          Graphics2D g2b = (Graphics2D)g2.create();
          g2b.scale(scale, scale);
          for (MyShape myShape : myShapes) {
             myShape.draw(g2b, scale);
          }
          g2b.dispose();
       }
    
       public void setScale(double scale) {
          this.scale = scale;
          repaint();
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private class MyMouseAdapter extends MouseAdapter {
          @Override
          public void mousePressed(MouseEvent e) {
             Point2D p1 = e.getPoint();
             Point2D p2 = new Point((int) (p1.getX() - 100),
                   (int) (p1.getY() + (1.732 / 2 * 200)));
             Point2D p3 = new Point((int) (p1.getX() + 100),
                   (int) (p1.getY() + (1.732 / 2 * 200)));
             Path2D path = new Path2D.Double();
             path.moveTo(p1.getX(), p1.getY());
             path.lineTo(p2.getX(), p2.getY());
             path.lineTo(p3.getX(), p3.getY());
             path.lineTo(p1.getX(), p1.getY());
    
             myShapes.add(new MyShape(path, p1));
             repaint();
          }
       }
    
       private class SliderChangeListener implements ChangeListener {
          @Override
          public void stateChanged(ChangeEvent arg0) {
             double value = slider.getValue() / 100.0;
             setScale(value);
          }
       }
    
       public static void main(String args[]) {
          // new Ecad();
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                EcadB ecadB = new EcadB();
                JFrame frame = new JFrame("Scale");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(ecadB);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
             }
          });
       }
    }
    
    class MyShape {
       Shape shape;
       Point2D fixedPoint;
    
       public MyShape(Shape shape, Point2D fixedPoint) {
          this.shape = shape;
          this.fixedPoint = fixedPoint;
       }
    
       public void draw(Graphics2D g2, double scale) {
          Graphics2D g2b = (Graphics2D) g2.create();
          double tx = fixedPoint.getX() * (1.0 - scale) / scale;
          double ty = fixedPoint.getY() * (1.0 - scale) / scale;
          g2b.translate(tx, ty);
          g2b.draw(shape);
          g2b.dispose();
       }
    }
    

    Note that I make copies of my Graphics object before transforming them so as to not have the transform effect other objects that may be drawn by the Graphic object. For example if you get rid of the Graphics2D copy that I use in the JPanel’s paintComponent(...) method, you’ll find the JSlider gets scaled with everything else.

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

Sidebar

Related Questions

Almost any mature program that involves text implements double click to select the word
I'm working on a program for class that involves solving the Chinese Postman problem
So I'm working on a program that involves two datatypes: a linked list and
I'm using MinGW, which is gcc for Windows. My program involves multiple windows, two
I'm writing a program that involves playing back sine waves and combinations of sine
I have a speed critical multithreaded program which involves data in a tree structure.
I'm writing a C/C++ program that involves putting a hex representation of a number
I made this program for a school, and it involves a telnet server. When
I have a program that wants to be called from the command line many
I am trying to program a game in which I have a Table class

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.