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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:11:45+00:00 2026-06-18T03:11:45+00:00

So I have the following code: import java.awt.*; public class Triangle extends Point {

  • 0

So I have the following code:

import java.awt.*;

public class Triangle extends Point {
    protected int size;
    private int p1x, p2x, p3x, p1y, p2y, p3y;

    //Create a new triangle
    public Triangle()
    {
        super();
        size = 100;
        yPos = 100;
    }


    public void reSize(int newSize) {
        size = size + newSize;
    }

    //Draw the triangle with current specifications on screen.
    public void display(Graphics g) {
        p1x = (size / 3);
        p2x = (size / 2);
        p3x = ((2 * size) / 3);
        p1y = ((2 * size) / 3);
        p2y = (size / 3);
        p3y = ((2 * size) / 3);
        int[] xPoints = { p1x, p2x, p3x };
        int[] yPoints = { p1y, p2y, p3y };
        int npoints = 3;
        g.fillPolygon(xPoints, yPoints, npoints);
    }
}

And the following code is used to enlarge/reduce the triangle:

    if (e.getSource() == makeBigger) {
          aShape.reSize(20);
    }

    else if (e.getSource() == makeSmaller) {
          aShape.reSize(-20);
    }

The problem is, I also want the ability to drag it using the following code:

    this.addMouseListener(new MouseAdapter() {
    public void mouseReleased(MouseEvent me) {
        shapeColor = Color.black;
        repaint();
    }

    public void mouseDragged(MouseEvent me) {
        shapeColor = Color.lightGray;
        aShape.moveXY(me.getX(), me.getY());
        repaint();
    }
});

    this.addMouseMotionListener(new MouseAdapter() {
    public void mouseReleased(MouseEvent me) {
        shapeColor = Color.black;
        repaint();
    }

    public void mouseDragged(MouseEvent me) {
        shapeColor = Color.lightGray;
        aShape.moveXY(me.getX(), me.getY());
        repaint();
    }
});

And the only way I can see it is that I can have either one or the other. How do I combine it so I can have both features? At the moment it resizes perfectly, but say I change the variablle size in the x/y coordinates to yPos/xPos I’ll be able to drag the triangle but not reshape it.

Thanks!

  • 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-18T03:11:46+00:00Added an answer on June 18, 2026 at 3:11 am

    You’ll want to add xOffset and yOffset members and add those in when calculating the corner points:

    public class Triangle extends Point {
        protected int size;
        private int p1x, p2x, p3x, p1y, p2y, p3y;
        private int xOffset=0;
        private int yOffset=0;
    
        // ...
    
        //Draw the triangle with current specifications on screen.
        public void display(Graphics g) {
            p1x = (size / 3) + xOffset;
            p2x = (size / 2) + xOffset;
            p3x = ((2 * size) / 3) + xOffset;
            p1y = ((2 * size) / 3) + yOffset;
            p2y = (size / 3) + yOffset;
            p3y = ((2 * size) / 3) + yOffset;
            int[] xPoints = { p1x, p2x, p3x };
            int[] yPoints = { p1y, p2y, p3y };
            int npoints = 3;
            g.fillPolygon(xPoints, yPoints, npoints);
        }
    
        public void moveXY(int deltaX, int deltaY) {
            xOffset += deltaX;
            yOffset += deltaY;
        }
    }
    

    This should let you translate the polygon. Note however that the moveXY method stated above expects parameters containing the difference from earlier, not an absolute position. If you want an absolute position instead, that’s easy to achieve and left as an exercise. 😉

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

Sidebar

Related Questions

please have a look at the following code import java.util.ArrayList; import java.util.List; public class
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
Please have a look the following code import javax.swing.*; import java.awt.event.*; import java.awt.*; public
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code import java.awt.event.*; import java.awt.*; import javax.swing.*;
Please have a look at the following code import java.awt.event.*; import javax.swing.*; import java.awt.*;
Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout;

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.