-5

Could you tell me why my repaint() method doesn't work in my code? Do you know some websites from which I can learn the basic stuff. Thanks in advance.

public class OknoGry extends JFrame
{   
public static enum Tryb{NOWA_GRA,LISTA_NAJLEPSZYCH, USTAWIENIA,   MENU}                             
private JButton nowa_gra,wyjscie,ustawienia,lista_najlepszych;  

OknoGry()
{
    zaladujPrzysicki();
}


public void zaladujPrzysicki(){

    JFrame okno_gry = new JFrame ("LUNAR LANDER");
    JPanel panel = (JPanel)okno_gry.getContentPane();
    panel.setLayout(null);
    okno_gry.setBounds(0,0,600,400);
    okno_gry.setVisible(true);
    wyjscie = new JButton("Wyjscie");
    wyjscie.setBounds(150, 200, 300, 50);
    wyjscie.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
               System.exit(0);
          }
       });
    panel.add(wyjscie);
    nowa_gra = new JButton("Nowa Gra");
    nowa_gra.setBounds(150,150,300,50);
    nowa_gra.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
               przyciskNowaGra();
            //   repaint();
          }
       });
    panel.add(nowa_gra);
    ustawienia = new JButton("Ustawienia");
    ustawienia.setBounds(150, 100, 300, 50);
    ustawienia.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
               przyciskUstawienia();
          }
       });
    panel.add(ustawienia);

    lista_najlepszych = new JButton("Lista Najlepszych Wynikow");
    lista_najlepszych.setBounds(150, 50, 300, 50);
    lista_najlepszych.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
               przyciskListaNajlepszych();
          }
       });
    panel.add(lista_najlepszych);
    okno_gry.addWindowListener(new WindowAdapter() {                                    
        public void windowClosing(WindowEvent we) {
            System.exit(0);
        }
    });
    okno_gry.setVisible(true);
    okno_gry.setResizable(false);
//  panel.paintComponents(g);

}
protected void przyciskUstawienia() {
    tryb_gry = tryb_gry.USTAWIENIA;
}
protected void przyciskListaNajlepszych() {

    tryb_gry = tryb_gry.LISTA_NAJLEPSZYCH;
}
protected void przyciskNowaGra() {

    tryb_gry = tryb_gry.NOWA_GRA;
    //System.out.println("uzytkowniku kliknales na przysick nowa gra");
    nowa_gra.setVisible(false);
    wyjscie.setVisible(false);
    ustawienia.setVisible(false);
    lista_najlepszych.setVisible(false);
    //System.out.println(tryb_gry);

    repaint();

}
public void rysujMape(Graphics g){                  
        Dimension dim = getSize();                      
        System.out.println("JESTEM W METODZIE rysujMape");
        System.out.println(punkt_y1*dim.height);
        System.out.println(punkt_x2*dim.width);
        g.drawLine(0, punkt_y1*dim.height/100, punkt_x2*dim.width/100, punkt_y2*dim.height/100);                    
        g.drawLine(punkt_x2*dim.width/100, punkt_y2*dim.height/100, punkt_x3*dim.width/100, punkt_y3*dim.height/100);
        g.drawLine(punkt_x3*dim.width/100,punkt_y3*dim.height/100,punkt_x4*dim.width/100,punkt_y4*dim.height/100);
        g.drawLine(punkt_x4*dim.width/100,punkt_y4*dim.height/100, punkt_x5*dim.width/100,punkt_y5*dim.height/100);


}
public void paintComponent(Graphics g)           
{   
  super.paintComponent(g);
    System.out.println("jestem w metodzie paintComponent");
    switch(tryb_gry){
    case NOWA_GRA:{

    rysujMape(g);               
    rakieta.rysujRakiete(g);
    break;
    }
    case USTAWIENIA:{
        break;
    }
    case LISTA_NAJLEPSZYCH:{
        break;
    }
    case MENU:{
        break;
    }
    default:{

        break;
    }
    }
}

public static void main(String args[])
{
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            //createAndShowGUI();
            OknoGry gf = new OknoGry();
        }
    });
}}

I have no idea why it isn't working, please help.

Prophet
  • 32,350
  • 22
  • 54
  • 79
Kabun
  • 35
  • 3
  • What do you want to do? Any errors? What is not working? You should at least show some effort before posting a question here, or your questions get downvoted and you don't get much feedback usually. (I didn't downvote it btw, but I understand those who did) – vefthym May 30 '14 at 13:32

1 Answers1

2
  1. Your class is a JFrame, which has no paintComponent method, so aren't actually overriding any paint functionality.

  2. You class is already a JFrame but you create another JFrame instance in the constructor. Why? Don't. Choose one or the other.

  3. paintComponent should be overridden in JPanel. So make a class that extends JPanel, override the paintComponent in that panel, then add that panel to your frame.

  4. Make use of the @Override annotation, so you know if you're properly overriding a method.

    @Override
    protected void paintComponent(Grapchics g) {}
    
  5. When you call repaint() make sure you call it on the panel and not the frame.

  6. For more about painting see Performing Custom Painting

  7. set your frame visible after adding your components.

  8. Avoid using null layouts. Learn to use layout managers. See Laying Components Within a container

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720