简体   繁体   中英

Jpanel inside Jframe with paint

hi i'm triyng to do something like this example

but i'm always getting the cross or in top-west or it doesn't appear and don't know why. I try to see the borderLayout and some stackoverflow explanations examples but didn´t find anything related. can someone explain to me what i'm doing wrong and why please?

public Base() {
        super("Menu");
        JPanel p = createPanel();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        setSize(screen.width, screen.height);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(p);
    }

    private JPanel createPanel() {
        JPanel P = new JPanel() {
            public void paint(Graphics g) {
                super.paint(g);
                Graphic gr = new Graphicimpl();
                g.drawLine(gr.PositionX(-25.0), gr.PositionY(0.0), gr.PositionX(25.0), gr.PositionY(0.0));
                g.drawLine(gr.PositionX(0.0), gr.PositionY(25.0), gr.PositionX(0.0), gr.PositionY(-25.0));

            }
        };

        //P.setSize(50, 50);
        //P.setBorder(BorderFactory.createEmptyBorder(300, 300, 300, 300));
        return P;
    }
}
public class Graphicimpl implements Graphic{
    int FACTOr_ESCALACION = 10;


    public  int PositionX(Double x) {
        return (int) ((x *  FACTOr_ESCALACION) + 320);
    }

    @Override
    public int PositionY(Double y) {
        return (int) ( - (y * FACTOr_ESCALACION ) +240);
    }


}

public interface Graphic {
    int PositionX(Double x);
    int PositionY(Double y);
}
public class Main {
    public static void main(String args[]) {
        Base base=new Base();
    }

}

The location of the cross depends on the coordinates you have inserted. Since you are inserting them manually. You can test the program with different coordinates until the cross shows where you want it to.

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