使用 drawImage(g) 创建闪屏的正确方法
Proper method for creating splashscreen with drawImage(g)
我正在学习构建 JApplet,但还没有深入到表面之下。我有一个图像 (.png),理想情况下,它会在界面 "painted" 之前由 paintComponent(s) 绘制,或者更确切地说是从 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
您没有调用 super.paint(...) 搞乱了 Swing 的默认绘制机制。
无论如何,不要覆盖 JApplet 的 paint(..)!没有必要这样做。
在复习了 Graphics 的基础知识后 class,特别是
public无效油漆()
这是事件驱动的,我在 paint 方法中添加了一个计数器以快速了解它在启动时被调用了多少次,并使用该数字作为执行 drawImage() 方法的条件——成功。一个有趣的注意事项是,不同的编译器在它们对 paint() 的初始调用中似乎有所不同,例如,Eclipse 的 5 个,javac 1.8 的 3 个。
我正在学习构建 JApplet,但还没有深入到表面之下。我有一个图像 (.png),理想情况下,它会在界面 "painted" 之前由 paintComponent(s) 绘制,或者更确切地说是从 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
您没有调用 super.paint(...) 搞乱了 Swing 的默认绘制机制。
无论如何,不要覆盖 JApplet 的 paint(..)!没有必要这样做。
在复习了 Graphics 的基础知识后 class,特别是 public无效油漆() 这是事件驱动的,我在 paint 方法中添加了一个计数器以快速了解它在启动时被调用了多少次,并使用该数字作为执行 drawImage() 方法的条件——成功。一个有趣的注意事项是,不同的编译器在它们对 paint() 的初始调用中似乎有所不同,例如,Eclipse 的 5 个,javac 1.8 的 3 个。