Select java graph and drag them

I have a class of the House where I built a House of java graphic design (rectangle and lines). I have a SelectAndDragTest class, which is the main class. When I run this class, it will generate a House on the JPanel. It is movable. But when I click on the button to add another House, I can't drag the House newly added, but not previous homes. How that other House is selectable on mouse down and movable? I would be grateful if anyone could give suggestions. Here are my codes:

SelectAndDragTest.java

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class SelectAndDragTest extends JPanel {


  private static final String TITLE = "House";
  private static final int W = 640;
  private static final int H = 480;
  public static Random random = new Random();
  public static Point objPt = new Point(W / 2, H / 2);
  private Point mousePt;
  House house;
  public static  JButton btnAdd;


  public SelectAndDragTest() {
  this.setFont(new Font("Sans Serif", Font.BOLD, 12));
  this.addMouseListener(new MouseAdapter() {


  @Override
  public void mousePressed(MouseEvent e) {
  mousePt = e.getPoint();
  repaint();
  }
  });
  this.addMouseMotionListener(new MouseMotionAdapter() {


  @Override
  public void mouseDragged(MouseEvent e) {
  int dx = e.getX() - mousePt.x;
  int dy = e.getY() - mousePt.y;
  objPt.setLocation(objPt.x + dx, objPt.y + dy);
  mousePt = e.getPoint();
  repaint();
  }
  });


  btnAdd = new JButton("Add new house");
  btnAdd.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  house = new House(objPt.x, objPt.y);
  addHouse(house);


  }
  });
  this.add(btnAdd);
  }




  @Override
  public Dimension getPreferredSize() {
  return new Dimension(W, H);
  }


  List<House> houses = new ArrayList<House>();
  @Override
  public void paintComponent(Graphics g) {
  super.paintComponent(g);
  for (House h:houses) {
  h.paint(g);
  }
  House onehouse = new House(objPt.x, objPt.y);
  onehouse.paint(g);
  }


  public void addHouse(House house) {
  houses.add(house);


  repaint();
  }




  public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
  @Override
  public void run() {
  JFrame f = new JFrame(TITLE);
  f.add(new SelectAndDragTest());
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.pack();
  f.setLocationRelativeTo(null);
  f.setVisible(true);
  }
  });
  }
}

House.Java

import java.awt.Graphics;


public class House {


  int x,y;

  public House(int x, int y) {
  this.x = x;
  this.y = y;
  }


  public void paint (Graphics page) {
page.drawRect(x, y, 40, 40);
  page.drawLine(x, y, x+20, y-20);
  page.drawLine(x+40, y, x+20, y-20);
}
}

OK, here's your project with minimal changes, I could do so he can do what you originally described - choose any home on the screen and move.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;  

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;  

public class SelectAndDragTest_lm extends JPanel {
  private static final String TITLE = "House";
  private static final int W = 640;
  private static final int H = 480;
  public static Random random = new Random();
  public static Point objPt = new Point(W / 2, H / 2);
  private Point mousePt;
  House house;
  public static  JButton btnAdd;  

  public SelectAndDragTest_lm() {
    this.setFont(new Font("Sans Serif", Font.BOLD, 12));
    this.addMouseListener(new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent e) {
        mousePt = e.getPoint();
        for (House h:houses) {
          h.select(mousePt.x, mousePt.y); //check to see if selected--Houe will know
        }
      }
      public void mouseReleased(MouseEvent e) {
        for (House h:houses) h.unselect(); //button up--nobody should move any more
      }
    });
  this.addMouseMotionListener(new MouseMotionAdapter() {  

      @Override
      public void mouseDragged(MouseEvent e) {
      mousePt = e.getPoint();
      for (House h:houses) h.move(mousePt.x, mousePt.y); //tell Houses to move--they will know if they should
      repaint();
      }
      });  

      btnAdd = new JButton("Add new house");
      btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          house = new House(objPt.x, objPt.y);
          addHouse(house);
        }
      });
      this.add(btnAdd);
    }  

      @Override
      public Dimension getPreferredSize() {
      return new Dimension(W, H);
      }  

      List houses = new ArrayList();
      @Override
      public void paintComponent(Graphics g) {
      super.paintComponent(g);
      for (House h:houses) {
      h.paint(g);
      }
      //House onehouse = new House(objPt.x, objPt.y);
      //onehouse.paint(g);
      }  

      public void addHouse(House house) {
      houses.add(house);  

      repaint();
      }  

      public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
      JFrame f = new JFrame(TITLE);
      f.add(new SelectAndDragTest_lm());
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.pack();
      f.setLocationRelativeTo(null);
      f.setVisible(true);
      }
      });
      }
    }

//-----------------------House
import java.awt.Graphics;
import java.awt.Polygon;

public class House extends Polygon{
  int x,y;
  int tx, ty;
  boolean isSelected = false;
  public House(int x, int y) {
    this.x = x;
    this.y = y;
    this.addPoint(x, y);
    this.addPoint(x, y - 20);
    this.addPoint(x - 10, y - 30);
    this.addPoint(x - 20, y - 20);
    this.addPoint(x - 20, y);
    isSelected = true;
    move(x, y);
    isSelected = false;
  }
  public void select(int x, int y){
    if(this.contains(x, y)) isSelected=true;
  }
  public void unselect(){
    isSelected = false;
  }
  public void move(int x, int y) {
    if(isSelected) {
      tx = x;
      ty= y;
      translate(tx-this.x, ty-this.y);
      this.x = tx;
      this.y = ty;
    }
  }
  public void paint(Graphics g) {
    g.drawPolygon(this);
    g.fillPolygon(this); //uncomment this line to fill the house
  }
}

Tags: Java

Similar Questions

  • No preview of the clip when I select the side and drag (as it was in the program monitor) now, it shows that what is in order at this time? How can I disable that (in the way that it used to be)?

    it totally ruins the way I change...

    I put several angles of the camera on the timeline and drag through each one to find the pats I want to reveal in my edit

  • Select multiple PDF files and share them [iOS]

    I use adobe Acrobat reader on my iPad and I would select several files and share them. Is this possible?

    Hi davidw26200688,

    I'm sorry ts not possible to share multiple PDF files at the same time in the iPad, however as a work around you can try to send the pdf by linking them directly to your email in PDF share also share the PDF by email.

    Kind regards

    Nicos

  • The questions "By clicking and dragging" during the recording of training simulations

    I am trying to record a training simulation in which I want the user to be able to click (to select an object) and drag it out into the workspace of the application (all in a single movement). While the app saves me ok, performing the action when it comes to testing the finished result the user cannot imitate this gesture as the simulation just goes ahead and does it for them. This action and drag is fundamental for our software, the user must be able to do themselves. Sorry, I hope this makes sense - of help is welcome.

    I don't know if this will be useful for your purpose, but worth a try. Here are some tips: advice & glide - Captivate blog

  • Fusion, space and dragging back &amp; forth - window minimizes

    Hello

    The merge in LOVE and my OS X machine.

    I have OS X spaces put in place to perform Windows of merge mode full screen on my Mac. Fusion is configured to run on the second 'space' in space, just to the right of the primary monitor. I just press ctrl + right arrow and the slides of the screen in Windows mode - it is EXCELLENT!

    But sometimes, I feel a prpoblme when I want to drag files across to the Windows form. Normally, I drag the file on the edge of the screen where windows and slides of the screen on Windows where I can drop the file in my environment to Win. Sometimes, however, I drag my document on the right side of the screen, the spaces OSX slip on Windows, then after I release thecopied document, Fusion screen immediately shrinks and jumps out of mode full screen.

    All my windows get resized and jump to the top right (it's HELL for web development) - I have to resize and drag them into their appropriate whenever places. Quite boring.

    Here all the patches?

    Thank you

    -


    S

    As you pointed out, there are far too many workarounds to make this too big of an issue at this stage.  However, if it's something obstacle workflow so it's something that I don't mind looking for later.  I wish newspapers appreciate from you.  If I can get the op as well, that would make it easier to compare.

  • Select multiple files and download

    Forms [32 bit] Version 10.1.2.3.0 (Production)
    using Webutil or something similar, is there a current functionality with my version of forms to select multiple files and upload them to the database, one after the other without going through the user use the dialog open several times?

    Thanks in advance

    Hello

    See the Webutil_File.FILE_MULTI_SELECTION_DIALOG () function.

    François

  • Can't click and drag to select anything in the Apple Pages

    I worked for hours on this unanswered anywhere on the internet.

    I can't click and drag to select text in the Apple Pages. I can click and drag to select text in other applications on the same computer, but in the Pages, I can't click and drag. It will show the text highlight for a fraction of a second, and then it will disappear. I can't copy or cut, because it will not select it. If I select the text with clicking and then holding down the SHIFT key and then clicking on at the end of the text, he chooses for me. But clicking and slippery will only highlight, then disappear. What's wrong???

    I have

    • computer restarts
    • restarted apple pages
    • trashed plists iWork (cannot find a specific pages plist)
    • Troubleshoot-made form another computer by comparing the preferences, settings, all on the same version of Pages
    • sought objects to make them 'selectable' (there is no objects, and this happens on all documents, including new pages documents I just create and try to highlight a Word)

    I'm on an iMac OSx 10.11.5 end 2013

    Apple Pages 5.6.2 (2573)

    It is a long-standing problem in OS X, are not specifically in any version of the Pages. Have you tried clicking & holding it down for a second or two in the text before you drag?

  • How can I insert space in a long page? Say, 1000px. Without selection and dragging everything down.

    Let's say I have a portfolio page and around 1500px from the top I want to insert 4 new fotos. I've been going to the bottom of the page and click-drag-ability to select all the fotos and pulling them down.

    Can I insert a space at a certain time and have everything go down accordingly?

    Thank you!

    Anthony

    You must physically move in the place. This is not a desktop program where you can specify line spacing, think of it as a blank canvas.

  • Some songs bought on Itunes and add them to an existing selection. They appear in the playlist and on computer but will not be synchronized to the Iphone. Help, please!

    Bought a few songs on ITunes and add them to an existing selection. They appear on the computer and in the playlist, but will not be synced to an existing selection on the IPhone 6. Help, please.

    This is the computer on which the phone is synchronized, and playlist is selected on the tab music from the phone to synchronize the phone? If the 'sync' button is not a copy the playlist update on you have tried to make a change on the screen of synchronization (for example in selecting/disable-selecting a playlist and sync and then get back on your original selection and re-sync)?

  • I'm unable to select, crop, or drop and drag. I also cannot manually move or resize a window or a file.

    Help with the selection

    I'm unable to select, crop, or drop and drag. I also cannot manually move or resize a window or a file. What the devil?

    You're not really in the right forum for this...

    Might be interesting to try a system restore to when it worked.

    http://Windows.Microsoft.com/en-us/Windows-Vista/what-is-system-restore

  • My nail of the thumb in bridge always have evidence the adjustments I make to RAW files. Now, for the first time they have not. However when I select the images adjusted and open them in CS6 adjustments are there. I restarted the bridge and PS, and I rest

    My nail of the thumb in bridge always have evidence the adjustments I make to RAW files. Now, for the first time they have not. However when I select the images adjusted and open them in CS6 adjustments are there. I restarted bridge and PS, and I restarted the computer (Mac). Still the same problem.

    I thank assani, but it wasn't the answer. The problem was, I finally understood, that the "option for generation of quality and preview thumbnails" wasn't on "high quality" as it usually is. I have no idea how this happened as I never change this option.

    Thanks a lot for your help.

    Mitchel Gray

  • How can I select several images contiguous hundreds of thousands of people in my library and copy them...

    How can I select several images contiguous hundreds of thousands in my organizer of items and copy them to a USB key so I can share with a family member who doesn't have photoshop?

    I fear that even if I find a method to select and move a batch of photos, images can be in a format that his computer is not supported.

    roberts80288042 wrote:

    How can I select several images contiguous hundreds of thousands in my organizer of items and copy them to a USB key so I can share with a family member who doesn't have photoshop?

    I fear that even if I find a method to select and move a batch of photos, images can be in a format that his computer is not supported.

    If I understand correctly...

    The photos you have posted in a particular order (probably the result of a selection). You can select contiguous files by clicking the first item, and then, with the SHIFT key, click the last item you want. That should highlight your contiguous files.

    Then, you use the function "export" in the file menu.

    In the dialog box, you have several useful options: file format, compression, the file size, the possible change of name and the destination. This destination can be a USB key or a Dropbox folder, as suggested by hatstead.

    If you have a large number to select, it's usually possible create a search with various criteria so that only the desired files are indicated.

    If you are not satisfied with the sort order (chronological, filename, import batch), I suggest to create an album. In an album, you can manually sort you want by moving the thumbnails in the navigation area. (Depends on your version of elements).

    Then all you have to do after that selection should select all (Ctrl A) and to export.

  • How multi select documents out for signature and remove them?

    During the test a form, I sent many forms for signature. Now they clutter the page manage. Is there a way multi SELECT and delete them without doing them one by one?

    Hello Balusha,

    From now on, we do not have bulk option remove documents on the tab manage in E-Sign. But, I probably took this as a feedback.

    Kind regards

    -Usman

  • In an earlier version of adobe, I could select multiple sheets of an excel workbook and arrange them in the order.  I could not find it in Pro XI.  ideas?

    In an earlier version of adobe, I could select multiple sheets of an excel workbook and arrange them in the order.

    Hi jerryt,

    You can reorder the pages in Acrobat following the instructions on this page: Adobe Acrobat X Pro * reorder pages in a PDF

    (these instructions also apply to Acrobat XI).

    Best,

    Sara

  • When I choose my quick selection tool, I click and drag the first time, and he adds (my selection). BUT if I let go of the mouse button and try to select more it goes to Negative (-) and removes all selections I made and won't allow me not one

    When I choose my quick selection tool, I click and drag the first time, and he adds (my selection). BUT if I let go the mouse button and try to select it goes to Negative (-) and removes all the selections I made and not will not let me add again. I never had this problem before, you usually need to hold down the Alt (or Option MAC) to subtract from selections. No one knows what happened or how to fix this problem, maybe a setting I accidentally hit? Thanks for your suggestions! :))

    Look at the options of quick selection - particularly the four icons on the left.

Maybe you are looking for