0

I want the user of my program to be able to highlight an area on a JFrame to select a group of items on screen. The code I posted below works, but it is very choppy, which becomes quite an eyesore every single time the JFrame repaints. Here's an image of what I'm talking about: http://i1065.photobucket.com/albums/u400/mfgravesjr/choppy%20draw%20rectangle_zpsspqsqnyf.png

enter image description here Maybe someone here has suggestions to improve my code?


This is the mouseDragged method in MouseMotionListener:

     public void mouseDragged(MouseEvent me)
     {
        if(groupingTerr&&me.getSource()==background)
        {
           endPoint = MouseInfo.getPointerInfo().getLocation();
           topLeftRect = new Point(Math.min(startPoint.x,endPoint.x),Math.min(startPoint.y,endPoint.y));
           bottomRightRect = new Point(Math.max(startPoint.x,endPoint.x),Math.max(startPoint.y,endPoint.y));
           for(Point p:map.territoryPoints)
           {
              if(p.x>topLeftRect.x&&p.x<bottomRightRect.x&&p.y>topLeftRect.y&&p.y<bottomRightRect.y)img.getGraphics().drawImage(selectedIco,p.x-selectedIco.getWidth()/2,p.y-selectedIco.getHeight()/2,null);
              else img.getGraphics().drawImage(defaultIco,p.x-defaultIco.getWidth()/2,p.y-defaultIco.getHeight()/2,null);
           }
           background.repaint();
        }
     }

This is the private overridden JFrame class

  private static class DrawableJFrame extends JFrame
  {
     @Override
     public void paint(Graphics g)
     {
        super.paint(g);
        g.setColor(new Color(0,0,0,100));
        g.drawRect(topLeftRect.x,topLeftRect.y,bottomRightRect.x-topLeftRect.x,bottomRightRect.y-topLeftRect.y);
        g.setColor(new Color(200,10,10,100));
        g.fillRect(topLeftRect.x,topLeftRect.y,bottomRightRect.x-topLeftRect.x,bottomRightRect.y-topLeftRect.y);
     }
  }

The image is original artwork. Please do not use it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
user3376587
  • 134
  • 2
  • 12
  • Why are you redrawing the whole image *every time* in mousedragged? – durron597 Aug 23 '15 at 01:22
  • Because how else would the rectangle corner follow the mouse? Is there a different way that you're thinking of? – user3376587 Aug 24 '15 at 22:57
  • Swing repaints the screen automatically; if your component draws the image in `paintComponent` then you only need to update the rectangle. – durron597 Aug 24 '15 at 23:00
  • The rectangle is drawn on the frame itself. So to update the rectangle, I would update the jframe, right? Is there a way to draw the rectangle separately? – user3376587 Aug 26 '15 at 20:08
  • IT should be drawn on a `JPanel` with `JFrame.setContentPane(panel);` You would then `.add` the rectangle to the panel. – durron597 Aug 26 '15 at 20:12
  • That's great advice, but the JFrame's content pane is already set to a JLabel with the background image in it. Setting it again would void out the background image. Whether I draw the rectangle on the JLabel or the JFrame, it's still going repaint every pixel on the screen. :( Any other suggestions? – user3376587 Aug 27 '15 at 00:31

0 Answers0