随笔 - 63  文章 - 0  trackbacks - 0
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

图像显示

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 @ 2008-08-10 21:37 lanxin1020 阅读(169) | 评论 (0)编辑 收藏

 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 @ 2008-08-10 07:51 lanxin1020 阅读(261) | 评论 (0)编辑 收藏
初学警示:


面向对象编程思想:
编程人员脑海里面想的问题应该是:程序运行时的各个时刻的各个对象的内存布局和变化状态,以及每行代码执行后对这些对象产生的影响。而非顶住程序中静止不变的代码,孤立的琢磨每条语句的语法是否正确。
posted @ 2008-08-07 15:53 lanxin1020 阅读(147) | 评论 (0)编辑 收藏
仅列出标题
共7页: 上一页 1 2 3 4 5 6 7