未调用Paint组件

时间:2014-11-05 01:58:33

标签: java swing jframe jpanel paintcomponent

我试图编写一个小迷宫跑步者程序,并且遇到了与paintComponent()相关的一些麻烦。我已经完成了调试,由于某种原因,我的paintComponent()从未被调用,即使我的计时器调用了repaint()。

private void jpanelinit() {
        JPanel Background = new JPanel (new BorderLayout());        
        JPanel Menu = new JPanel (new BorderLayout());
        JPanel Maze = new JPanel (new GridLayout(arow, acolumn));
        Background.setPreferredSize(new Dimension(850,850));        
        Menu.setPreferredSize(new Dimension(850, 100));
        Maze.setPreferredSize(new Dimension(850,750));
        frame.add(Background);              
        comboboxinit();
        Background.add(Menu, BorderLayout.NORTH);                   
        Background.add(Maze, BorderLayout.SOUTH);
        Menu.add(Startpause, BorderLayout.WEST);                    
        Menu.add(Reset, BorderLayout.EAST);
        Menu.add(Intervalpick, BorderLayout.CENTER);
        Intervalpick.setVisible(true);                          
        Intervalpick.addActionListener(this);
        Startpause.setVisible(true);
        Startpause.addActionListener(this);
        Reset.setVisible(true);
        Reset.addActionListener(this);
        Maze.setVisible(true);

}
    private static void frameinit() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.setResizable(false);
        frame.setSize(850,850);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

这些是我的frame和jpanel init方法。

@Override 
public void paintComponent(Graphics g){
    System.out.println("Entered Graphics");
    super.paintComponent(g);
    g.drawImage(biWall,0,100,850,750, this );

}

这是我的paintComponent,图像确实已缓冲并已存储。

    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==Intervalpick)
        timecheck();        //Checks if the time was changed
    if(e.getSource()==Startpause||e.getSource()==Reset)
        buttoncheck(e);     //Checks if the buttons were pressed
    if(t.isRunning())
        mazeupdate();
     }

    private void mazeupdate() { 
    repaint();
    }

这是我的actionPerformed,用我的计时器调用,默认设置为5秒。

public class mazerunner extends JPanel implements ActionListener {


static JButton Startpause = new JButton("Start");
static JButton Reset = new JButton("Reset");
static JComboBox Intervalpick= new JComboBox();
static JFrame frame=new JFrame ("Maze Runner");
static int arow=0, acolumn=0, icurrenttime=5000;
static boolean gameplaying=false;
static Timer t;
static BufferedImage biWall, biFloor, biMouse, biCheese;

public static void main(String[] args) {

    mazerunner mr= new mazerunner();

    filereader();           //Reads the file
    imagebuffer();          //Buffers the images
    mr.jpanelinit();        //Inits the gui
    frameinit();            //Inits the frame
    mr.timerinit();         //Inits the timer

}

    private static void imagebuffer() {

        try{
            biWall=ImageIO.read(new File("cobblewall.jpg"));
        }
        catch(IOException e){}
        try{
            biFloor=ImageIO.read(new File("woodenfloor.jpg"));
        }
        catch(IOException e){}
        try{
            biCheese=ImageIO.read(new File("chest_cheese.jpg"));
        }
        catch(IOException e){}
        try{
            biMouse=ImageIO.read(new File("lara_mouse.jpg"));
        }
        catch(IOException e){}          

}

    private void  timerinit() {

        t=new Timer(icurrenttime,this);

}

    private void jpanelinit() {

        JPanel Background = new JPanel (new BorderLayout());        //Inits all the JPanels
        JPanel Menu = new JPanel (new BorderLayout());
        JPanel Maze = new JPanel (new GridLayout(arow, acolumn));

        Background.setPreferredSize(new Dimension(850,850));        //Sets the size of the panels
        Menu.setPreferredSize(new Dimension(850, 100));
        Maze.setPreferredSize(new Dimension(850,750));

        frame.add(Background);                                      //Adds background into the frame

        comboboxinit();

        Background.add(Menu, BorderLayout.NORTH);                   //Adds the other panels into the background
        Background.add(Maze, BorderLayout.SOUTH);

        Menu.add(Startpause, BorderLayout.WEST);                    //Adds the menu's components into the menu panel
        Menu.add(Reset, BorderLayout.EAST);
        Menu.add(Intervalpick, BorderLayout.CENTER);

        Intervalpick.setVisible(true);                              //Sets the components to visible and adds actionlistener
        Intervalpick.addActionListener(this);
        Startpause.setVisible(true);
        Startpause.addActionListener(this);
        Reset.setVisible(true);
        Reset.addActionListener(this);
        Maze.setVisible(true);

}

    private static void comboboxinit() {

        for(int a=5;a<=30;a=a+5){                                           //Sets the text inside the combobox
            Intervalpick.addItem(a+" Seconds");
        }
        DefaultListCellRenderer dlcr = new DefaultListCellRenderer();       //Centers the text inside the combobox
        dlcr.setHorizontalAlignment(DefaultListCellRenderer.CENTER); 
        Intervalpick.setRenderer(dlcr); 

    }

    private static void frameinit() {

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //Inits the jframe
        frame.setResizable(false);
        frame.setSize(850,850);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);


    }

    private static void filereader() {

        try{
            FileReader fr=new FileReader("maze.txt");
            BufferedReader br= new BufferedReader(fr);
            String LineIn=br.readLine();
            int num, row=0;
            arow=Integer.parseInt(LineIn);
            LineIn=br.readLine();
            acolumn=Integer.parseInt(LineIn);
            int Maze[][]=new int[arow][acolumn];    //inits the maze itself
            LineIn=br.readLine();
            do{                                     //stores the maze from the file into the arrray
                int collumn=0;
                    StringTokenizer tokens=new StringTokenizer(LineIn);
                    while (tokens.hasMoreTokens()){
                        num=Integer.parseInt(tokens.nextToken());
                        Maze[row][collumn]=num;
                        collumn++;
                    }
                LineIn=br.readLine();
                row++;
            }while(LineIn!=null);
            br.close();
            fr.close();
        }   
        catch(FileNotFoundException e){
            System.err.println("file not found");
        }
        catch(IOException e){
            System.err.println("read failed");
        }

    }

@Override 
public void paintComponent(Graphics g){
    System.out.println("Entered Graphics");
    super.paintComponent(g);
    g.drawImage(biWall,0,100,850,750, this );

}

public void actionPerformed(ActionEvent e) {

    if(e.getSource()==Intervalpick)
        timecheck();        //Checks if the time was changed
    if(e.getSource()==Startpause||e.getSource()==Reset)
        buttoncheck(e);     //Checks if the buttons were pressed
    if(t.isRunning())
        mazeupdate();

}

    private void mazeupdate() {

        repaint();

}

    private void buttoncheck(ActionEvent e) {
        if(e.getSource()==Startpause){          //Starts and pauses the simulation
            if(gameplaying==false){
                gameplaying=true;
                t.start();
                Startpause.setText("Pause");
            }
            else if(gameplaying==true){
                gameplaying=false;
                t.stop();
                Startpause.setText("Start");
            }
        }
        else if(e.getSource()==Reset){              //Resets the maze to the original maze
            if(t.isRunning())
                t.stop();
            gameplaying=false;
            Startpause.setText("Start");
            filereader();
        //  t.repaint();
        }

}

    private void timecheck() {

    int boxtime= Integer.parseInt(((String) Intervalpick.getSelectedItem()).split(" ")[0])*1000;        //Turns Intervalpick into a milisecond amount
    if(boxtime!=icurrenttime){                  //Checks if the selected delay is equal to the actual delay
        boolean gamerunning=false;
        icurrenttime=boxtime;
        if(t.isRunning()){                      //Stops timer if running
            t.stop();
            gamerunning=true;
        }
        t.setDelay(icurrenttime);               //Changes delay
        if(gamerunning==true)                   //If timer was running it turns it back on
            t.start();
    }

}

}

如果您有兴趣,这是我的完整代码。我的问题是为什么我无法将图像绘制到迷宫面板上。

1 个答案:

答案 0 :(得分:1)

由于三个基本原因,

paintComponent无法被召唤。

  1. 该组件未附加到有效的本地对等方(即,它没有直接或间接连接到窗口上可见的窗口)
  2. 或者其大小不超过0x0
  3. 或者它不可见......
  4. 浏览您的代码,我无法找到mazerunner实际添加frame的实例...

    您可能希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码

相关问题