一旦我手动调整窗口大小,我的jcomponent才会重新涂抹(),如何使它尽可能地工作



我已经尽力阅读有关该主题的阅读,但是我找不到/了解如何将答案应用于我的件代码(我应用的那些不起作用)

我使用了" Ivor Horton的"开始Java 2 JDK"书中的示例",这是我第一次使用repaint(),但是除非我调整了窗口大小,否则它似乎行不通。它试图重新粉刷但使屏幕空白。

这是代码,请让我知道我是否做错了。

public class CurveDrawings extends JFrame{
public CurveDrawings (String title){
    setTitle(title);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pane = new CurvePane();                     //Create pane containing curves
    Container content = getContentPane();       //Get the content pane
    //Add the pane containing the curves to the content pane for the applet
    content.add(pane);
    MouseHandler handler = new MouseHandler();
    pane.addMouseListener(handler);
    pane.addMouseMotionListener(handler);
}
//Class defining pane on which to draw
class CurvePane extends JComponent{
    public CurvePane(){
        quadCurve = new QuadCurve2D.Double(
            startQ.x, startQ.y, control.x, control.y, endQ.x, endQ.y
        );
        cubicCurve = new CubicCurve2D.Double(
            startC.x, startC.y, controlStart.x, controlStart.y,
            controlEnd.x, controlEnd.y, endC.x, endC.y
        );
    }
}
class Marker{//not needed for my problem}
class MouseHandler extends MouseInputAdapter{
    public void mousePressed(MouseEvent e){
        if(ctrlQuad.contains(e.getX(),e.getY())){
            selected = ctrlQuad;}
        else if(ctrlCubic1.contains(e.getX(),e.getY())){
            selected = ctrlCubic1;}
        else if(ctrlCubic2.contains(e.getX(),e.getY())){
            selected = ctrlCubic2;}
    }

    public void mouseReleased (MouseEvent e){
        selected = null;
    }
    public void mouseDragged (MouseEvent e){
        System.out.println("DRAGGED");
        if(selected != null){
            //Set the marker to current cursor position
            selected.setLocation(e.getX(),e.getY());
            pane.validate();
            pane.repaint();
        }
    }
    Marker selected = null;
}
    public void paint (Graphics g){
        Graphics2D g2D = (Graphics2D)g;         //Get a 2D device context
    //Code to draw each component
    }

//Points for quadratic curve
//Points for cubic curve
//More unnecessary code}

incase,这是应用程序的"启动器"类的任何帮助

预先感谢。

您需要致电

super.paint(g);

CurveDrawings类中的paint方法中,可以利用JFrame超级类中已经提供的绘画功能。

注意,在摆动中使用自定义绘画的标准方法是使用基于使用和覆盖paintComponentjavax.swing.JComponent的自定义组件。这种方法将利用Swing的优化绘画模型。

请参阅:执行自定义绘画

相关内容

最新更新