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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:29:53+00:00 2026-06-05T20:29:53+00:00

I have a problem with 2D transformation program I have the source code import

  • 0

I have a problem with 2D transformation program

I have the source code

import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class House extends JPanel {

    MyCanvas canvas;
    JSlider sliderTransX, sliderTransY, sliderRotateTheta, sliderRotateX,
        sliderRotateY, sliderScaleX, sliderScaleY, sliderWidth;
    double transX = 0.0;
    double transY = 0.0;
    double rotateTheta = 0.0;
    double rotateX = 150.0;
    double rotateY = 150.0;
    double scaleX = 1.0;
    double scaleY = 1.0;
    float width = 1.0f;

    public House() {
        super(new BorderLayout());
        JPanel controlPanel = new JPanel(new GridLayout(3, 3));
        add(controlPanel, BorderLayout.NORTH);

        controlPanel.add(new JLabel("Translate(dx,dy): "));

        sliderTransX = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150,
            100, 50);
        sliderTransY = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150,
            100, 50);

// To control rotation
        controlPanel.add(new JLabel("Rotate(Theta,ox,oy): "));
        sliderRotateTheta = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 360,
            0, 90, 45);

        JPanel subPanel = new JPanel();
        subPanel.setLayout(new GridLayout(1, 2));

        sliderRotateX = setSlider(subPanel, JSlider.HORIZONTAL, 0, 300, 150,
            150, 50);

        sliderRotateY = setSlider(subPanel, JSlider.HORIZONTAL, 0, 300, 150,
            150, 50);
        controlPanel.add(subPanel);

// To control scaling
        controlPanel.add(new JLabel("Scale(sx,sy)x10E-2:"));

        sliderScaleX = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 200, 100,
            100, 10);

        sliderScaleY = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 200, 100,
            100, 10);

// To control width of line segments
        JLabel label4 = new JLabel("Width Control:", JLabel.RIGHT);
        sliderWidth = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
        sliderWidth.setPaintTicks(true);
        sliderWidth.setMajorTickSpacing(5);
        sliderWidth.setMinorTickSpacing(1);
        sliderWidth.setPaintLabels(true);
        sliderWidth.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                width = sliderWidth.getValue();
                canvas.repaint();
            }
        });
        JPanel widthPanel = new JPanel();
        widthPanel.setLayout(new GridLayout(1, 2));
        widthPanel.add(label4);
        widthPanel.add(sliderWidth);
        add(widthPanel, BorderLayout.SOUTH);

        canvas = new MyCanvas();
        add(canvas, "Center");
    }

    public JSlider setSlider(JPanel panel, int orientation, int minimumValue,
        int maximumValue, int initValue, int majorTickSpacing,
        int minorTickSpacing) {
        JSlider slider = new JSlider(orientation, minimumValue, maximumValue,
            initValue);
        slider.setPaintTicks(true);
        slider.setMajorTickSpacing(majorTickSpacing);
        slider.setMinorTickSpacing(minorTickSpacing);
        slider.setPaintLabels(true);
        slider.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                JSlider tempSlider = (JSlider) e.getSource();

                if (tempSlider.equals(sliderTransX)) {
                    transX = sliderTransX.getValue() - 150.0;
                    canvas.repaint();
                } else if (tempSlider.equals(sliderTransY)) {
                    transY = sliderTransY.getValue() - 150.0;
                    canvas.repaint();
                } else if (tempSlider.equals(sliderRotateTheta)) {
                    rotateTheta = sliderRotateTheta.getValue() * Math.PI / 180;
                    canvas.repaint();
                } else if (tempSlider.equals(sliderRotateX)) {
                    rotateX = sliderRotateX.getValue();
                    canvas.repaint();
                } else if (tempSlider.equals(sliderRotateY)) {
                    rotateY = sliderRotateY.getValue();
                    canvas.repaint();
                } else if (tempSlider.equals(sliderScaleX)) {
                    if (sliderScaleX.getValue() != 0.0) {
                        scaleX = sliderScaleX.getValue() / 100.0;
                        canvas.repaint();
                    }
                } else if (tempSlider.equals(sliderScaleY)) {
                    if (sliderScaleY.getValue() != 0.0) {
                        scaleY = sliderScaleY.getValue() / 100.0;
                        canvas.repaint();
                    }
                }
            }
        });
        panel.add(slider);

        return slider;
    }

    class MyCanvas extends Canvas {

        public void paint(Graphics g) {
            Graphics2D g2D = (Graphics2D) g;

            g2D.translate(transX, transY);
            g2D.rotate(rotateTheta, rotateX, rotateY);
            g2D.scale(scaleX, scaleY);

            BasicStroke stroke = new BasicStroke(width);
            g2D.setStroke(stroke);

            drawHome(g2D);
        }

        public void drawHome(Graphics2D g2D) {
            Line2D line1 = new Line2D.Float(100f, 200f, 200f, 200f);
            Line2D line2 = new Line2D.Float(100f, 200f, 100f, 100f);
            Line2D line3 = new Line2D.Float(100f, 100f, 200f, 100f);
            Line2D line5 = new Line2D.Float(200f, 100f, 200f, 200f);

            g2D.draw(line1);
            g2D.draw(line2);
            g2D.draw(line3);
            g2D.draw(line5);
        }
    }

    public static void main(String[] a) {
        JFrame f = new JFrame();
        f.getContentPane().add(new House());
        f.setDefaultCloseOperation(1);
        f.setSize(700, 550);
        f.setVisible(true);
    }
}

Please help me.

  • 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-05T20:29:54+00:00Added an answer on June 5, 2026 at 8:29 pm

    Don’t mix AWT and Swing components needlessly: extend JPanel and override paintComponent(). A call to super.paintComponent(g) will clean up rendering, and RenderingHints will improve rotated drawing.

    class MyCanvas extends JPanel {
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2D = (Graphics2D) g;
            g2D.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    
            g2D.translate(transX, transY);
            g2D.rotate(rotateTheta, rotateX, rotateY);
            g2D.scale(scaleX, scaleY);
    
            BasicStroke stroke = new BasicStroke(width);
            g2D.setStroke(stroke);
    
            drawHome(g2D);
        }
    ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a problem with XSLT transformation. I have input XML as
I have a problem where the fog works like intended on a desktop program
I have following problem that in a transformation (which is completely done in memory)
I have a problem with namespace position in result xml file after xsl transformation.
I have a problem with changing the properties of the SCD transformation in SSIS
I have a pretty annoying problem. I would like to create a drawing program,
I have the problem under Chrome/Safari browsers. After applying CSS3 transformation I don't receive
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are

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.