一、Graphics类与图形绘制
1、Component.getGraphics方法与Graphics类
2、Graphics.drawLine(Int x1,Int y1,Int x2,Int y2)方法
3、Graphics.drawString(String str,Int x,Int y)方法
drawString的坐标x,y是字符串str的左下角坐标(其他语言一般是左上角)
例子:以鼠标在窗口中按下时的位置作为起点,释放时作为终点画出直线。并显示起止点坐标
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
public class DrawLine extends Frame
{
public DrawLine() throws HeadlessException
{
this.addMouseListener(new MouseAdapter()
{
int OrgX, OrgY;
public void mousePressed(MouseEvent e)
{
OrgX = e.getX();
OrgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
Graphics g = getGraphics(); //每调用一次getGraphics()就会返回一个Graphics类对象,所以要先生成一个Grahpics类对象,再用这个对象来操作
g.setColor(Color.RED);
g.drawLine(OrgX, OrgY, e.getX(), e.getY());
g.drawString("x="+OrgX+",y="+OrgY, OrgX, OrgY);
g.drawString("x="+e.getX()+",y="+e.getY(), e.getX(), e.getY());
}
});
}
public static void main(String[] args)
{
System.out.println("staring drawing line");
DrawLine mainFrame = new DrawLine();
mainFrame.setSize(400, 400);
mainFrame.setTitle("DrawLine");
mainFrame.setVisible(true);
}
}
二、组件重绘的处理
在上面的例子中,当窗口被最小化后再还原时(这个过程也叫"曝光"),界面上原有的线条就没有了。这是因为系统在还原时只会还原原先的窗口和窗口上的子部件,对于用Graphics产生的内是不重绘的。
AWT线程在还原窗口及窗口的子部件的时候都会调用paint(Graphics g)方法。那么我们如果在paint方法中编写代码,把之前Graphics画的图形或文本重新画出来,就会让人感觉到窗口没有变。
从上面的流程图我们可以发现我们只要调用repaint()方法,repaint()方法会自动调用paint()方法进行绘制。现在我们把上面代码稍作修改,实现线条的重绘。
我们需要把线条的信息存储到一个集合对象中,通过调用repaint()方法,再从集合中实现重绘。
/*===========================重新定义一个类===================================*/
import java.awt.Graphics;
public class MyLine
{
private int OrgX, OrgY;
private int endX, endY;
public MyLine(int OrgX, int OrgY, int endX, int endY)
{
this.OrgX = OrgX;
this.OrgY = OrgY;
this.endX = endX;
this.endY = endY;
}
public void DrawMe(Graphics g)
{
g.drawLine(OrgX, OrgY, endX, endY);
}
}
/*==========================================================================*/
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.Vector;
public class DrawLine extends Frame
{
int OrgX, OrgY;
int endX, endY;
Vector vLines = new Vector();
public void paint(Graphics g)
{
Enumeration e =vLines.elements();
while (e.hasMoreElements()){
MyLine line = (MyLine)e.nextElement();
line.DrawMe(g);
}
}
public DrawLine() throws HeadlessException
{
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.drawLine(OrgX, OrgY, endX, endY);
g.drawString("x="+OrgX+",y="+OrgY, OrgX, OrgY);
g.drawString("x="+endX+",y="+endY, endX, endY);*/
vLines.add(new MyLine(OrgX, OrgY, endX, endY));
repaint();//repaint()会调用paint()方法,从集合中取得数据绘制直线,所以上面的代码可以屏蔽掉
}
});
}
public static void main(String[] args)
{
System.out.println("staring drawing line");
DrawLine mainFrame = new DrawLine();
mainFrame.setSize(400, 400);
mainFrame.setTitle("DrawLine");
mainFrame.setVisible(true);
}
}
三、图像显示
使用Graphics.drawImage(Image img,int x,int y,ImageObserver observer)方法显示图像。
使用Componet.getToolkit.getImage(String Path)语句获得Image实例对象
四、双缓冲技术
用双缓冲技术可以方便的实现重绘问题
Component.createImage方法创建内存Image对象。
在Image对象上进行绘制的结果就成了一幅图像。
在Image对象上执行与组件表面同样的绘制,Image对象中的图像是组件表面内容的复制,当组件重画时,
只需要将内存中的Image对象在组件上画出。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class DrawLine extends Frame
{
int OrgX, OrgY;
int endX, endY;
Graphics og = null;
Image oimg = null;
public void paint(Graphics g)
{
//重绘时显示Image就可以了
g.drawImage(oimg,0,0,this);
}
public DrawLine() throws HeadlessException
{
setSize(400, 400);
setTitle("DrawLine");
setVisible(true);
Dimension d = this.getSize();//获得窗口大小
oimg = this.createImage(d.width,d.height);//创建一个和窗口大小一致的Image对象
og = oimg.getGraphics();
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.drawLine(OrgX, OrgY, endX, endY);
g.drawString("x="+OrgX+",y="+OrgY, OrgX, OrgY);
g.drawString("x="+endX+",y="+endY, endX, endY);
//在Image对象上也同样画线
og.setColor(Color.RED);
og.drawLine(OrgX, OrgY, endX, endY);
og.drawString("x="+OrgX+",y="+OrgY, OrgX, OrgY);
og.drawString("x="+endX+",y="+endY, endX, endY);
}
});
}
public static void main(String[] args)
{
System.out.println("staring drawing line");
DrawLine mainFrame = new DrawLine();
}
}
阅读(863) | 评论(0) | 转发(0) |