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

📄 whiteboardframe.java

📁 远程网络画板,用JAVA RMI实现,远程的多个用户可以同时在同一画板上绘画
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                if(board1.getConnectStatus(callbackid)){
                    
                board1.add(anitem);
                System.out.println("ok");}
            }catch(Exception ev) {
                System.out.println("Lookup: " + ev.getMessage());
            }
     createNewItem();
     }

	 

    //if input by text , do following steps
     if(currentChoice==14)
     {
      itemLista[index].x1=e.getX();
      itemLista[index].y1=e.getY();
        String input;
      input=JOptionPane.showInputDialog(
          "Please input the text you want!");
      itemLista[index].s1=input;
      itemLista[index].x2=f1;
      itemLista[index].y2=f2;
      itemLista[index].s2=style1;

      index++;    
      
      
      
      
      anitem.x1=e.getX();
      anitem.y1=e.getY();

      
      anitem.s1=input;
      anitem.x2=f1;
      anitem.y2=f2;
      anitem.s2=style1;
      try{
                if(board1.getConnectStatus(callbackid)){
                board1.add(anitem);
                System.out.println("ok");}
            }catch(Exception ev) {
                System.out.println("Lookup: " + ev.getMessage());
            }
  
      currentChoice=14;
      createNewItem();
      drawingArea.repaint();
      }
    }

   public void mouseReleased(MouseEvent e)
    {statusBar.setText("     Mouse Released @:[" + e.getX() +
                              ", " + e.getY() + "]");

    if(currentChoice==3||currentChoice==13)
    {
     itemLista[index].x1=e.getX();
     itemLista[index].y1=e.getY();
     anitem.x1=e.getX();
     anitem.y1=e.getY();
    }
     itemLista[index].x2=e.getX();
     itemLista[index].y2=e.getY();
     anitem.x2=e.getX();
     anitem.y2=e.getY();
     repaint();
     index++;
     try
     {
	//WhiteboardFrame.putInto(anitem,index);
        if(board1.getConnectStatus(callbackid)){
     	board1.add(anitem);
        System.out.println("ok");}
     }catch(Exception ev)
     {
     	System.out.println("Lookup: " + ev.getMessage());
     }	
     createNewItem();
    }

   public void mouseEntered(MouseEvent e)
   {
           statusBar.setText("     Mouse Entered @:[" + e.getX() +
                              ", " + e.getY() + "]");
           }

   public void mouseExited(MouseEvent e)
   {
           statusBar.setText("     Mouse Exited @:[" + e.getX() +
                              ", " + e.getY() + "]");
           }
  }




 class mouseB extends MouseMotionAdapter
 {
  public void mouseDragged(MouseEvent e)
  {statusBar.setText("     Mouse Dragged @:[" + e.getX() +
                              ", " + e.getY() + "]");

   if(currentChoice==3||currentChoice==13)
   {
    itemLista[index-1].x1=itemLista[index].x2=itemLista[index].x1=e.getX();
    itemLista[index-1].y1=itemLista[index].y2=itemLista[index].y1=e.getY();
    index++;
    anitem.x1=itemtempx1;
    anitem.x2=e.getX();
    anitem.y1=itemtempy1;
    anitem.y2=e.getY();
    
                try{
                if(board1.getConnectStatus(callbackid)){
                board1.add(anitem);
                
                System.out.println("ok");
                }
            }catch(Exception ev) {
                System.out.println("Lookup: " + ev.getMessage());
            }
    itemtempx1=anitem.x2;
    itemtempy1=anitem.y2;
    createNewItem();
   }
   else
    {
	   
     itemLista[index].x2=e.getX();
     itemLista[index].y2=e.getY();
	   
     anitem.x2=e.getX();
     anitem.y2=e.getY();
	 itemlist.add(anitem);
    }
   repaint();
   }

  public void mouseMoved(MouseEvent e)
   {statusBar.setText("     Mouse Moved @:[" + e.getX() +
                              ", " + e.getY() + "]");}
  }


// set listener to font
private class checkBoxHandler implements ItemListener
 {
  public void itemStateChanged(ItemEvent e)
  {
   if(e.getSource()==bold)
     if(e.getStateChange()==ItemEvent.SELECTED)
        f1=Font.BOLD;
      else
        f1=Font.PLAIN;
   if(e.getSource()==italic)
     if(e.getStateChange()==ItemEvent.SELECTED)
        f2=Font.ITALIC;
      else
        f2=Font.PLAIN;
          }
 }


// draw panel
 class DrawPanel extends JPanel
 {
   public DrawPanel()
  {
   setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
   setBackground(Color.white);
   addMouseListener(new mouseA());
   addMouseMotionListener(new mouseB());
  }

    public void paintComponent(Graphics g)
    {
      super.paintComponent(g);

      Graphics2D g2d=(Graphics2D)g;
      if(update==true)
      {
          for(int i=0;i<itemlist.size();i++)
         {
            drawings anitem=(drawings)itemlist.elementAt(i);
            anitem.draw(g2d);
          }
       }
     }
 }



 void createNewItem()
  { if(currentChoice==14)
          drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
          else
          drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

    switch (currentChoice)
    {
       case 3:
        itemLista[index]=new Pencil();
        anitem=new Pencil();
        break;
      case 4:
      itemLista[index]=new Line();
        anitem=new Line();
        break;
      case 5:
       itemLista[index]=new Rect();
        anitem=new Rect();
        break;
      case 6:
       itemLista[index]=new fillRect();
        anitem=new fillRect();
        break;
      case 7:
        itemLista[index]=new Oval();
        anitem=new Oval();
        break;
      case 8:
        itemLista[index]=new fillOval();
        anitem=new fillOval();
        break;
      case 9:
        itemLista[index]=new Circle();
        anitem=new Circle();
        break;
      case 10:
        itemLista[index]=new fillCircle();
        anitem=new fillCircle();
        break;
      case 11:
        itemLista[index]=new RoundRect();
        anitem=new RoundRect();
        break;
      case 12:
      itemLista[index]=new fillRoundRect();
        anitem=new fillRoundRect();
        break;
      case 13:
        itemLista[index]=new Rubber();
        anitem=new Rubber();
        break;
      case 14:
      itemLista[index]=new Word();
        anitem=new Word();
        break;
    }
    itemLista[index].type=currentChoice;
    itemLista[index].R=R;
    itemLista[index].G=G;
    itemLista[index].B=B;
    itemLista[index].stroke=stroke;
    anitem.type=currentChoice;
    anitem.R=R;
    anitem.G=G;
    anitem.B=B;
    anitem.stroke=stroke;
  }


//choose a color
public void chooseColor()
 {
    color=JColorChooser.showDialog(WhiteboardFrame.this,
                          "Choose a color",color);
    R=color.getRed();
    G=color.getGreen();
    B=color.getBlue();
    itemLista[index].R=R;
    itemLista[index].G=G;
    itemLista[index].B=B;
    anitem.R=R;
    anitem.G=G;
    anitem.B=B;
  }

// set stroke size
public void setStroke()
 {
  String input;
  input=JOptionPane.showInputDialog(
          "Please input a float stroke value! ( >0 )");
  stroke=Float.parseFloat(input);
  anitem.stroke=stroke;
  }


  //save a file
  public void saveFile()
  {


	  if (saved)
	  {
		  try
		  {
			  savedFile.delete();
			  FileOutputStream fos = new FileOutputStream(savedFile);

			  output = new ObjectOutputStream(fos);
			  drawings record;

			  output.writeInt(index);

			  for (int i = 0; i < itemlist.size(); i++)
			  {
				  drawings p = (drawings)itemlist.get(i);
				  output.writeObject(p);
				  output.flush();    
			  }
			  output.close();
			  fos.close();
		  }
		  catch (IOException ioe)
		  {
			  ioe.printStackTrace();
		  }

	  }
	  else
	  {
		  JFileChooser fileChooser = new JFileChooser();
		  fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		  int result = fileChooser.showSaveDialog(this);
		  if (result == JFileChooser.CANCEL_OPTION)
			  return;
		  File fileName = fileChooser.getSelectedFile();
		  fileName.canWrite();

		  if (fileName == null || fileName.getName().equals(""))
			  JOptionPane.showMessageDialog(fileChooser, "Invalid File Name",
					  "Invalid File Name", JOptionPane.ERROR_MESSAGE);
		  else
		  {
			  try
			  {
				  fileName.delete();
				  FileOutputStream fos = new FileOutputStream(fileName);

				  output = new ObjectOutputStream(fos);
				  drawings record;

				  output.writeInt(index);

				  for (int i = 0; i < itemlist.size(); i++)
				  {
					  drawings p = (drawings)itemlist.get(i);
					  output.writeObject(p);
					  output.flush();    
				  }
				  output.close();
				  fos.close();
				  this.savedFile = fileName;
				  this.saved = true;
			  }
			  catch (IOException ioe)
			  {
				  ioe.printStackTrace();
			  }
		  }
	  }
  }






//save a file
 public void saveAsFile()
 {
    JFileChooser fileChooser=new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int result =fileChooser.showSaveDialog(this);
    if(result==JFileChooser.CANCEL_OPTION)
             return ;
    File fileName=fileChooser.getSelectedFile();
    fileName.canWrite();

    if (fileName==null||fileName.getName().equals(""))
    JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
            "Invalid File Name", JOptionPane.ERROR_MESSAGE);
    else{
      try {
       fileName.delete();
       FileOutputStream fos=new FileOutputStream(fileName);

       output=new ObjectOutputStream(fos);
       drawings record;

       output.writeInt( index );

       for(int i=0;i< itemlist.size() ;i++)
       {
               drawings p= (drawings) itemlist.get(i);
        output.writeObject(p);
        output.flush();    
               }
      output.close();
      fos.close();
	  saved = true;
	  savedFile = fileName;
      }
       catch(IOException ioe)
       {
         ioe.printStackTrace();
       }
      }
   }

// open a file
 public void loadFile()
 {
	 
	 Vector temp = new Vector();
    JFileChooser fileChooser=new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int result =fileChooser.showOpenDialog(this);
    if(result==JFileChooser.CANCEL_OPTION)
          return ;
    File fileName=fileChooser.getSelectedFile();
    fileName.canRead();
    if (fileName==null||fileName.getName().equals(""))
       JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
            "Invalid File Name", JOptionPane.ERROR_MESSAGE);
    else {
      try {

          FileInputStream fis=new FileInputStream(fileName);

          input=new ObjectInputStream(fis);
          drawings inputRecord;

          int countNumber=0;
          countNumber=input.readInt();

          for(index=0;index< countNumber ;index++)
          {
            inputRecord=(drawings)input.readObject();
            temp.add(inputRecord) ;

          }
		  board1.load(temp);
          createNewItem();
          input.close();
		  saved = true;
		  savedFile = fileName;

          repaint();
          }
           catch(EOFException endofFileException){
            JOptionPane.showMessageDialog(this,"no more record in file",
                           "class not found",JOptionPane.ERROR_MESSAGE );
          }
          catch(ClassNotFoundException classNotFoundException){
            JOptionPane.showMessageDialog(this,"Unable to Create Object",
                           "end of file",JOptionPane.ERROR_MESSAGE );
          }
          catch (IOException ioException){
            JOptionPane.showMessageDialog(this,"error during read from file",
                           "read Error",JOptionPane.ERROR_MESSAGE );
            }
          }
	  
       }


//create a new file
 public void newFile()
 {
  index=0;
  currentChoice=3;
  color=Color.black;
  stroke=1.0f;
	 try{
  board1.newFile();
	 }
	 catch (Exception ev)
	 {
		 System.out.println("Lookup: " + ev.getMessage());
	 }

  createNewItem();
  repaint();// initailise the variables and repaint the graph
  saved = false;
 }



//main function

 }






⌨️ 快捷键说明

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