Dragging a Triangle on Screen

I need help in dragging the vertex of a triangle or just dragging a triangle when the user clicks on it . The code to draw the triangle is as follows. I have used a Polygon and LIne2D with Point2D to draw the lines on screen.
Please Help as i have to have this and more stuff done asap.
The code to draw the triangle on screen is as follows
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
* @author  Kripa Bhojwani
public class Triangle extends javax.swing.JPanel {
    /** Creates new form Triangle */
    public Triangle() {
        initComponents();
      Polygon triangle = new Polygon(3);
    public void paintComponent(Graphics gfx) {
        super.paintComponent(gfx); // standard setup
        Graphics2D g = (Graphics2D) gfx;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
        Polygon triangle = new Polygon(3);
        triangle.add(new Point2D.Double(150,300));
        triangle.add(new Point2D.Double(300,450));
        triangle.add(new Point2D.Double(300,300));
        g.setPaint(Color.BLACK);
        triangle.draw(g);
        // define the elements we will draw
Ellipse2D.Double e1 = new Ellipse2D.Double(p1.x-r1,p1.y-r1, 4*r1, 5*r1 );
        g.fill(e1);
        g.draw(e1);
        Point2D.Double p1 = new Point2D.Double(900, 750);
        Point2D.Double p2 = new Point2D.Double(450, 300    );
        Point2D.Double p3 = new Point2D.Double(300,300);
       /* Ellipse2D.Double e1 = new Ellipse2D.Double(p1.x-r1,p1.y-r1, 4*r1, 5*r1 );
        g.fill(e1);
        g.draw(e1);
        Line2D.Double line = new Line2D.Double(p1, p2);
        Line2D.Double line1 = new Line2D.Double(p2, p3);
        Line2D.Double line2 = new Line2D.Double(p1, p3);
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
    private void initComponents() {
        setLayout(new java.awt.BorderLayout());
    // Variables declaration - do not modify
    // End of variables declaration
}the Main Application code is on my JFrame and is as follows:
import javax.swing.*;
* @author  Kripa Bhojwani
public class GUI extends javax.swing.JFrame {
    /** Creates new form GUI */
    public GUI() {
        initComponents();
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        triangle1 = new Triangle();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItem3 = new javax.swing.JMenuItem();
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
        triangle1.setBackground(new java.awt.Color(51, 204, 255));
        triangle1.setPreferredSize(new java.awt.Dimension(500, 500));
        triangle1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                triangle1MouseClicked(evt);
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                triangle1MouseEntered(evt);
            public void mouseExited(java.awt.event.MouseEvent evt) {
                triangle1MouseExited(evt);
        triangle1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                triangle1MouseDragged(evt);
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                triangle1MouseMoved(evt);
        jScrollPane1.setViewportView(triangle1);
        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
        jMenuBar1.setBackground(new java.awt.Color(102, 153, 255));
        jMenu1.setBackground(new java.awt.Color(204, 204, 255));
        jMenu1.setText("File");
        jMenuItem1.setBackground(new java.awt.Color(255, 204, 255));
        jMenuItem1.setText("Open");
        jMenuItem1.setToolTipText("Open");
        jMenu1.add(jMenuItem1);
        jMenu1.add(jSeparator1);
        jMenuItem2.setText("Save");
        jMenuItem2.setToolTipText("Save");
        jMenu1.add(jMenuItem2);
        jMenu1.add(jSeparator2);
        jMenuItem3.setText("Exit");
        jMenuItem3.setToolTipText("X");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
        jMenu1.add(jMenuItem3);
        jMenuBar1.add(jMenu1);
        setJMenuBar(jMenuBar1);
        pack();
    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        System.exit(0);
    private void triangle1MouseMoved(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
         setTitle("Mouse is at [" + evt.getX()  + "," + evt.getY() + "]");
    private void triangle1MouseExited(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        setTitle("Mouse out of Application Window");
    private void triangle1MouseDragged(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        setTitle("Mouse Dragged at [" + evt.getX()  + "," + evt.getY() + "]");
    private void triangle1MouseEntered(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
                setTitle("Mouse is in Application Window");
    private void triangle1MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
         setTitle("Mouse clicked at [" + evt.getX()  + "," + evt.getY() + "]");
    private void formMouseDragged(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here
         //setTitle("Mouse is at [" + evt.getX()  + "," + evt.getY() + "]");
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
     * @param args the command line arguments
    public static void main(String args[]) {
        new GUI().show();
        JFrame GUI = new JFrame("GUI");
        Triangle tri = new Triangle();
        GUI.getContentPane().add(tri);
    // Variables declaration - do not modify
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private Triangle triangle1;
    // End of variables declaration
}Any help on how to drag the triangle would be greatly appreciated.
After this i need to stretch the vertex of the triangle such that i can increase the size of it.
Thanks a lot
Sharan

Hi ... I have finally managed to draw and stretch a triangle on screen as well as Increase its size on screen.
I have drawn a Right angle Triangle on screen or so it appears by the constraints set out to draw it.
Then I have enabled dragging only when the user clicks on the Left Cordinates of the BASE. (PRECISELY ON THOSE POINTS)
And stretching any side is possible only when the user clicks on the TOP or the RIght Hand side cordinates of the base.
The problem is that as and when i stretch the triangle or drag the triangle for that matter it redraws itself a number of times i.e. i can c the entire triangle moving on screen and hence it overlaps and redraws itself on screen.
Besides finding the exact cordinates of the triangle to DRAG it i.e. 300,300 for the base is difficult coz as of now thats the way i could work it out.
The code for my program is below and i would appreciate if you could sort my problems out and tellme how to drag the triangle when the user just clicks within the area of the triangle and stretches it in a way that it does not redraw the entire triangle..
Thanks...
This is my main Class
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.Graphics.*;
import java.awt.geom.Point2D;
* @author  Kripa Bhojwani
public class Geometry extends javax.swing.JFrame  {
    /** Creates new form Geometry */
    public Geometry() {
        JFrame Geometry = new JFrame("Geometry");
           initComponents();
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
    private void initComponents() {
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
        pack();
    private void formMouseDragged(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
     * @param args the command line arguments
    public static void main(String args[]) {
         TriangleModel tri = new TriangleModel(300,150,450,300,300,300);
        View view  = new View(tri);   // view takes in model
         JFrame Geometry = new JFrame("Geometry");
        Geometry.setSize(500,500);
         Geometry.setContentPane(view);
        Geometry.setVisible(true);
        Geometry.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // new Geometry().show();
    // Variables declaration - do not modify
    // End of variables declaration
This is my triangleModel class ---  the Model
* TriangleModel.java
* Created on 02 December 2004, 20:27
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.awt.Graphics;
import java.awt.Point;
* @author  Kripa Bhojwani
public class TriangleModel extends Observable {
    private int cx, cy, x1,x2,x3, y1,y2,y3;
    private int _transx;
    private int _transy;
    /** Creates a new instance of TriangleModel */
    public TriangleModel(int x1, int y1, int x2, int y2, int x3, int y3) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.x3 = x3;
        this.y3 = y3;
        setChanged();
        notifyObservers();
    public void setTop(int x1, int y1){
        this.y1= y1;
        setChanged();
        notifyObservers();
    public void setRight(int x2, int y2){
        this.x2 = x2;     
        setChanged();
        notifyObservers();
    public void setLeft(int x3, int y3){
        _transx = x3 - this.x3;
        _transy = y3 - this.y3;
        this.x3 += _transx;
        this.y3 += _transy;
        this.y2 += _transy;
        this.x2 += _transx;
        this.x1 += _transx;
        this.y1 += _transy;
        setChanged();
        notifyObservers();
    public Point getTop(){
        Point p = new Point(x1,y1);
        return p;
    public Point getRight(){
        Point p1 = new Point(x2,y2);
        return p1;
    public Point getLeft(){
        Point p3 = new Point(x3,y3);
        return p3;
This is the Controller and View
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.Graphics.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.event.*;
import java.awt.Graphics2D;
* @author  Kripa Bhojwani
public class View extends javax.swing.JPanel implements Observer, MouseMotionListener, MouseListener{
    private TriangleModel model;
    private Polygon triangle;
    private boolean pressed = false;
    private boolean pressT = false;
    private boolean pressR = false;
    /** Creates new form View */
    public View(TriangleModel model) {
        this.model = model;
        model.addObserver(this);
        addMouseListener(this);
        addMouseMotionListener(this);
        // initComponents();
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
    private void initComponents() {
        setLayout(new java.awt.BorderLayout());
    public void paintComponent(Graphics gfx) {
        Graphics2D g = (Graphics2D) gfx;
        //  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        //RenderingHints.VALUE_ANTIALIAS_ON);
        //        Polygon triangle = new Polygon(3);
        Point tc = model.getTop();
        Point lc = model.getLeft();
        Point rc = model.getRight();
        //        triangle.add(new Point2D.Double(tc.getX(), tc.getY()));
        //        triangle.add(new Point2D.Double(lc.getX(), lc.getY()));
        //        triangle.add(new Point2D.Double(rc.getX(), rc.getY()));
        Point2D.Double p1 = new Point2D.Double(tc.getX(),tc.getY());
        Point2D.Double p2 = new Point2D.Double(lc.getX(),lc.getY());
        Point2D.Double p3 = new Point2D.Double(rc.getX(),rc.getY());
        Line2D.Double line = new Line2D.Double(p1, p2);
        Line2D.Double line1 = new Line2D.Double(p2, p3);
        Line2D.Double line2 = new Line2D.Double(p1, p3);
        // g.setPaint(Color.BLACK);
        // triangle.draw(g);
        g.draw(line);
        g.draw(line2);
        g.draw(line1);
    public void update(Observable o, Object arg) {
        repaint();
    public void mouseClicked(java.awt.event.MouseEvent e) {
    public void mouseDragged(java.awt.event.MouseEvent e) {
        if(pressed == true){
            model.setLeft(e.getX() ,  e.getY());
        else if(pressT == true){
            model.setTop(e.getX() , e.getY());
        else if (pressR == true){
            model.setRight(e.getX(), e.getY());
        else {
            pressed = false;
            pressT= false;
    public void mouseEntered(java.awt.event.MouseEvent e) {
    public void mouseExited(java.awt.event.MouseEvent e) {
    public void mouseMoved(java.awt.event.MouseEvent e) {
        System.out.println(""+e.getX() + e.getY());
    public void mousePressed(java.awt.event.MouseEvent e) {
        if (model.getLeft().getX()== e.getX() && model.getLeft().getY()== e.getY()){
            pressed = true;
        else if (model.getTop().getX()==e.getX() && model.getTop().getY()==e.getY()){
            pressT = true;
        else if(model.getRight().getX() == e.getX() && model.getRight().getY()==e.getY()){
            pressR = true;
        else {
            pressed =false;
            pressT = false;
            pressR = false;
    public void mouseReleased(java.awt.event.MouseEvent e) {
        if(pressed == true){
            model.setLeft(e.getX(),e.getY());
        else if (pressT ==true ){
            model.setTop(e.getX(), e.getY());
        else if(pressR ==true){
            model.setTop(e.getX(),e.getY());
        else{
            pressed = false;
            pressT = false;
            pressR = false;
    // Variables declaration - do not modify
    // End of variables declaration
Thanks again
code]

Similar Messages

  • I have just intalled PSElements 12 on a new PC with windows 8.1. It installed ok but when I open it up it displays different than on my windows 7 PC. The buttons and text are tiny and almosy impossible to see even when I drag it to full screen. How do I a

    I have just intalled PSElements 12 on a new PC with windows 8.1. It installed ok but when I open it up it displays different than on my windows 7 PC. The buttons and text are tiny and almosy impossible to see even when I drag it to full screen. How do I adjust it to look normal?

    Hi Paul ,
    It could be a compatibility issue as well as Acrobat 8 is an older version.
    Do you get any error message while registering for the product?
    Try repairing it and once and also check for updates as well.
    Is it happening with all the word files or any specific one'?
    Regards
    Sukrit Dhingra
    Acrobat 8 and Windows 7 Don't Work

  • The screen where my apps show on all the pages is not letting me drag them. The screen looks kind of gray. What do I do to be able to move them to different home screens, and check, and uncheck the apps in the list

    The screen where my apps show on all the pages is not letting me drag them. The screen looks kind of gray. What do I do to be able to move them to different home screens, and check, and uncheck the apps in the list

    The specific demo would need to provide code for touch events.
    http://www.w3.org/TR/touch-events/

  • I am using MAC OSX 10.9.4 and LR5 Ver 5.6. I can't drag LR to my second screen. It's always worked before. All of my other apps drag to the second screen with no issues. I've re-booted my computer and it did't help. Any ideas???

    I am using MAC OSX 10.9.4 and LR5 Ver 5.6. I can't drag LR to my second screen. It's always worked before. All of my other apps drag to the second screen with no issues. I've re-booted my computer and it did't help. Any ideas???

    Make sure you are not in one of the full screen modes.
    Window > Screen Mode > Normal
    Can you see the "Traffic Lights" at the top left of the Lightroom window?

  • Why does dragging on a Touch Screen change the cursor into a Hand Tool?

    Why does dragging on a Touch Screen change the cursor into a Hand Tool? This did not happen in CS 5.5 and I could edit on my touch screen monitor. With Photoshop CS 6, as soon as I start to drag, the cursor changes to a hand cursor. I have look at all of the system options and nothing seems to affect it so I have to assume it is something wrong with Photoshop CS 6.

    Try staying with it for a while.  It is much nicer panning that way.  Give it a flick, and touch the screen again when in the right position. 
    I think it came in with CS4 BTW, but it makes a lot of sense keeping it on the Touch version.

  • Can't drag icon from customize screen to tool bar on Dell Venue 8 Pro tablet.

    On my Dell Venue 8 Pro tablet, win 8.1, I am unable to drag the 'download' (arrow) icon from the customize screen on to the tool bar.
    I was able to drag the 'search' icon from the screen to the tool bar.
    Why can I drag one, and not another?
    Thanks,
    WoodyB

    You can click the "Restore Default Set" button to restore the default toolbar setup if there is a problem with customizing the toolbars.
    You can check for problems caused by a corrupted localstore.rdf file if you continue to have problems.
    *http://kb.mozillazine.org/Corrupt_localstore.rdf

  • FF4.0 Windows XPv3 drag tab to 2nd screen, drag bar off screen at top, close other browser window, shuts down both browser instances. BTW, look at how SAFARI does the drag tab to other screen, that is how!

    FF4, drag 2nd tab to new window.
    2. top dragbar is off screen at top. I unmaximize the original screen, now that one also pushes the drag bar to the top off the screen. I resize the bottom of the screen and I can see a sliver of the top dragbar. If I close one browser window, both shut down. Auch!
    Please take a look at how safari renders a second browser window when you drag one tab to the other window. Cool!

    FF4, drag 2nd tab to new window.
    2. top dragbar is off screen at top. I unmaximize the original screen, now that one also pushes the drag bar to the top off the screen. I resize the bottom of the screen and I can see a sliver of the top dragbar. If I close one browser window, both shut down. Auch!
    Please take a look at how safari renders a second browser window when you drag one tab to the other window. Cool!

  • Yosemite Finder: bug when dragging files in full screen

    I use my Finder in full screen mode.
    When I drag files through some folders, the entire content of the window moves up so that I cannot see the top files in any column anymore.
    The only solution to this bug is to end full screen and then start full screen again.
    Is this really a bug or a function I do not understand? And how can I prevent this from happening, because it is really annoying?
    I encountered this bug on my old iMac 27" and my new iMac 5K.
    Attached to this post is a screenshot what it looks like.

    Send Apple feedback. They won't answer, but at least will know there is a problem. If enough people send feedback, it may get the problem solved sooner.
    Feedback
    Or you can use your Apple ID to register with this site and go the Apple BugReporter. Supposedly you will get an answer if you submit feedback.
    Feedback via Apple Developer

  • Select does not open drag option on Internet screen

    After downloading program, leaf on explorer bar opens Smart Web Printing screen on left but Select button does NOTHING!   I select this option and move to Internet Explorer screen but select option does not open any box or show any controls on screen when drag used.  There is no effect at all.

    I hope someone has an answer to this soon.  I have the same problem.

  • Cannot drag and drop when screen sharing

    When i try to drag and drop files, whilst i'm screen sharing with my mac at work, i just get a message in the file transfer window, saying that 'ichat waiting for mac to accept file transfer'. But i don't get a dialogue box asking me to accept the file. this happens both ways, so when i drag a file from my macbook to my mac mini at work nothing happens and vice-versa.
    I have the mac at work set up to auto accept file transfers and screen sharing. The annoying thing is, that when i take my macbook to work, and both machines are sharing the same connection, the file transferring works perfectly.
    I'm assuming its something to do with my router at home, but i cant work out what i need to do. anyone got any ideas?
    Both macs are running iChat 4.0.5 and are using google accounts.

    upgrade your browser to Firefox 8 and check
    * getfirefox.com

  • Dragging icons to other screens is difficult

    I am having problems trying to rearrange screens on my LG G2 with KitKat.  I can't drag an icon to a screen to the left any more only to the right and when I move it to the right side to cause the screens to rotate the screens switch very fast making it difficult to drop it on the screen I wish to move it to.  I didn't use to have this issue before.  I'm not sure if it started with the KitKat upgrade or not.  Anyone else have this issue?

    In regards to this issue, as well as your last battery related post you should reset and wipe.  Otherwise go to home screen, press menu, add apps or widgets, scroll to the screen you want the app on, then scroll to app in bottom half of screen and pick app.  It will go to the chosen screen.

  • Drag windows across two screens?

    SO newbie to MOTION 5  = how can I set up a custom layout for workspace? I want to drag this or that window and place across my two screens...

    Motion is only one window, not several separate windows.  You can go to the Window menu and set it to show either the Canvas or the Timing Pane on the second monitor.

  • Delete Airdisplay, iScreen, etc to solve drag and drop, white screen, etc issues

    Deleting the screensharing program (in my case iscreen, guess airdisplay, etc are the same) did solve all my issues -- as mentioned before drag and drop did not work, white screen in several programs, not menu bar in full screen, etc.
    Lion is now running smoothly - so everybody having the same issue just delete airdisplay and co :-))))))

    I would:
    a) check on the email provider's website to check if it had copied there, and deal with it. Your mention of bandwidth implies this is an IMAP-connected account.
    b) close Thunderbird, open its [https://support.mozilla.org/en-US/kb/profiles-tb?esab=a&s=profiles&r=1&as=s profile] and delete the offending folder there. You need to browse your profile for a while to understand its structure before doing anything rash.
    On the whole, I discourage users from dragging folders about as you have done. By all means drag the ''contents'' (better yet, use multi-select, then right-click and "copy to" or "move to") but moving ''folders'' is IMHO an undefined operation with unpredictable outcomes. I've heard of users moving a folder from one account to another and finding that it still associates with (i.e. "belongs to") the original account.

  • IPhone 4 app icon won't drag to the left screen

    I accidentally moved an icon over to a new screen on the right.  When I try to move it back to the left, it won't move.

    To add to Chris' comments, when you drag it to the side of the screen...
    drag until about 1/2 of the icon is off screen
    wait until the screen switches
    position as desired on the new screen
    press the home button

  • When drag the progress indicator screen become blank

    Hi
    I have made the simulation with captivate 4 around 45 slide and publish into the swf format and publish swf file load in the flash 8 move with his own control. When I drag the progress bar indicator screen become blank. Please help me.
    Regards,
    Manish

    When you drag the progress indicator, it pauses the playback.
    So it could be that you've dragged to a point where your objects from one slide have faded out but the ones from the next haven't started to fade in yet.
    Clicking the Play button on the playbar should get you going again, if that's what's happpened.

Maybe you are looking for