Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing an equilateral triangle that stays equilateral as it's moved
    primarykey
    data
    text
    <p>I'm trying to create a program that will draw an equilateral triangle with an oval at the center point when the mouse is pressed, and while the mouse is being dragged, the triangle will redraw itself so that the triangle remains equilateral regardless of where the mouse is (the mouse acts as one of the corners of the triangle). I have the following code so far.</p> <pre><code>public class EquilateralRubberBand extends JFrame { /** * */ public EquilateralRubberBand() { super("Equilateral Triangle Rubber Band"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBackground(Color.white); setContentPane(new DrawTrianglePanel()); setSize(400, 400); // you can change this size but don't make it HUGE! setVisible(true); } /** * */ private class DrawTrianglePanel extends JPanel implements MouseListener, MouseMotionListener { final float dash[] = { 5.0f }; final BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash, 0.0f); final BasicStroke solid = new BasicStroke(1.0f); final int sizeOfCenter = 5; final int sizeOfRectangles = 10; private String displayString; private boolean start; private Point centerPoint; private Point p1; private Point p2; private Point p3; /** */ public DrawTrianglePanel() { addMouseListener(this); addMouseMotionListener(this); } /** * * @param g the graphics context with which we paint into. */ public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2; g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (start) { g2.setColor(Color.RED); g2.fillOval(centerPoint.x, centerPoint.y, sizeOfCenter, sizeOfCenter); g2.setColor(Color.BLUE); g2.drawRect(p1.x, p1.y, sizeOfRectangles, sizeOfRectangles); g2.drawRect(p2.x, p2.y, sizeOfRectangles, sizeOfRectangles); g2.drawRect(p3.x, p3.y, sizeOfRectangles, sizeOfRectangles); g2.drawString(displayString, 50, 25); g2.setStroke(dashed); g2.drawLine(p1.x, p1.y, p2.x, p2.y); g2.drawLine(p1.x, p1.y, p3.x, p3.y); g2.drawLine(p2.x, p2.y, p3.x, p3.y); } } public void mousePressed(MouseEvent e) { start = true; centerPoint = e.getPoint(); doCoordinateCalculations(e.getPoint()); displayString = "Pressed: " + centerPoint.x + ", " + centerPoint.y; repaint(); } public void mouseReleased(MouseEvent e) { Point currentPoint = e.getPoint(); displayString = "Released: " + currentPoint.x + ", " + currentPoint.y; repaint(); } public void mouseDragged(MouseEvent e) { Point currentPoint = e.getPoint(); displayString = "Dragging: " + currentPoint.x + ", " + currentPoint.y; doCoordinateCalculations(currentPoint); repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } public void doCoordinateCalculations(Point point) { double distanceFromCenter; double angleP1; double angleP2; double angleP3; double v2x; double v2y; double v3x; double v3y; double p2x; double p2y; double p3x; double p3y; p1 = point; distanceFromCenter = Math.sqrt((Math.pow((p1.x - centerPoint.x), 2) + Math.pow((p1.y - centerPoint.y), 2))); angleP1 = Math.atan2(p1.y - centerPoint.y, p1.x - centerPoint.x); angleP1 = ((2 * Math.PI)/3) - angleP1; angleP2 = angleP1 + ((2 * Math.PI)/3); v2x = distanceFromCenter * Math.cos(angleP2); v2y = distanceFromCenter * Math.sin(angleP2); p2x = v2x + centerPoint.x; p2y = v2y + centerPoint.y; p2 = new Point((int)p2x, (int)p2y); angleP3 = angleP1 - ((2 * Math.PI)/3); v3x = distanceFromCenter * Math.cos(angleP3); v3y = distanceFromCenter * Math.sin(angleP3); p3x = v3x + centerPoint.x; p3y = v3y + centerPoint.y; p3 = new Point((int)p3x, (int)p3y); } } /** * */ public static void main(String[] args) { new EquilateralRubberBand(); } }; </code></pre> <p>I've got the code to draw the triangle but when I try to move it, instead of staying equilateral and just spinning around the center , it shifts its shape and is able to move around the screen. I'm assuming there is something wrong with my math but I can't find the problem. Anybody have any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload