Chinaunix首页 | 论坛 | 博客
  • 博客访问: 215986
  • 博文数量: 33
  • 博客积分: 1690
  • 博客等级: 上尉
  • 技术积分: 381
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-27 18:57
个人简介

吟游天地间,隐没市井中..

文章分类
文章存档

2014年(1)

2009年(6)

2008年(26)

我的朋友

分类: Java

2008-09-28 14:06:05

    在写applet的过程中,我曾经在一些小问题上卡到了,这里列出来作为一个笔记。如果能为后来人提供一点帮助,那更是荣幸之至了。
    1:applet中经常会用一些资源,我们会把这些资源打到jar包中,那么在程序中该如何引用呢?
       比方说你要引用一个图片作为图标,那么使用

     ImageIcon picture = new ImageIcon(getClass().getResource("demo.jpg"));

       然后再把demo.jpg打到jar包中就可以了。

   2:如何在applet中设置背景图片呢?

     p1 = new JPanel(){
         public void paint(Graphics g){
                    g.clearRect(0, 0, getSize().width, getSize().height);
                    picture.paintIcon(this, g, 0, 0); //这里就是把picture作为背景
                    paintComponents(g);
            }
     };


    3:如何使FileDialog设置为保存的时候自动以某种后缀名保存呢?
    网上看到很多人用chooser来实现,我感觉异常麻烦,如果只需要保存一种格式的话,其实最简单的办法就是:

FileDialog fd=new FileDialog(f,"Save",FileDialog.SAVE);
fd.setVisible(true);
File f1 = new File(fd.getDirectory(),fd.getFile()+".jpg");

    直接在getFile后面加上后缀就可以了,然后把f1通过FileOutputStream写到文件中。

    4:如何实现全屏:

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.*;

public class java_test implements KeyListener {
        JFrame frame;

        int hight, width;

        Graphics g;

        Image plaatje;

        BufferStrategy bufferStrategy;
       
       
       
        boolean running = true;
        JButton b1;

        int x = 0, y = 0;

        public java_test() {
        try{
        b1 = new JButton("OK");
   
                frame = new JFrame("MoveNL");
                frame.addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                                System.exit(0);
                        }
                });
                frame.setUndecorated(true);
                frame.setIgnoreRepaint(true);
                frame.setFocusable(true);
                frame.addKeyListener(this);
                GraphicsEnvironment gEnv = GraphicsEnvironment
                                .getLocalGraphicsEnvironment();
                GraphicsDevice gDev = gEnv.getDefaultScreenDevice();
                DisplayMode dplMode = new DisplayMode(1024, 768, 16,
                                DisplayMode.REFRESH_RATE_UNKNOWN);
                gDev.setFullScreenWindow(frame);
                gDev.setDisplayMode(dplMode);
                frame.setVisible(true);
                frame.createBufferStrategy(2);
                bufferStrategy = frame.getBufferStrategy();
                plaatje = Toolkit.getDefaultToolkit().getImage("Winter.jpg");
                render();
                frame.dispose();
        }catch(Exception exe){
            exe.printStackTrace();
        }

        }

        private void render() {
                while (running) {
                        try {
                                g = bufferStrategy.getDrawGraphics();
                                g.fillRect(0, 0, 1024, 768);
                                g.drawImage(plaatje, x, y, null);
                        } finally {
                                g.dispose();
                        }
                        bufferStrategy.show();
                }
        }

        public void keyTyped(KeyEvent e) {
                switch (e.getKeyChar()) {
                case 'q':
                        running = false;
                        break;
                }
                switch (e.getKeyChar()) {
                case 'z':
                        x++;
                        y++;
                        break;
                }
                return;
        }

        public void keyReleased(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        public static void main(String[] args) {
                new java_test();
        }
}


  5:如何通过一个按钮事件使该图片全屏,比方说通过

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class test implements ActionListener{
public static void main(String[] args) {
new test();
}

public test(){
JFrame f1;
JButton b1;
f1 = new JFrame("test");
b1 = new JButton("OK");
f1.add(b1);
f1.setBounds(50, 50, 300, 300);
f1.setEnabled(true);
f1.setVisible(true);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
new java_test();
}
}

这个小程序来调用呢?
需用到以下代码:

public void actionPerformed(ActionEvent e) {
        new Thread(new Runnable(){
            @Override
            public void run() {
                // TODO Auto-generated method stub

                
                new java_test();
            }
        }).start();
    }


以上记得有点乱,准备写个图片浏览器把这些东东串起来。不过还是过完十一,陪完老爹老娘先。
阅读(1355) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~