简体   繁体   中英

My JComponent will only repaint() once I manually resize the window, how do I get it to work as it should?

I have tried my best to read around on the topic but I cannot find/understand how to apply the answers to my piece code (and those I have applied don't work)

I have used an example from "Ivor Horton's Beginning Java 2 JDK book" and it is the first time I'm using repaint() but it doesn't seem to work unless I resize the window. It tries to repaint but leaves the screen blank.

Here's the code please let me know if there is something I'm doing wrong.

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 it is any help here's the 'Launcher' class for the application

Thanks in advance.

You will need to call

super.paint(g);

in your paint method in your CurveDrawings class to avail of the painting functionality already provided in the JFrame super class.

Note, the standard way of using custom painting in Swing is to use custom components that are based on javax.swing.JComponent that use and override paintComponent . This approach would leverage Swing's optimized painting model.

See: Performing Custom Painting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM