图像显示
1 使用Graphics.drawImage(Image img, int x, int y, ImageObserver observer)
方法显示图像。
2 使用Component.getToolkit.getImage(String path)语句获得Image实例对象。
例:
package drawimage;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class DrawImage extends Frame
{
private static final long serialVersionUID = 1L;
public DrawImage()
{
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public static void main(String[] args)
{
System.out.println("Starting DrawImage.");
DrawImage mainFrame = new DrawImage();
Image img = mainFrame.getToolkit().getImage("love.jpg");
mainFrame.setSize(400,400);
mainFrame.setTitle("DrawImage");
mainFrame.setVisible(true);
Graphics g = mainFrame.getGraphics();
while(!g.drawImage(img,150,150,mainFrame));
}
}
图像重画
package drawimage;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class DrawImage extends Frame
{
Image img = getToolkit().getImage("love.jpg");
private static final long serialVersionUID = 1L;
public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}
public DrawImage()
{
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public static void main(String[] args)
{
System.out.println("Starting DrawImage.");
DrawImage mainFrame = new DrawImage();
mainFrame.setSize(400,400);
mainFrame.setTitle("DrawImage");
mainFrame.setVisible(true);
/*Graphics g = mainFrame.getGraphics();
while(!g.drawImage(img,150,150,mainFrame));
*/
}
}
双缓冲技术:
1 Component.createImage方法创建内存Image对象
2 在Image对象上进行绘制的结果就成了一幅图像
3 在Image对象上执行与组件表面同样的绘制,Image
对象中的图像就是组件表面内容的复制,当组件重画 时,只需要将内存中的Image对象在组件上画出;
package drawline;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.util.Enumeration;
import java.util.Vector;
public class DrawLine1 extends Frame
{
int orgX;
int orgY;
int endX;
int endY;
Image oimg = null;
Graphics og = null;
Vector<MyLine> vLines = new Vector<MyLine>();
public void paint(Graphics g)
{
if(oimg != null)
{
g.drawImage(oimg,0,0,this);
}
/*g.setColor(Color.RED);
g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
Enumeration<MyLine> e = vLines.elements();
while(e.hasMoreElements())
{
MyLine line = (MyLine)e.nextElement();
line.drawMe(g);
}*/
//g.drawLine(orgX, orgY, endX, endY);
}
private static final long serialVersionUID = 9004940250333651314L;
public DrawLine1()
{
setSize(400,400);
setTitle("Drawline");
setVisible(true);
Dimension d = getSize();
oimg = createImage(d.width,d.height);
og = oimg.getGraphics();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
Graphics g = getGraphics();
g.setColor(Color.RED);
g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
g.drawString(orgX + "," + orgY,orgX,orgY);
g.drawString(e.getX() + "," + e.getY(), e.getX(),e.getY());
g.drawLine(orgX, orgY, e.getX(),e.getY());
og.setColor(Color.RED);
og.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
og.drawString(orgX + "," + orgY,orgX,orgY);
og.drawString(e.getX() + "," + e.getY(), e.getX(),e.getY());
og.drawLine(orgX, orgY, e.getX(),e.getY());
/*vLines.add(new MyLine(orgX,orgY,endX,endY));
repaint();*/
}//为什么用getGraphics().setColor(Color.RED)是不用导java.awt.Graphics包
//Enumeration<MyLine>???????????????????????????
});
}
public static void main( String[] args) {
System.out.println("String DrawLine");
new DrawLine1();
}
}
//构建主框架对象时是先调用paint()还是构造函数
posted on 2008-08-10 21:37
lanxin1020 阅读(169)
评论(0) 编辑 收藏 所属分类:
j2se