0

I'm working on a little java project wherein I need to draw a small handful of shapes to the screen. I also need to capture mouse events for utility functions. Right now, I have a program that captures a click, and sends out a little ripple from that position. Whenever the mouse is moved, however, the paintComponent(Graphics g) method just stops entirely. I thought it was a buffering issue, so I implemented one, and it just slowed even more at normal rendering. Another strange thing: When the mouse is being held down, the paintComponent method doesn't care about the mouse movement anymore. It resumes as normal. EDIT: I should also mention that the mouse events are triggered by a class that implements MouseListener. They are calling correctly. To get the mouse position, I use this: public Point getMouse() { Point mousePos = MouseInfo.getPointerInfo().getLocation(); SwingUtilities.convertPointFromScreen(mousePos, screen); return mousePos; }

package com.noneofyebidmness;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.ArrayList;

import javax.swing.JPanel;

import com.noneofyebidmness.Organism;

@SuppressWarnings("serial")
public class Screen extends JPanel {
private int diameter = 10;
private ArrayList<Point> ripplePositions;
private ArrayList<Point> rippleDiameters;

public Screen() {
    ripplePositions = new ArrayList<Point>();
    rippleDiameters = new ArrayList<Point>();
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(Reference.SCREEN_WIDTH_DEFAULT, Reference.SCREEN_HEIGHT_DEFAULT);
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    System.out.println("Painting");
    Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        for(Organism o : Wrapper.getInstance().getOrganisms()) {
            o.draw(g2d);
        }

        Point mouse = Wrapper.getInstance().getMouse();
        Utility.background(Utility.color(0, 150, 250), g2d, this);          
        g2d.setColor(Utility.color(255));
        g2d.fillOval((int)mouse.getX() - diameter / 2, (int)mouse.getY() - diameter / 2, diameter, diameter);

        g2d.setColor(Color.BLACK);
        g2d.drawString(Wrapper.FPS_PERIODIC + "", 10, 20);

        for(int i = 0; i < ripplePositions.size(); i++) {
            int w = rippleDiameters.get(i).x;
            g2d.setColor(Utility.color(255, Math.min(220-w, 255)));
            g2d.setStroke(new BasicStroke(10));
            g2d.drawOval(ripplePositions.get(i).x - w / 2, ripplePositions.get(i).y - w / 2, rippleDiameters.get(i).x, rippleDiameters.get(i).x);

            if(w < 255) {
                rippleDiameters.get(i).x += rippleDiameters.get(i).y;
                if(rippleDiameters.get(i).y > 0)
                    rippleDiameters.get(i).y -= 0.01;
            }

            if(220-w == 0) { 
                ripplePositions.remove(i);
                rippleDiameters.remove(i);
            }
        }
    g2d.dispose();
}

public void addRipple() {
    ripplePositions.add(Wrapper.getInstance().getMouse());
    rippleDiameters.add(new Point(diameter, 20));
}
}

TL;DR: When the mouse is moved over my JPanel, it stops calling paintComponent(). If the mouse is held down and moved, things operate as normal.

Any help is appreciated, Carl Litchman

Carl Litchman
  • 129
  • 2
  • 10
  • The issues seems to be that you don't understand how panting works in Swing. Have a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html), [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details about how painting works – MadProgrammer Feb 20 '16 at 20:47
  • Then have a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for some ideas about how you might solve the problem and finally, have a look at [example](http://stackoverflow.com/questions/16857380/jpanel-listeners-and-threads-issues/16867558#16867558) for an example – MadProgrammer Feb 20 '16 at 20:48
  • 1
    Instead of "pulling" the mouse location, use [addMouseMotionListener](https://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#addMouseMotionListener-java.awt.event.MouseMotionListener-) to be notified of changes to the mouse location. Have your listener save the mouse location in an instance field, then call `repaint`. – VGR Feb 20 '16 at 22:03
  • @MadProgrammer Thanks, I'm still pretty new to using Java for anything but logic applications. – Carl Litchman Feb 20 '16 at 22:33

0 Answers0