2

I have an application that displays 10-50 images at a time on my frame, with boxes around them. I can drag the images and place them in a new location in the array, and the frame will update the whole time. The reason I'm making this post is because my 3440x1440 monitor makes this program lag, and my old 1920x1080 didn't lag at all.

Here's a gif showing what I'm talking about: https://gfycat.com/InferiorFrenchArachnid

and here's my code for drawing the images and the boxes surrounding them to the frame:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    if (model.getCarPath() == null){
        viewImage = createImage(1000,1000);
        viewGraphics = viewImage.getGraphics();
        viewGraphics.setColor(color);
        viewGraphics.fillRect(0, 0, 1000, 1000);
    }
try{

        if (model.getCarPath() != null){
            if(viewImage==null){
                viewImage = createImage(1000,1000);
                viewGraphics = viewImage.getGraphics();
            }
            viewGraphics.setColor(color);
            viewGraphics.fillRect(0, 0, 1000, 1000);

            viewGraphics.setColor(Color.blue);
            for (int i = 0; i < model.getPictureArray().size()-1; i++){
                //draw the images
                viewGraphics.drawImage(x,y,width,height, this);

            }
            // draws the selected picture over top of the rest
            viewGraphics.drawImage(model.getSelected().getImage(), (int)x,
                    (int)y, (int)width,
                    (int)height, this);

            if (model.doesLineExist() == true){ 
                // draws the line next to the pictures to show where the picture will be dropped 
                // when the mouse is released
                viewGraphics.fillRect((int)model.getLine().getX(), (int)model.getLine().getY(), 5,
                        (int)model.getLine().getHeight());
            }
        }
    } catch (Exception e){ }
    g.drawImage(viewImage, 0, 0, this);
}

Without posting the entirety of my code, I think this encompasses the least efficient part. Is there anything you see that I can change that will stop the program from lagging?

Nexion21
  • 149
  • 11
  • 2
    Try to do as little as possible in paint. Pre-cache the values as much as you can and maintain those values for as long as you (think) you need them, maybe even re-using them if possible. [For example](http://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184) – MadProgrammer Feb 02 '17 at 23:02
  • @MadProgrammer Could you point out what I might be able to take out? – Nexion21 Feb 03 '17 at 00:20
  • Any time you create something an image, it shouldn't be done within the paint method – MadProgrammer Feb 03 '17 at 00:29
  • If creating the image is some sort of "lazy construction" (and it looks like one), then this could be OK, and should not cause any lags after the first paint call. However, the code looks a bit sketchy. What is the `drawImage` call that is (superfluously) commented with `//draw the images`? It doesn't seem like a method of the `Graphics` class. Also: What types of images are this? (How is `createImage` implemented, and where do the `model.getSelected().getImage()` ones come from?) But in the end, maybe `model.doesLineExist()` is the slow part - who knows, we can only guess... – Marco13 Feb 03 '17 at 01:51

0 Answers0