shapetalk.java

来自「java画图板」· Java 代码 · 共 1,383 行 · 第 1/4 页

JAVA
1,383
字号
		/**the button copy*/
		buttonCopy.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ev)
			{
				if(selectedObjectCopy >= 0)
				{
					centerCenter.add(new ImageInfo(centerCenter.getObj(selectedObjectCopy)));
					if(centerCenter.getObj(selectedObjectCopy).fill == 4)
					{
						centerCenter.store.elementAt(centerCenter.length()-1).x += 5;
						centerCenter.store.elementAt(centerCenter.length()-1).y += 5;
					}else{
						AffineTransform moveMatrix = new AffineTransform();
						moveMatrix.setToTranslation(5, 5);
						centerCenter.store.elementAt(centerCenter.length()-1).shape = moveMatrix.createTransformedShape(centerCenter.store.elementAt(centerCenter.length()-1).shape);
					}
					centerCenter.repaint();
					if(connected == 1){
						ImageInfo tempImageInfo = new ImageInfo(centerCenter.getObj(centerCenter.length() -1));
						centerCenter.store.remove(centerCenter.length()-1);
						tempImageInfo.flag = 1;
						talkSec.drawToServer(tempImageInfo);
					}
				}
			}
		});
		
		/**the button redo*/
		buttonResume.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ev)
			{
				if(unDoPanel.length() < 1)
					return;
				if(connected == 0)
				{
					centerCenter.add(unDoPanel.getObj(unDoPanel.length()-1));
					unDoPanel.remove(unDoPanel.length()-1);
					centerCenter.repaint();
				}else if(connected == 1)
				{
					talkSec.drawToServer(new ImageInfo(3));
				}
			}
		}	
		);
		
		/**the button clear*/
		buttonClear.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ev)
			{
				if(connected == 0){
					unDoPanel.clear();
					centerCenter.clear();
					centerCenter.repaint();
				}else if(connected == 1){
					talkSec.drawToServer(new ImageInfo(4));
				}
			}
		}	
		);	
			
		/**the button new*/
		newImage.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				DrawPanel temp = centerCenter;
				temp.setVisible(false);
				centerCenter = new DrawPanel();
				centerCenter.setBackground(Color.WHITE);
				unDoPanel.clear();
				centerCenter.setVisible(true);
				centerCenter.repaint();
			}
		});
		/**the button open*/
		open.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ev) 
			{
				JFileChooser openFile = new JFileChooser();
				File currentFile = new File(".");
				openFile.setCurrentDirectory(currentFile);
				int selected = openFile.showOpenDialog(frame);
				if (selected == JFileChooser.APPROVE_OPTION)
				{
					File file= openFile.getSelectedFile();
					try{
						FileInputStream ifs = new FileInputStream(file);
						ObjectInputStream iss = new ObjectInputStream(ifs);
						try{
							centerCenter.setVisible(false);
							centerCenter = (DrawPanel)iss.readObject();
							centerPanel.add(centerCenter, BorderLayout.CENTER);
							unDoPanel.clear();
							centerCenter.repaint();
						}catch(IOException ex){
							JOptionPane.showMessageDialog(frame, "Cannot open the file!", "error", JOptionPane.INFORMATION_MESSAGE);
						}catch(ClassNotFoundException ex){
							JOptionPane.showMessageDialog(frame, "Cannot open the file!", "error", JOptionPane.INFORMATION_MESSAGE);
						}
						iss.close();
						ifs.close();

					}catch(FileNotFoundException ex){
						JOptionPane.showMessageDialog(frame, "Cannot open the file!", "error", JOptionPane.INFORMATION_MESSAGE);
					}catch(IOException ex){
						JOptionPane.showMessageDialog(frame, "Open the file error!", "error", JOptionPane.INFORMATION_MESSAGE);	
					}
										
				}
			}
		});
		
		/**the button save*/
		save.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ev) 
			{
				JFileChooser saveFile = new JFileChooser();
				File currentFile = new File(".");
				saveFile.setCurrentDirectory(currentFile);
				int selected = saveFile.showSaveDialog(frame);
				if (selected == JFileChooser.APPROVE_OPTION)
				{
					File file= saveFile.getSelectedFile();
					if(file.exists() == true)
					{
						int change = JOptionPane.showConfirmDialog(null, "Do you confirm to change "+file.getName()+"?", "confirm", JOptionPane.YES_NO_OPTION);
						if(change != JOptionPane.OK_OPTION)
						{
							return;
						}
					}
					try{
						FileOutputStream ofs = new FileOutputStream(file);
						ObjectOutputStream oss = new ObjectOutputStream(ofs);
						oss.writeObject(centerCenter);
						oss.close();
						ofs.close();

					}catch(FileNotFoundException ex){
						JOptionPane.showMessageDialog(frame, "Cannot open the file!", "error", JOptionPane.INFORMATION_MESSAGE);
					}catch(IOException ex){
						JOptionPane.showMessageDialog(frame, "Writing to file error!", "error", JOptionPane.INFORMATION_MESSAGE);	
					}
										
				}
			}
		});
		
		/**the button save as*/
		saveas.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ev) 
			{
				JFileChooser saveFile = new JFileChooser();
				File currentFile = new File(".");
				saveFile.setCurrentDirectory(currentFile);
				int selected = saveFile.showSaveDialog(frame);
				if (selected == JFileChooser.APPROVE_OPTION)
				{
					File file= saveFile.getSelectedFile();
					if(file.exists() == true)
					{
						int change = JOptionPane.showConfirmDialog(null, "Do you confirm to change "+file.getName()+"?", "confirm", JOptionPane.YES_NO_OPTION);
						if(change != JOptionPane.OK_OPTION)
						{
							return;
						}
					}
					try{
						FileOutputStream of = new FileOutputStream(file);
						OutputStreamWriter os = new OutputStreamWriter(of);
						os.close();
						of.close();
					}catch(FileNotFoundException ex){
						JOptionPane.showMessageDialog(frame, "Cannot open the file!", "error", JOptionPane.INFORMATION_MESSAGE);
					}catch(IOException ex){
						JOptionPane.showMessageDialog(frame, "Writing to file error!", "error", JOptionPane.INFORMATION_MESSAGE);	
					}
										
				}
			}
		});
		
		//mouse event to draw
		class MouseDraw implements MouseListener{
			
			Shape tempShape;
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			
			/**when the mouse pressed */
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				x0 = (int)e.getPoint().getX();
				y0 = (int)e.getPoint().getY();
				posXText.setText(String.valueOf(x0));
				posYText.setText(String.valueOf(y0));
				
				if(state ==3)	//change color
				{
					selectedObject = centerCenter.check(x0, y0);
					if(selectedObject >= 0)
					{
						centerCenter.getObj(selectedObject).color = colorSet.getColor();
						centerCenter.repaint();
						if(connected == 1)
						{
							centerCenter.getObj(selectedObject).position = selectedObject;
							centerCenter.getObj(selectedObject).flag = 5;
							talkSec.drawToServer(centerCenter.getObj(selectedObject));
						}
					}
					return;
					
				}else if(state == 2){		//delete
					selectedObject = centerCenter.check(x0, y0);
					if(selectedObject >= 0)
					{
						if(connected == 0)
						{
							centerCenter.remove(selectedObject);
							centerCenter.repaint();
						}
						else if(connected == 1)
						{
							talkSec.drawToServer(new ImageInfo(6, selectedObject));
						}
					}
					return;
				}else if(state == 9){	//choose color
					selectedObject = centerCenter.check(x0, y0);
					if(selectedObject >= 0)
					{
						colorSet.setColor(centerCenter.getObj(selectedObject).color);
					}
					return;
				}

				if(changeButton.isSelected() || state == 10)	//change
				{
					int x = 100, w = 100, y = 100, h = 100;
					selectedObjectCopy = selectedObject = centerCenter.check(x0, y0);
					if(selectedObject != -1)
					{
						int f = centerCenter.getObj(selectedObject).fill;
						Shape s = centerCenter.getObj(selectedObject).shape;
						if(f == 0 || f == 1 || f == 2 || f ==3)
						{
							x = s.getBounds().x-3;
							w = s.getBounds().width+6;
							y = s.getBounds().y-3;
							h = s.getBounds().height+6;
						}else if(f ==4)
						{
							x = centerCenter.getObj(selectedObject).x-3;
							w = (int)(centerCenter.getObj(selectedObject).font.getSize()*centerCenter.getObj(selectedObject).chat.length()*0.6+6);
							y = (int)(centerCenter.getObj(selectedObject).y-centerCenter.getObj(selectedObject).font.getSize()*0.7)-3;
							h = (int)(centerCenter.getObj(selectedObject).font.getSize()*0.7+6);
						}
						centerCenter.add(new Rectangle2D.Double(x, y, w, h), Color.GREEN, 0, 2);
						centerCenter.repaint();
					}
					return;
				}
				if(state == 5)	//any line
				{
					aPoint = e.getPoint(); //get the mouse
					gPath = new GeneralPath(); 
					gPath.moveTo(aPoint.x,aPoint.y); //add path
					Color tempColor = colorSet.getColor();
					Integer tempThick = thickness.getValue();
					centerCenter.add(new ImageInfo(gPath, tempColor, tempThick));
					centerCenter.repaint(); 
					return;
				}
				Shape tempShape = new Line2D.Double(x0, y0, x0, y0);
				Color tempColor = colorSet.getColor();
				Integer tempThick = thickness.getValue();
				centerCenter.add(tempShape, tempColor, 2, tempThick);
				unDoPanel.clear();
				centerCenter.repaint();
			}

			@Override
			//moouse released
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				if(state == 3 || state ==9 || state ==2)
					return;
				
				x = (int)e.getPoint().getX();
				y = (int)e.getPoint().getY();

				//set size to output
				sizeXText.setText(String.valueOf(x0>x?x0-x:x-x0));
				sizeYText.setText(String.valueOf(y0>y?y0-y:y-y0));
				
				//change
				if(changeButton.isSelected() || state == 10)
				{
					if(selectedObject != -1)
					{
						centerCenter.remove(centerCenter.length()-1);
						centerCenter.repaint();
						centerCenter.store.elementAt(selectedObject).flag = 5;
						centerCenter.store.elementAt(selectedObject).position = selectedObject;
						talkSec.drawToServer(centerCenter.getObj(selectedObject));
					}
					selectedObject = -1;
					return;
				}

				/**the class used to show the string*/
				class StringFont {
					
					//the frame to show the font chooser
					JFrame fontFrame;
					Font font[];
					Vector<String> fontString;
					JList fontChooser;
					JScrollPane scrollPane;
					Integer size[] = {5,6,7,8,9,10,12,14,16,18,20,24,28,32,38,46,56,70,90,120,150};
					JComboBox sizeChooser;

⌨️ 快捷键说明

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