zoukankan      html  css  js  c++  java
  • 【Java】Java绘图技术与事件处理机制

    摘要:本文主要通过实例讲解Java中绘图技术与事件处理机制

     // 此程序用来练习使用绘图技术!
      // Version:1.0
      // @Author:Yongchun_zha
      package cha08;
      import java.awt.*;
      import javax.swing.*;
      public class L8_1 extends JFrame
       {
       wdmb panl=null;
       public static void main(String[] args)
       {
        L81 l81=new L8_1();
       }
       public L8_1()
       {
        panl=new wdmb();
        this.add(panl);
    
        this.setTitle("Layout");//设置用户界面
        this.setSize(500,500);//设置窗口大小,数字代表像素
        this.setLocation(300,260);//设置窗体位置
        //this.setResizable(false);//设置窗体大小是否可调整
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭进程
        this.setVisible(true);//true 显示,false不显示
    
       }
      }
    
      class wdmb extends JPanel
      {
       public void paint(Graphics g)
       {
        //绘制图形
        g.setColor(Color.CYAN);//设置颜色
        g.drawOval(50, 50, 80, 80);
        g.drawLine(100,100,200,200);
        g.drawRect(100,100,200,200);
        g.draw3DRect(200,200,400,400,true);
        g.fillRect(10,10,20,20);//绘制填充图形
    
        //绘制图像
        Image tp=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/notebook.jpg"));
        g.drawImage(tp,100,80,20,20,this);
        //注:绘图用到的图片要拷贝到src目录下
    
        //绘制文字
        g.setColor(Color.RED);
        g.setFont(new Font("华文彩云",Font.BOLD,50));
        g.drawString("Hi,大家好",200,100);
       }
       //paint方法在以下三种情况下会被系统自动调用:①启动程序 ②窗口大小发生变化 ③调用repaint函数
      }
    • 事件监听

      // 此程序用来练习使用事件监听!
      // Version:1.0
      // @Author:Yongchun_zha
      package cha08;
      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      public class L8_2 extends JFrame implements ActionListener//接口实现监听
      {
      wdmb2 pa=null;
      JButton bon1,bon2;
      
      public static void main(String[] args)
      {
      L82 l82=new L8_2();
      }
      
      public L8_2()
      {
      pa=new wdmb2();
      pa.setBackground(Color.CYAN);
      
      bon1=new JButton("红色");
      bon2=new JButton("蓝色");
      
      this.setLayout(new BorderLayout());
      
      this.add(bon1,BorderLayout.NORTH);
      this.add(bon2, BorderLayout.SOUTH);
      this.add(pa);
      
      bon1.addActionListener(this);//this表示监听的是本类,按需添加所需监听的类即可
      bon1.setActionCommand("111");//可以对多个类进行监听
      bon2.addActionListener(this);
      bon2.setActionCommand("222");
      
      this.setTitle("Layout");//设置用户界面
      this.setSize(500,500);//设置窗口大小,数字代表像素
      this.setLocation(300,260);//设置窗体位置
      this.setResizable(false);//设置窗体大小是否可调整
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭进程
         this.setVisible(true);//true 显示,false不显示
      }
      
      public void actionPerformed(ActionEvent e)//接口的抽象构造函数
      {
      System.out.println("按钮起作用了!");
      
      if(e.getActionCommand().equals("111"))
      {
       System.out.println("红色红色");
       pa.setBackground(Color.RED);
      }
      
      else if(e.getActionCommand().equals("222"))
      {
       System.out.println("蓝色蓝色");
       pa.setBackground(Color.BLUE);
      }
      }
      
      }
      class wdmb2 extends JPanel
      {
      public void paint(Graphics g)
      {
      super.paint(g);
      }
      }
      
    • 键盘监听

      // 此程序用来练习使用事件监听!
      // Version:1.0
      // @Author:Yongchun_zha
      package cha08;
      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      public class L8_3 extends JFrame
      {
      wdmb4 pan=null;
      
      public static void main(String[] args)
      {
      L83 l83=new L8_3();
      }
      
      public L8_3()
      {
      pan=new wdmb4();
      
      this.add(pan);
      this.addKeyListener(pan);
      
      this.setTitle("Layout");//设置用户界面
      this.setSize(500,500);//设置窗口大小,数字代表像素
      this.setLocation(300,260);//设置窗体位置
      this.setResizable(false);//设置窗体大小是否可调整
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭进程
         this.setVisible(true);//true 显示,false不显示
      
      }
      }
      class wdmb4 extends JPanel implements KeyListener
      {
      int x=100, y=100;
      
      public void paint(Graphics g)
      {
      super.paint(g);
         g.fillRect(x,y,30,30);
      
      }
      
      public void keyTyped(KeyEvent e)
      {
      //有字符输出的函数
      }
      
      public void keyPressed(KeyEvent e)
      {
      System.out.println("键盘被按下: "+(char)e.getKeyCode());
      if(e.getKeyCode()==KeyEvent.VK_DOWN)
      {
       System.out.println("Down");
       y+=10;
      }
      
      else if(e.getKeyCode()==KeyEvent.VK_UP)
      {
       System.out.println("Up");
       y-=10;
      }
      
      else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
      {
       System.out.println("Right");
       x+=10;
      }
      
      else if(e.getKeyCode()==KeyEvent.VK_LEFT)
      {
       System.out.println("Left");
       x-=10;
      }
      
      this.repaint();
      
      }
      
      public void keyReleased(KeyEvent e)
      {
      //按键抬起
      }
      }
    • 鼠标监听

      // 此程序用来练习使用鼠标监听!
      // Version:1.0
      // @Author:Yongchun_zha
      package cha08;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      public class L8_4 extends JFrame
      {
      wdmb5 panl=null;
      
      public static void main(String[] args)
      {
      L84 l84=new L8_4();
      }
      public L8_4()
      {
      panl=new wdmb5();
      
      this.add(panl);
      
      this.addMouseListener(panl);
      this.addMouseMotionListener(panl);
      
      this.setTitle("Layout");//设置用户界面
      this.setSize(500,500);//设置窗口大小,数字代表像素
      this.setLocation(300,260);//设置窗体位置
      this.setResizable(false);//设置窗体大小是否可调整
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭进程
         this.setVisible(true);//true 显示,false不显示
      
      }
      }
      class wdmb5 extends JPanel implements MouseListener,MouseMotionListener
      {
      public void paint(Graphics g)
      {
      super.paint(g);
      }
      
      public void mouseClicked(MouseEvent e) //鼠标单击
      {
      System.out.print("鼠标点击的位置是: ");
      System.out.println("("+e.getX()+","+e.getY()+")");
      }
      
      public void mousePressed(MouseEvent e)
        {
         System.out.println("鼠标被按下");
      }
      
        public void mouseReleased(MouseEvent e)
      {
      System.out.println("鼠标被松开");
      }
      
      public void mouseEntered(MouseEvent e)
      {
      System.out.println("鼠标移动到界面中");
      }
      
      public void mouseExited(MouseEvent e)
        {
         System.out.println("鼠标离开界面");
      }
      
      public void mouseDragged(MouseEvent e)
      {
      System.out.println("鼠标当前位置是:");
      System.out.println("("+e.getX()+","+e.getY()+")");
      }
      
      public void mouseMoved(MouseEvent e)
      {
      System.out.println("鼠标当前位置是:");
      System.out.println("("+e.getX()+","+e.getY()+")");
      }
      }
      
    • 窗口监听

      // 此程序用来练习使用窗口监听!
      // Version:1.0
      // @Author:Yongchun_zha
      package cha08;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      public class L8_5 extends JFrame
      {
        Wdmb6 mb=null;
      
        public static void main(String[] args)
        {
         L85 lx=new L85();
      }
      
      L8_5()
      {
      mb=new Wdmb6();
      this.add(mb);
      this.addWindowListener(mb);
      
      
      this.setSize(400,300);
      this.setLocation(300,280);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setVisible(true);
      }
      }
      class Wdmb6 extends JPanel implements WindowListener
      {
      public void paint(Graphics g)
      {
      super.paint(g);
      }
      @Override
      public void windowOpened(WindowEvent e)
      {
      System.out.println("窗口打开");
      }
      public void windowClosing(WindowEvent e)
      {
       System.out.println("窗口关闭");
      }
        public void windowClosed(WindowEvent e)
        {
      }
        public void windowIconified(WindowEvent e)
        {
         System.out.println("最小化");
      }
      public void windowDeiconified(WindowEvent e)
      {
      System.out.println("从最小化变成正常");
      }
      public void windowActivated(WindowEvent e)
      {
      System.out.println("窗口被激活");
      }
      public void windowDeactivated(WindowEvent e)
      {
      
      }
      }
      

  • 相关阅读:
    HDU4628+状态压缩DP
    Javascript 去掉字符串前后空格的五种方法
    Javascript 数组之判断取值和数组取值
    ASP.NET MVC 出现错误 “The view 'XXX' or its master was not found or no view engine support”
    ASP.NET MVC 页面调整并传递参数
    ASP.NET MV3 部署网站 报"Could not load file or assembly ' System.Web.Helpers “ 错的解决方法
    ASP.NET MVC 控制器向View传值的三种方法
    CSharp 如何通过拼接XML调用存储过程来查询数据
    SQLServer : EXEC和sp_executesql的区别
    关于SQLServer2005的学习笔记—异常捕获及处理
  • 原文地址:https://www.cnblogs.com/yczha/p/13160274.html
Copyright © 2011-2022 走看看