shapetalk.java

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

JAVA
1,383
字号
					JCheckBox bold;
					JCheckBox italic;
					JButton ok;
					JButton cancel;
					JLabel fontLabel;
					JLabel sizeLabel;
					JTextArea show;
					JPanel panelUpper;
					JPanel panelBottom;
					JPanel okPanel;
					JPanel sizePanel;
					Font setFont;
					int style;
					Font finalFont;
					String finalString;
						
					//the creation function
					StringFont()
					{	
						fontFrame =  new JFrame("Text Dialog");
						fontFrame.setLayout(new GridLayout(2,1));
						font = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
						fontString = new Vector<String>();
						for(int i = 0; i < font.length; i++)
						{
							fontString.add(font[i].getName());
						}
						fontChooser = new JList(fontString);
						scrollPane = new JScrollPane(fontChooser);
						sizeChooser = new JComboBox(size);
						bold = new JCheckBox("Bold");
						italic = new JCheckBox("Italic");
						ok = new JButton("OK");
						cancel = new JButton("Cancel");
						fontLabel = new JLabel("Font:");
						sizeLabel = new JLabel("Size:");
						show = new JTextArea();
						panelUpper = new JPanel(new BorderLayout());
						panelBottom = new JPanel(new BorderLayout());
						okPanel = new JPanel();
						sizePanel = new JPanel();
						
						fontFrame.add(panelUpper);
						fontFrame.add(panelBottom);
						panelUpper.add(fontLabel, BorderLayout.NORTH);
						panelUpper.add(scrollPane, BorderLayout.CENTER);
						panelUpper.add(sizePanel, BorderLayout.SOUTH);
						sizePanel.add(bold);
						sizePanel.add(italic);
						sizePanel.add(sizeLabel);
						sizePanel.add(sizeChooser);
						okPanel.add(ok);
						okPanel.add(cancel);
						panelBottom.add(show, BorderLayout.CENTER);
						panelBottom.add(okPanel, BorderLayout.SOUTH);
						
						setFont = new Font(fontString.elementAt(0),style, size[0]*2);
						fontChooser.setSelectedIndex(0);
						sizeChooser.setSelectedIndex(0);
						show.setFont(setFont);
						
						//choose font
						class GetListSelectionListener implements ListSelectionListener{
							public void valueChanged(ListSelectionEvent e)
							{
								setFont = new Font(fontString.elementAt(fontChooser.getSelectedIndex()), style, size[sizeChooser.getSelectedIndex()]*2);
								show.setFont(setFont);
							}
						}
						
						fontChooser.addListSelectionListener(new GetListSelectionListener());

						//choose size
						sizeChooser.addActionListener(new ActionListener()
						{
							public void actionPerformed(ActionEvent e)	
							{
								setFont = new Font(fontString.elementAt(fontChooser.getSelectedIndex()), style, size[sizeChooser.getSelectedIndex()]*2);
								show.setFont(setFont);
							}
						});
						
						//italic
						italic.addActionListener(new ActionListener()
						{
							public void actionPerformed(ActionEvent e)	
							{
								style = 0;
								if(italic.isSelected())
									style += Font.ITALIC;
								if(bold.isSelected())
									style += Font.BOLD;
								setFont = new Font(fontString.elementAt(fontChooser.getSelectedIndex()), style, size[sizeChooser.getSelectedIndex()]*2);
								show.setFont(setFont);
							}
						});
						
						//bold
						bold.addActionListener(new ActionListener()
						{
							public void actionPerformed(ActionEvent e)	
							{
								style = 0;
								if(italic.isSelected())
									style += Font.ITALIC;
								if(bold.isSelected())
									style += Font.BOLD;
								setFont = new Font(fontString.elementAt(fontChooser.getSelectedIndex()), style, size[sizeChooser.getSelectedIndex()]*2);
								show.setFont(setFont);
							}
						});
						
						//the ok button
						ok.addActionListener(new ActionListener()
						{
							public void actionPerformed(ActionEvent e)	
							{
								style = 0;
								if(italic.isSelected())
									style += Font.ITALIC;
								if(bold.isSelected())
									style += Font.BOLD;
								finalFont = new Font(fontString.elementAt(fontChooser.getSelectedIndex()), style, size[sizeChooser.getSelectedIndex()]*2);
								finalString = show.getText();
								Color tempColor = colorSet.getColor();
								if(connected == 1)
								{
									centerCenter.remove(centerCenter.length()-1);
									talkSec.drawToServer(new ImageInfo(finalString, finalFont, tempColor, x, y));
								}else if(connected == 0){
									centerCenter.setElementAt(centerCenter.length()-1, new ImageInfo(finalString, finalFont, tempColor, x, y));
								}
								centerCenter.repaint();
								fontFrame.setVisible(false);
							}
						});
						
						//the cancel button
						cancel.addActionListener(new ActionListener()
						{
							public void actionPerformed(ActionEvent e)	
							{
								fontFrame.setVisible(false);
							}
						});
						
						fontFrame.setSize(400, 500);
						fontFrame.setVisible(true);
					}
				}

				
				//StringFont drawString;
				if(state == 5)
				{
					Color tempColor = colorSet.getColor();
					Integer tempThick = thickness.getValue();
					if(connected == 1){
						centerCenter.remove(centerCenter.length()-1);
						talkSec.drawToServer(new ImageInfo(gPath, tempColor, tempThick));
					}
					return;
				}
				Integer tempFill = 0;
				Color tempColor = colorSet.getColor();
				if(drawButton.isSelected())
					tempFill = 0;
					else if(fillButton.isSelected())
						tempFill = 1;
				Integer tempThick = thickness.getValue();
				switch(state)
				{
				case 1:tempShape = new Ellipse2D.Double(x0>x?x:x0, y0>y?y:y0, x0>x?x0-x:x-x0, y0>y?y0-y:y-y0); break;
				case 4:	tempShape = new Line2D.Double(x0, y0, x, y);
						tempFill = 2;
						break;
				case 6:tempShape = new Rectangle2D.Double(x0>x?x:x0, y0>y?y:y0, x0>x?x0-x:x-x0, y0>y?y0-y:y-y0); break;
				case 7:tempShape = new RoundRectangle2D.Double(x0>x?x:x0, y0>y?y:y0, x0>x?x0-x:x-x0, y0>y?y0-y:y-y0, 40, 40); break;
				case 8:	StringFont drawString = new StringFont();return;
				default: tempShape = new Line2D.Double(x0, y0, x, y);
						tempFill = 2;
				}
				if(connected == 1)
				{
					centerCenter.remove(centerCenter.length()-1);
					talkSec.drawToServer(new ImageInfo(tempShape, tempColor, tempFill, tempThick));
				}else if(connected == 0){
					centerCenter.setElementAt(centerCenter.length()-1, tempShape, tempColor, tempFill, tempThick);
				}
				centerCenter.repaint();
			}
		}
		
		class MouseMove implements MouseMotionListener{
			@Override
			/**mouse dragged*/
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				x = (int)e.getPoint().getX();
				y = (int)e.getPoint().getY();
				sizeXText.setText(String.valueOf(x0>x?x0-x:x-x0));
				sizeYText.setText(String.valueOf(y0>y?y0-y:y-y0));
				returnMatrix = new AffineTransform(); 
				if(state == 9 || state == 2 || state == 3)
					return;
				if(changeButton.isSelected() || state == 10)
				{
					if(selectedObject != -1)
					{
						double x1, x2, y1, y2, w, h;
						w = x - x0;
						h = y - y0;
						int f = centerCenter.getObj(selectedObject).fill;
						Shape s = centerCenter.store.elementAt(selectedObject).shape;
						if(f == 4)
						{
							if(scale.isSelected()){
								Font fo = centerCenter.getObj(selectedObject).font;
								centerCenter.getObj(selectedObject).font = new Font(fo.getName(), fo.getStyle(), (int)(fo.getSize()+h/0.7));
								x0 = x;
								y0 = y;
							}else if(rotate.isSelected()){
							}else if(shear.isSelected()){
							}else if(translation.isSelected()){
								centerCenter.getObj(selectedObject).x += w;
								centerCenter.getObj(selectedObject).y += h;
								x0 = x;
								y0 = y;
							}
						}else
						{
							x1 = s.getBounds().x;
							x2 = s.getBounds().x+s.getBounds().width;
							y1 = s.getBounds().y;
							y2 = s.getBounds().y+s.getBounds().height;
							if(scale.isSelected()){
								changeMatrix.setTransform(((double)x+w-((double)x1+x2)/2)/(x-((double)x1+x2)/2),0,0,((double)y+h-((double)y1+y2)/2)/(y-((double)y1+y2)/2),(1-(((double)x+w-((double)x1+x2)/2)/(x-((double)x1+x2)/2)))*((double)x1+x2)/2,(1-(((double)y+h-((double)y1+y2)/2)/(y-((double)y1+y2)/2)))*((double)y1+y2)/2);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(s);
								x0 = x;
								y0 = y;
							}else if(rotate.isSelected()){
								changeMatrix.setToTranslation(-(x1+x2)/2, -(y1+y2)/2);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								centerCenter.store.elementAt(selectedObject).shape = returnMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								changeMatrix.setToTranslation((x1+x2)/2, (y1+y2)/2);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								changeMatrix.setToTranslation(-(x1+x2)/2, -(y1+y2)/2);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								changeMatrix.setToRotation(w/500, -h/500); 
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								changeMatrix.setToTranslation((x1+x2)/2, (y1+y2)/2);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
							}else if(shear.isSelected()){
								changeMatrix.setToTranslation(-(x1+x2)/2, -(y1+y2)/2);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								centerCenter.store.elementAt(selectedObject).shape = returnMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								changeMatrix.setToTranslation((x1+x2)/2, (y1+y2)/2);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								changeMatrix.setToTranslation(-(x1+x2)/2, -(y1+y2)/2);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								changeMatrix.setToShear(w/100, h/100); 
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								changeMatrix.setToTranslation((x1+x2)/2, (y1+y2)/2);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
							}else if(translation.isSelected()){
								changeMatrix.setToTranslation(w, h);
								centerCenter.store.elementAt(selectedObject).shape = changeMatrix.createTransformedShape(centerCenter.store.elementAt(selectedObject).shape);					
								x0 = x;
								y0 = y;
							}
						}
						centerCenter.repaint();
						if(rotate.isSelected()){
							returnMatrix.setToRotation(-w/500, h/500);
						}else if(shear.isSelected()){
							returnMatrix.setTransform(1+(w/100/(100/h-w/100)), -1/(100/h-w/100), -w/h, 100/h, 0, 0);
						}
					}
					return;
				}
				if(state ==5)
				{
					aPoint = e.getPoint();
					gPath.lineTo(aPoint.x, aPoint.y); 
					gPath.moveTo(aPoint.x, aPoint.y);
					Color tempColor = colorSet.getColor();
					Integer tempThick = thickness.getValue();
					centerCenter.setElementAt(centerCenter.length()-1, new ImageInfo(gPath, tempColor, tempThick));
					centerCenter.repaint(); 
					return;
				}

				Shape tempShape;
				Integer tempFill = 0;
				Color tempColor = colorSet.getColor();
				if(drawButton.isSelected())
					tempFill = 0;
					else if(fillButton.isSelected())
						tempFill = 1;
				Integer tempThick = thickness.getValue();
				switch(state)
				{
				case 1:tempShape = new Ellipse2D.Double(x0>x?x:x0, y0>y?y:y0, x0>x?x0-x:x-x0, y0>y?y0-y:y-y0); break;
				case 4:tempShape = new Line2D.Double(x0, y0, x, y);
					tempFill = 2;
					break;
				case 6:tempShape = new Rectangle2D.Double(x0>x?x:x0, y0>y?y:y0, x0>x?x0-x:x-x0, y0>y?y0-y:y-y0); break;
				case 7:tempShape = new RoundRectangle2D.Double(x0>x?x:x0, y0>y?y:y0, x0>x?x0-x:x-x0, y0>y?y0-y:y-y0, 40, 40); break;
				default: tempShape = new Line2D.Double(x0, y0, x, y);
					tempFill = 2;
				}
				centerCenter.setElementAt(centerCenter.length()-1, new ImageInfo(tempShape, tempColor, tempFill, tempThick));
				centerCenter.repaint();
			}
			
			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		}

		centerCenter.addMouseListener(new MouseDraw());
		centerCenter.addMouseMotionListener(new MouseMove());
		
		frame.setSize(1280, 770);
		frame.setVisible(true);
	}
	
	

	public static void main(String args[])
	{
		//set the GUI same as my operating system
	      try {
	          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	      } catch (Exception exc) {
	          System.err.println("Error loading L&F: " + exc);
	      }

		ShapeTalk aGUI = new ShapeTalk();
		aGUI.go();
	}
}

⌨️ 快捷键说明

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