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