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

📄 draw.java

📁 本程序是程序设计第五版课后习题,实现画圆,画直线,画矩形
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Draw extends JFrame implements ActionListener ,
                                MouseListener,MouseMotionListener
{
	int     x1,y1,x2,y2;
	int     sgin = -1;
	int 	flag=0;
	Run 	textJPanel;
	JLabel  label = new JLabel(" Welcome !");
	Myshape shape = new Myline();
	JButton button1 = new JButton("Line"),
	        button2 = new JButton("Rectangle"),
	        button3 = new JButton("Roundrect"),
	        button4 = new JButton("Oval"),
	        buttonColor = new JButton("BackColor"),
	        button5 = new JButton("Clear");
	JCheckBox one ;
	ButtonGroup group;
	Container con;
	String out ="",out1="",out2="",out3="";
	                 
	public Draw()
	{
		super(" ");
		setSize(800,500);
		setLocation(250,250);
		setVisible(true);
		addMouseListener(this);
		addMouseMotionListener(this);
		
	    con = getContentPane();
        con.setLayout(null);
        con.setBackground(Color.white);
        
        one = new JCheckBox("Filled");
        one.setBackground(Color.blue);
        one.setBounds(680,190,100,20);
        con.add(one);
	    
	    RadioButtonHandler handler = new RadioButtonHandler();
	    one.addItemListener(handler);
       
        button1.setBounds(100,20,100,20);
        button1.addActionListener(this);
        con.add(button1);
        
        button2.setBounds(200,20,100,20);
        button2.addActionListener(this);
		con.add(button2);
		
		button3.setBounds(300,20,100,20);
		button3.addActionListener(this);
		con.add(button3);
		
		button4.setBounds(400,20,100,20);
		button4.addActionListener(this);
		con.add(button4);
		
		button5.setBounds(550,20,100,20);
		button5.setBackground(Color.green);
		button5.addActionListener(this);
		con.add(button5);
		
		buttonColor.setBounds(680,170,100,20);
		buttonColor.setBackground(Color.red);
		buttonColor.addActionListener(this);
		con.add(buttonColor);
		
		textJPanel = new Run();
		textJPanel.setLocation(0,400);
		con.add(textJPanel);
	
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == button1)
				{
	     	     	sgin = 1;
	     	     	out = "Line ";
	     	     	setDraw();
	     	     	repaint();
				}
		else if (e.getSource() == button2)
		        {
		        	sgin = 2;
		        	out = "Rect";
		        	setDraw();
		        	repaint();
		        }
		else if (e.getSource() == button3)
		 		{
		 			sgin = 3; 
		 			out = "GroundRect";
		 			setDraw();
		 			repaint();
		 		}
		else if (e.getSource() == button4)
		 		{
		 			sgin = 4;
		 			out = "Circle";
		 			setDraw();
		 			repaint();
		 		}
		else if (e.getSource() == button5)
				{
					sgin = -1;
					out = "2006116336";
					setDraw();
					repaint();	
				}
		else if(e.getSource() == buttonColor)
				{
					Color  color = Color.orange;
					color = JColorChooser.showDialog(	Draw.this,"Choose a color",color);
					con.setBackground(color);
					if(color == null)
					con.setBackground(color);
				}
		if(out != "")
	    setTitle(out);
	}
	
	public void mouseMoved(MouseEvent e){}
	public void mouseDragged(MouseEvent e){
		   if(sgin != -1)
	         {
	           	x2 = e.getX();
	           	y2 = e.getY();
	            out2 = out1+"  Piont2:[" + x2 +"," + y2 + "]";
               	setTitle(out2);
	 	       	repaint();
	 	       }	
	}
	
	public void mousePressed(MouseEvent event){
           if(sgin !=-1)
           {
           		x1 = event.getX();
           		y1 = event.getY();
           		out1 = out+"  Piont1:[" + x1 +"," + y1 + "]";	
           		setTitle(out1);  
           }    
	}
	
	public void mouseReleased(MouseEvent e)
	{
		if(sgin !=-1)
		{
		 	x2 = e.getX();
	     	y2 = e.getY(); 
         	setTitle(out2);
	 	 	repaint();
	 	 }	
		 
	}
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	
	public static void main(String args[])
	{
		Draw win = new Draw();
		win.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
	}
	
	public void paint(Graphics g)
	{
				super.paint(g);
				if(sgin == 1)
	           		{
	           			if(flag==0)  
	           				{shape = new Myline(x1, y1,0, x2, y2);shape.draw(g);}
	           			else
	           			    {shape = new Myline(x1, y1,1, x2, y2);shape.draw(g);}
	           	         
	           	     }
	           	else if(sgin == 2)
	           		{  
	           		
	           			if(flag==0)
	           				{shape = new Myrect(x1, y1,0, x2, y2);shape.draw(g);}
	           			else
	           				{shape = new Myrect(x1, y1,1, x2, y2);shape.draw(g);}
	           				
	           		}
	           	else if(sgin == 3)
	           		{ 
	           			if(flag==0) 
	           				{shape = new Myroundrect(x1, y1,0, x2, y2);shape.draw(g);}
	           			else 
	           				{shape = new Myroundrect(x1, y1,1, x2, y2);shape.draw(g);} 
	           	    }
	           	else if(sgin == 4)
	           		{
	           			if(flag==0)
	           				{shape = new Myoval(x1, y1,0, x2, y2); shape.draw(g);}
	           			else
	           				{shape = new Myoval(x1, y1,1, x2, y2); shape.draw(g);}
	           		}
	           	else if(sgin == 0)
					{	}
		
	}
	
	public void setDraw()
	{
		x1 = 0;
		x2 = 0;
		y1 = 0;
		y2 = 0;
	}
	
	public class RadioButtonHandler implements ItemListener
	{
		public void itemStateChanged(ItemEvent event)
		{
			
			if(event.getSource() == one)
			{
				if(flag == 0)
				{
					flag = 1;
				}
				else
				{
					flag = 0;
				}
			}
		}
	}

}

⌨️ 快捷键说明

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