使用drawImage创建启动画面的正确方法(g)

时间:2015-04-25 16:01:06

标签: java awt lifecycle japplet

我正在学习构建一个JApplet,尽管还没有走得太远。我有一个图像(.png),在接口被“绘制”之前理想地由paintComponent绘制,或者更确切地说是从paint()方法中添加。

有趣的是,JApplet仅在appletviewer克隆后才按预期加载。似乎我对生命周期或运营动态缺乏了解。

这是代码,从中获取swing comp。为简洁起见,已删除了定义:

 public class JFriendsPhones extends JApplet implements ActionListener, MouseListener {  

public void init(){


    submit.addActionListener(this);
    queryByName.addActionListener(this);
    queryByNum.addActionListener(this);



    setVisible(true);

    introduction = getGraphics();

    prepareImage(intro, this);
    intro = getImage( getCodeBase(), "introduction.png");

    setVisible(true);

    validate();
}

public void start(){

}

public void paint(Graphics g){

    paintComponent(g);


    }


public void stop(){

}

public void destroy(){

}

public void actionPerformed( ActionEvent ev ){

    Object source = ev.getSource();

    returnLab.setVisible(false);
    resultsPan.setBackground(Color.white);

    if( true ){

        if( queryByName.getText().equals("") != true && queryByNum.getText().equals("") != true ){

                outcomeLab.setText("Search by only one term");
                queryByName.setText("");
                queryByNum.setText("");

        } else if( queryByName.getText().equals("") == true && queryByNum.getText().equals("") == true){
            outcomeLab.setText("Enter search term");

            } else {

                if(queryByName.getText().equals("") == false){

                    for( int i = 0; i < 5; i++ ){
                        if(queryByName.getText().equalsIgnoreCase(friends[i])){
                            resultsPan.setBackground(new Color(190,255,200));
                            outcomeLab.setText("1-result found:");
                            returnLab.setText(frNumbers[i]);    
                            returnLab.setVisible(true);
                            found = true;
                        } 

                    } if(found == false){
                        resultsPan.setBackground(new Color(240,100,100));
                        outcomeLab.setText("0-matches");
                    }
                } else {

                    for( int i = 0; i < 5; i++ ){

                        if(queryByNum.getText().equalsIgnoreCase(frNumbers[i])){
                            resultsPan.setBackground(new Color(190,255,200));
                            outcomeLab.setText("1-result found:");
                            returnLab.setText(friends[i]);  
                            returnLab.setVisible(true);
                            found = true;
                        }                                       
                    }

                        if( found == false){
                                resultsPan.setBackground(new Color(240,100,100));
                                outcomeLab.setText("0-matches");
                        }

        }

              }
    }

    found = false;
}

public void componentResized( ComponentEvent cEv){
        repaint();
}

@Override
public void componentMoved(ComponentEvent e) {
        contain.repaint();

}

@Override
public void componentShown(ComponentEvent e) {

    paintComponent(introduction);

}

@Override
public void componentHidden(ComponentEvent e) {
    // TODO Auto-generated method stub

}

public void mouseClicked( MouseEvent clicked){
    repaint();
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

public void paintComponent(Graphics gr){

    super.paintComponents(gr);

    if(initialized == false){

        gr.drawImage(intro, 10, 10, 280, 430, this);
        validate();
            initialized = true;
        } else {

        titl_instrPan.add(titleLab);
        titl_instrPan.add(instructLab);
        titl_instrPan.add(instructLab2);

        inputPan.add(byNameLab);
        inputPan.add(queryByName);
        inputPan.add(byNumLab);
        inputPan.add(queryByNum);
        inputPan.add(regionPan);
        inputPan.add(submit);

        resultsPan.add(outcomeLab, BorderLayout.NORTH);
        resultsPan.add(returnLab, BorderLayout.SOUTH);
        resultsPan.setBackground(Color.white);

        mainPanel.add(titl_instrPan, BorderLayout.NORTH);
        mainPanel.add(inputPan, BorderLayout.CENTER);
        mainPanel.add(resultsPan, BorderLayout.SOUTH);

        contain.add(mainPanel);
        setVisible(true);
        validate();

        }


} end class JFriendsPhones

2 个答案:

答案 0 :(得分:0)

你通过不调用super.paint(...)搞乱了Swing的默认绘制机制。

无论如何,不​​要覆盖JApplet的paint(..)!没有必要这样做。

答案 1 :(得分:0)

特别是在查看了Graphics类的基础知识之后     public void paint() 这是事件驱动的,我在paint方法中添加了一个计数器,以快速了解它在启动时调用的次数,并使用该数字作为执行drawImage()方法的条件 - 成功。一个有趣的说明是,不同的编译器在对paint()的初始调用中似乎有所不同,例如,Eclipse的5,javac 1.8的3。