当前位置: 代码迷 >> Eclipse >> Java 显示多张图片,该怎么解决
  详细解决方案

Java 显示多张图片,该怎么解决

热度:29   发布时间:2016-04-23 13:53:20.0
Java 显示多张图片
如何在Java界面上同时显示多张图片?我用了继承Thread类,结果发现显示时候屏幕很闪,怎么办,有没有更好的办法?或者如何解决这个问题?
Java code
public class SolarSystem extends JFrame{            public void lode(){        new PaintThread().start();    }    Image sun = Toolkit.getDefaultToolkit().getImage("image/sun.jpg");    Image earth = Toolkit.getDefaultToolkit().getImage("image/earth.jpg");        public void paint(Graphics g){        g.setColor(Color.blue);        g.drawImage(backGround , 0 , 0 , null);        g.drawImage(sun , 435 , 275 , null);                t = t + Math.PI / 36.5;        t1 = t + Math.PI / 0.1;                g.drawOval(120, 145, 600, 400);        g.drawImage(earth , (int)x ,(int)y, null);        x = 375 + 300 * Math.cos(t);        y = 285 + 200 * Math.sin(t);        }            public static void main(String[] args){        int x_size, y_size;       //横纵坐标        x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());        y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());                SolarSystem solarSystem = new SolarSystem();        solarSystem.setSize(900, 800);        solarSystem.setLocation((x_size - 800) / 2, (y_size - 780) / 2);        solarSystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            solarSystem.setVisible(true);        //earth.setResizable(false);                solarSystem.lode();    }        private class PaintThread extends Thread{        public void run(){            while(true){                repaint();                try {                    Thread.sleep(20);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }}


------解决方案--------------------
双缓冲啊...
------解决方案--------------------

private Image offScreenImage = null;
protected static final int WIN_WIDTH = 800;
protected static final int WIN_HEIGHT = 600;
@Override
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(WIN_WIDTH, WIN_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.BLACK);
gOffScreen.fillRect(0, 0, WIN_WIDTH, WIN_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
------解决方案--------------------
继承 JPanel,在 paintComponent方法里绘制。使用 javax.swing.Timer 定时更新。
然后 加到 JFrame里。

继承 JFrame 重写 paint 还是 awt的思路。swing不同了
  相关解决方案