⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grawing.java

📁 画图程序 画直线 矩形 圆 椭圆 可填充
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Grawing extends JFrame {
   private JButton selectcolor;
   private Color color = Color.black;
   private JComboBox shape;
   private JCheckBox fill;
   private JLabel cur_color,s_shape;
    private Container container;
    private String shapes[]={"直线","椭圆","矩形","圆角矩形"};
    private int type;
    private boolean f=false;
    private int x,y,x1,y1,height,width;
    
    
    
    public Grawing()
    {
    	super("画图程序");
    	container = getContentPane();
        container.setLayout( new FlowLayout() );
        
        s_shape=new JLabel("选择形状");
        
        cur_color=new JLabel("    当前颜色");
        
        selectcolor=new JButton("选择颜色");
        shape =new JComboBox(shapes);
        shape.setMaximumRowCount(3);
        
        
        cur_color.setForeground(color);
      
       
       fill=new JCheckBox("填充");
      BoxHandler handler = new BoxHandler();
      shape.addItemListener( handler );
      fill.addItemListener( handler );
      
      selectcolor.addActionListener(

         new ActionListener() {  

            // handle JComboBox event
            public void actionPerformed( ActionEvent e )
            {
               color=JColorChooser.showDialog(Grawing.this,"选择颜色",color);
               cur_color.setForeground(color);
               
			   
            }

         }  // end anonymous inner class

      ); // end call to addItemListener


		MouseHandler mhandler = new MouseHandler();
        addMouseListener(mhandler);
        
         addMouseMotionListener(

         new MouseMotionAdapter() {  // anonymous inner class

            // store drag coordinates and repaint
            public void mouseDragged( MouseEvent event )
            {
               	x1=event.getX();
   		        y1=event.getY();
   				repaint();
               
               }
            }   
        
          

      );
       container.add(s_shape);
       container.add(shape);
       
        container.add(selectcolor);
       container.add(cur_color);
       container.add(fill);
      
    //   container.setBackground(Color.white);
    setLocation(200,200);
        setSize(350,400 );
        setVisible( true );

   } 
   
   private class BoxHandler implements ItemListener {
     
   
      public void itemStateChanged( ItemEvent event )
      {
         
         if ( event.getSource() == shape )
           
			   {if ( event.getStateChange() == ItemEvent.SELECTED )
                  type=shape.getSelectedIndex();
                  x=0;y=0;x1=0;y1=0;
                  repaint();
                 
                  }
               
       
         if ( event.getSource() == fill )
           
          	if(fill.isSelected())
           			f=true;
           			else
           			f=false;
           		

      } 

   } 
   
   private class MouseHandler extends MouseAdapter{
   	
   	public void mousePressed(MouseEvent event)
   	{
   		x=event.getX();
   		y=event.getY();
   			
   	}
   	
   	public void mouseReleased(MouseEvent event)
   	{
   		
   		x1=event.getX();
   		y1=event.getY();
   	
   			
   	}
   	
   	
   }
   
   public void paint( Graphics g )
   {
      super.paint( g ); // clears drawing area
      
      g.setColor(color);

     switch(type)
     {
     	case 0:			g.drawLine(x,y,x1,y1);break;
     			
     	case 1:if(x1>x)
     			{
     			
     			 g.drawOval(x,y,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)));
     			 if(f)
     			  g.fillOval(x,y,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)));
     			  }
     			  else
     			  {
     			
     			  g.drawOval(x1,y1,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)));
     			 if(f)
     			  g.fillOval(x1,y1,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)));
     			  }
     			  
     			 break;
     			 
     	case 2:  if(x1>x){
     
     			  g.drawRect(x,y,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)));
     			 if(f)
     			  g.fillRect(x,y,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)));
     			  }
     			  else
     			  {
     
     			  g.drawRect(x1,y1,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)));
     			 if(f)
     			  g.fillRect(x1,y1,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)));}
     			  
     			 break;
     	case 3:  
     			if(x1>x)
     			{
     			
     			 g.drawRoundRect(x,y,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)),(int)((Math.abs(x1-x))/5),(int)((Math.abs(y1-y))/5));
     			if(f)
     			  g.fillRoundRect(x,y,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)),(int)((Math.abs(x1-x))/5),(int)((Math.abs(y1-y))/5));
     			  }
     			 else
     			  {
     			
     			 g.drawRoundRect(x1,y1,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)),(int)((Math.abs(x1-x))/5),(int)((Math.abs(y1-y))/5));
     			  if(f)
     			  g.fillRoundRect(x1,y1,(int)(Math.abs(x1-x)),(int)(Math.abs(y1-y)),(int)((Math.abs(x1-x))/5),(int)((Math.abs(y1-y))/5));
     			  }
     		
     			 break;
     }
   }


   
   

   
   public static void main( String args[] )
   {
     Grawing application=new Grawing();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }
   
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -