当前位置: 技术问答>java相关
怎样在Canvas上画图....(UP者有分喽!!!!)
来源: 互联网 发布时间:2017-04-08
本文导语: 要求用JDK1.3,工具最好是JBuild 完成的 能发源码来最最好 lifeforsoft@hotmail.com | //ImagePain.java import java.awt.*; import java.awt.event.*; public class ImagePain extends Frame { public ImagePain() { ImageCanvas i...
要求用JDK1.3,工具最好是JBuild
完成的
能发源码来最最好 lifeforsoft@hotmail.com
|
//ImagePain.java
import java.awt.*;
import java.awt.event.*;
public class ImagePain extends Frame
{
public ImagePain()
{
ImageCanvas images = new ImageCanvas("images\photo.jpg");
add(images);
}
public static void main(String[] args)
{
ImagePain f = new ImagePain();
f.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e)
{
System.exit(0);
}
});
f.setSize(800, 600);
f.show();
}
class ImageCanvas extends Canvas
{
Image i;
public ImageCanvas(String imageName)
{
i = Toolkit.getDefaultToolkit().getImage(imageName);
MediaTracker mt = new MediaTracker(this);
try
{
mt.addImage(i,0);
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
g.drawImage(i,0,0,this);
}
public Dimension getPreferredSize()
{
int w = i.getWidth( this );
int h = i.getHeight( this );
return new Dimension( w, h );
}
}
}
import java.awt.*;
import java.awt.event.*;
public class ImagePain extends Frame
{
public ImagePain()
{
ImageCanvas images = new ImageCanvas("images\photo.jpg");
add(images);
}
public static void main(String[] args)
{
ImagePain f = new ImagePain();
f.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e)
{
System.exit(0);
}
});
f.setSize(800, 600);
f.show();
}
class ImageCanvas extends Canvas
{
Image i;
public ImageCanvas(String imageName)
{
i = Toolkit.getDefaultToolkit().getImage(imageName);
MediaTracker mt = new MediaTracker(this);
try
{
mt.addImage(i,0);
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
g.drawImage(i,0,0,this);
}
public Dimension getPreferredSize()
{
int w = i.getWidth( this );
int h = i.getHeight( this );
return new Dimension( w, h );
}
}
}
|
用paint()方法三