Reputation: 99
I've come across a problem when trying to move a custom polygon around a JPanel with the mouse; the location for the polygon never updates, even on repaint:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.ArrayList;
import java.util.List;
public class Figure extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public int nPoints = 17, x = 120, y = -60;
int MouseX, MouseY;
final static int SIZE = 400;
public int[] xPoints = {x + 100, x + 85, x + 70, x + 55, x + 40, x + 55, x + 55, x + 40, x + 25, x + 55, x + 55, x + 85, x + 85, x + 115, x + 100, x + 85, x + 85};
public int[] yPoints = {y + 375, y + 385, y + 340, y + 385, y + 375, y + 335, y + 200, y+ 240, y + 225, y + 185, y + 175, y + 175, y + 185, y + 220, y + 240, y + 200, y + 335, y + 375};
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
static final Figure m = new Figure();
public static void main(String[] args){
final JFrame frame = new JFrame();
frame.setTitle("Figure");
frame.getContentPane().add(m);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(SIZE, SIZE);
frame.setResizable(true);
frame.setVisible(true);
m.setBackground(Color.darkGray);
}
public void mouseMovement(){
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
if(((e.getX() >= xPoints[8] && e.getX() <= xPoints[13]) && (e.getY() >= yPoints[10] && e.getY() <= yPoints[1])) || ((e.getX() >= (x + 45) && e.getX() <= (x + 45 + SIZE / 8)) && (e.getY() >= (y + 102) && e.getY() <= (y + 102 + SIZE / (57 / 10))))){
x = e.getX() - 75;
y = e.getY() - 150;
repaint();
}else{
repaint();
}
}
});
}
public Figure(){
mouseMovement();
}
@Override
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.black);
g.fillPolygon(xPoints, yPoints, nPoints);
g.fillPolygon(xPoints, yPoints, nPoints);
g.setColor(Color.white);
g.fillOval(x + 45, y + 102, SIZE / 8, SIZE / (57 / 10));
}
}
My thoughts on this are that the integer array is not updating properly using my predefined variables, but then I come across the problem of not knowing how to properly update it. It seems like it should, considering the location for the oval i made to represent the figure's "head" does, but apparently I'm wrong on that, so I'd like to know why just using the array in fillPolygon
doesn't work and how I'd get the location to update. Many thanks in advance.
Upvotes: 0
Views: 215
Reputation: 324118
Use the Polygon
class to create a Polygon using your Array of points.
Then in the painting method you can cast the Graphics object to a Graphics2D object and use the Graphics2D.fill(Shape)
method to paint the polygon.
When you need to move the Polygon you can use the translate(...)
method of the Polygon class.
Also, you should NOT be overriding the paint() method. Custom painting is done by overriding the paintComponent()
method.
Upvotes: 4