随笔 - 63  文章 - 0  trackbacks - 0
<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 Graphics类与图形绘制
Componentp.getGraphics方法与Grahpics类

Graphics.drawLine(int x1,int y1,int x2, int y2)方法;
Graphics.drawStringString str,int x,int y)方法;
Graphics.drawSting方法的坐标参数是以坐下方为参考点的;


例:

package drawline;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class DrawLine extends Frame {

 private static final long serialVersionUID = 9004940250333651314L;

 public DrawLine()
 {
  this.addWindowListener(new WindowAdapter()
     {
      public void windowClosing(WindowEvent e) {
       dispose();
       System.exit(0);
      }
   
     });
  
  this.addMouseListener(new MouseAdapter()
     {
      
      int orgX;
      int orgY;
      public void mousePressed(MouseEvent e)
      {
       orgX = e.getX();
       orgY = e.getY();
       
      }
      public void mouseReleased(MouseEvent e)
      {
       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());
       
      }//为什么用getGraphics().setColor(Color.RED)是不用导java.awt.Graphics包
      
      
     });
 }

 public static void main( String[] args) {
  System.out.println("String DrawLine");
  DrawLine mainFrame = new DrawLine();
  mainFrame.setSize(400,400);
  mainFrame.setTitle("Drawline");
  mainFrame.setVisible(true);
  
  
 }
}

组件重建的处理:

1 组件重绘的原理  曝光
2 paint(Graphics g)的作用

AWT组件重绘路线:
AWT Thread ->paint() ->AWT Thread
repaint()+AWT Thread ->update(清除组件表面内容调用paint()方法)->paint();

例:
package drawline;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
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 DrawLine extends Frame
{
 int orgX;
 int orgY;
 int endX;
 int endY;
 Vector<MyLine> vLines = new Vector<MyLine>();
 
 public void paint(Graphics g)
 {
  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 DrawLine()
 {
  
  this.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());
       */
       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");
  DrawLine mainFrame = new DrawLine();
  mainFrame.setSize(400,400);
  mainFrame.setTitle("Drawline");
  mainFrame.setVisible(true);
  
  
 }
}


package drawline;

import java.awt.Graphics;


public class MyLine
{
 private int orgX;
 private int orgY;
 private int endX;
 private int 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)
 {
  System.out.println(orgX + "," + orgY + " "+endX + "," + endY);
  g.drawString(orgX + "," + orgY,orgX,orgY);
  g.drawString(endX + "," + endY,endX,endY);
  g.drawLine(orgX, orgY, endX, endY);
 }
 
 

}

posted on 2008-08-10 07:51 lanxin1020 阅读(261) 评论(0)  编辑  收藏 所属分类: j2se

只有注册用户登录后才能发表评论。


网站导航: