动态图片的JAVA代码 java动图制作
java中在窗体的创建一个面板中加入一个gif动态图
你是说 JPanel 吗?两种方法:
成都创新互联公司长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为阿瓦提企业提供专业的网站建设、网站制作,阿瓦提网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
添加 JLabel,给 JLabel 添加 ImageIcon,然后放置 JLabel 到 JPanel 上就行了:
JPanel pnl = new JPanel();
......
JLabel lblImage = new JLabel(new ImageIcon("你的文件名")); //创建一个带图片的 JLabel
lblImage.setBounds(0, 0, pnl.getWidth(), pnl.getHeight()); //设置 图片的横坐标、纵坐标、宽、高
pnl.add(lblImage); //放置这个 JLabel 到你的 JPanel 上面
还有一种方法是重写 JPanel 的 paint() 方法:
class YourPanel extends JPanel {
private Image img = Toolkit.getDefaultToolkit().createImage("你的文件名"); //用 Toolkit 的 createImage 方法来创建一个 Image 对象
@Override public void paint (Graphics g) {
super.paint(g);
g.drawImage(img, 0, 0, getWidth(), getHeight()); //绘制图片,第一个参数是 Image 对象,第二个参数是 横坐标,第三个是 纵坐标,第四个是 宽,第五个是 高
}
}
以上你隐式重写也可以:
JPanel pnl = new JPanel() {
@Override public void paint (Graphics g) {
...... //但是要注意这样如果你把 Image 声明成了局部变量或者参数形式,你需要声明成 final
}
}
java如何传输动态图片(GIF图片)?
实现思路:无论是何种类型,都是转换为流的形式进行的文件传输和存储。
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader bre = null;
OutputStreamWriter pw = null;//定义一个流
try {
String file = "D:/test/test.GIF";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
pw = new OutputStreamWriter(new FileOutputStream(“D:/New.GIF”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.GIF”实例
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
pw.write(str);//将要写入文件的内容,可以多次write
};
bre.close();
pw.close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。
求java做动画代码
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class TestImage extends Frame
{
private static final long serialVersionUID = 1L;
private static boolean PRESSED = false;
private static int pointX = 0;
private static int pointy = 200;
private static int RIGHT_GO = 0;
private static int LEFT_GO = 0;
private static int DIR = 0;
private static int ANGLE = 0;
private static int W = 50;
private static int H = 60;
private _Canvas canvas = null;
public TestImage ()
{
add (canvas = new _Canvas ());
setIgnoreRepaint (true);
requestFocus ();
}
public class _Canvas extends Canvas implements Runnable
{
private static final long serialVersionUID = 1L;
private BufferedImage bi = null;
private Image bufferedImage = null;
private Thread thread = null;
private long sleepTime = 10;
public _Canvas ()
{
try
{
bi = ImageIO.read (new File ("go.png"));
}
catch (IOException e)
{}
setBackground (Color.BLACK);
requestFocus ();
addKeyListener (new KeyListener ()
{
@Override
public void keyTyped ( KeyEvent e )
{}
@Override
public void keyReleased ( KeyEvent e )
{
RIGHT_GO = 0;
PRESSED = false;
}
@Override
public void keyPressed ( KeyEvent e )
{
// 38 40 37 39上下左右
DIR = e.getKeyCode ();
PRESSED = true;
}
});
}
@Override
public void paint ( Graphics g )
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint (RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage (rotateImage (bi.getSubimage (RIGHT_GO, LEFT_GO, W, H), ANGLE, true), pointX, pointy, W, H,
this);
g2d.dispose ();
}
@Override
public void update ( Graphics g )
{
if (null == bufferedImage)
{
bufferedImage = createImage (getWidth (), getHeight ());
}
Graphics bufferedG = bufferedImage.getGraphics ();
bufferedG.clearRect (0, 0, getWidth (), getHeight ());
paint (bufferedG);
bufferedG.dispose ();
g.drawImage (bufferedImage, 0, 0, this);
g.dispose ();
}
public void start ()
{
thread = new Thread (this);
thread.setName ("TestImage");
thread.setPriority (Thread.MIN_PRIORITY);
thread.start ();
}
public synchronized void stop ()
{
thread = null;
notify ();
}
@Override
public void run ()
{
Thread me = Thread.currentThread ();
while (thread == me !isShowing () || getSize ().width == 0)
{
try
{
Thread.sleep (555);
}
catch (InterruptedException e)
{
return;
}
}
while (thread == me isShowing ())
{
if (PRESSED)
{
try
{
if (DIR == 39)
{
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 0;
pointX = pointX + 1;
if (pointX 420)
{
ANGLE = 90;
pointX--;
pointy--;
W = 60;
H = 50;
}
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
}
else if (DIR == 37)
{
pointX = pointX - 1;
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 60;
if (pointX 0)
{
ANGLE = -90;
pointX++;
pointy--;
W = 60;
H = 50;
}
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
}
else if (DIR == 38)
{
W = 50;
H = 60;
pointy = 150;
ANGLE = 0;
RIGHT_GO = 100;
}
else if (DIR == 40)
{
W = 50;
H = 60;
ANGLE = 0;
pointy = 200;
RIGHT_GO = 0;
}
Thread.sleep (sleepTime);
repaint ();
}
catch (InterruptedException e)
{
break;
}
}
else
{
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 0;
pointX = pointX + 1;
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
if (pointX 500)
{
pointX = 0;
}
try
{
Thread.sleep (sleepTime);
repaint ();
}
catch (InterruptedException e)
{
break;
}
}
}
thread = null;
}
}
/**
* 旋转图像为指定角度
*
* @param degree
* @return
*/
public static BufferedImage rotateImage ( final BufferedImage image, final int angdeg, final boolean d )
{
int w = image.getWidth ();
int h = image.getHeight ();
int type = image.getColorModel ().getTransparency ();
BufferedImage img;
Graphics2D graphics2d;
( graphics2d = ( img = new BufferedImage (w, h, type) ).createGraphics () ).setRenderingHint (
RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate (d ? -Math.toRadians (angdeg) : Math.toRadians (angdeg), w / 2, h / 2);
graphics2d.drawImage (image, 0, 0, null);
graphics2d.dispose ();
return img;
}
public static void main ( String[] args )
{
EventQueue.invokeLater (new Runnable ()
{
@Override
public void run ()
{
final TestImage ti = new TestImage ();
ti.setSize (new Dimension (500, 300));
ti.setLocationRelativeTo (null);
ti.addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
@Override
public void windowDeiconified ( WindowEvent e )
{
ti.canvas.start ();
}
@Override
public void windowIconified ( WindowEvent e )
{
ti.canvas.stop ();
}
});
ti.setResizable (false);
ti.canvas.start ();
ti.setVisible (true);
}
});
}
}
新闻标题:动态图片的JAVA代码 java动图制作
标题路径:http://azwzsj.com/article/doccood.html