覆盖paintComponent

时间:2013-03-24 18:21:21

标签: java swing jpanel paintcomponent mouselistener

好的,所以我一直试图通过指导和在线帮助来解决这个问题,但没有成功。

我想要做的就是当用户点击PaintPanel时绘制一个椭圆形 请有人帮忙让我睡觉:P

screen

在PaintApplet类中:

  private void PaintPanelMouseClicked(java.awt.event.MouseEvent evt) {                                        
    // TODO add your handling code here:
    if(FillRadioButton.isSelected())
    {
    PaintPanel.setBackground(JColor.getColor());
   PaintPanel.repaint();
    }
    if(BrushRadioButton.isSelected())
    {
         Point ComponentPoint = PaintPanel.getLocationOnScreen();
         PaintPanel.add(new Painter(ComponentPoint));
    }
}           

画家类:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
 import java.awt.*;

public class Painter extends JPanel{

Point  Component;

public Painter(Point Com)
{
    Component = Com;
}

public void paintComponent(Graphics g) 
{

        super.paint(g);
      Point CursorPoint=   MouseInfo.getPointerInfo().getLocation(); //gets cursorpoint

       int ComPX =  Component.x;
        int ComPY =  Component.y;
        int CurPX =  CursorPoint.x;
        int CurPY =  CursorPoint.y;
        int FinalX = CurPX - ComPX;
        int FinalY =  CurPY - ComPY;

       g.drawOval(FinalX, FinalY, 20, 20);




}
}

1 个答案:

答案 0 :(得分:3)

不要打电话

super.paint(g);
paintComponent

。这将导致油漆链机制中的无限循环。相反,你需要

super.paintComponent(g);

为了更好的帮助,请考虑发布SSCCE