简体   繁体   English

绘画组件未调​​用

[英]Paint component not being called

I'm trying to code a little maze runner program, and have run into some trouble relating to the paintComponent(). 我正在尝试编写一些迷宫赛跑者程序,并且遇到了与paintComponent()有关的麻烦。 I have gone through the debug and for some reason my paintComponent() is never called, even with the repaint() which is being called by my timer. 我已经进行了调试,由于某种原因,我的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);
    }

Those are my frame and jpanel init methods. 这些是我的frame和jpanel初始化方法。

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

}

This is my paintComponent, the image is indeed buffered and has been stored. 这是我的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();
    }

That is my actionPerformed which is called with my timer which is set to a 5 sec interval at default. 那就是我的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();
    }

}

} }

That is my full code if you're interested. 如果您有兴趣,这就是我的完整代码。 My question is why I can't get the image to draw onto the Maze panel. 我的问题是为什么我无法将图像绘制到迷宫面板上。

paintComponent won't be called for three basic reasons. 由于三个基本原因,不会调用paintComponent

  1. The component isn't attached to a valid native peer (ie, it's not attached to a window directly or indirectly that is visible on the screen) 该组件未附加到有效的本机对等项(即,它未直接或间接地附加在屏幕上可见的窗口上)
  2. or it doesn't have a size greater than 0x0 或大小不大于0x0
  3. or it's not visible... 还是看不见...

Skimming through your code, I can't find anywhere an instance of mazerunner is actually added to the frame ... 浏览您的代码,我找不到在frame实际添加任何mazerunner实例的地方...

You might like to have a read through Code Conventions for the Java TM Programming Language , it will make it easier for people to read your code and for you to read others 您可能希望通读Java TM编程语言的代码约定 ,这将使人们更容易阅读您的代码,并使您阅读其他代码更容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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