2012-04-05 8 views
0

カットコード:これはコードです、私はtank2を描画する行を取り除くことはできません。このサーバーにデータを送信しているクライアントを切り替えます。これは私が作っていたゲームのサーバー側である: - ペイント方法でこれを行うコードでコメントとして特定の行を省略すると、コードブロック、入力が行われず、フリーズします

package dataEx; 

import java.applet.*; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 

import java.net.MalformedURLException; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.net.URL; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 

import java.awt.image.FilteredImageSource; 
import java.awt.image.ImageFilter; 
import java.awt.image.ImageProducer; 
import java.awt.image.RGBImageFilter; 

import javax.imageio.ImageIO; 


public class test extends Applet implements Runnable, ActionListener,MouseListener, MouseMotionListener,Serializable 
{ 

    dataToExchange xc; 

    public static Image makeColorTransparent(Image im, final Color color) 
      { 
      ImageFilter filter = new RGBImageFilter() 
      { 

        public int markerRGB = color.getRGB() | 0xFF000000;    //color to make transparent 

        public final int filterRGB(int x, int y, int rgb) 
        { 
        if ((rgb | 0xFF000000) == markerRGB) 
        { 
         // Mark the alpha bits as zero - transparent 
         return 0x00FFFFFF & rgb; 
        } 

        else 
        { 
         // nothing to do 
         return rgb; 
        } 
       } 
      }; 

     ImageProducer ip = new FilteredImageSource(im.getSource(), filter); 
     return Toolkit.getDefaultToolkit().createImage(ip); 

    } 

    private Image dbImage; 
    private Graphics dbg; 

    ServerSocket server; 
    Socket connection; 

    dataToExchange data_recv=null; 
    Thread t; 


    int frameNumber=0; 
    int speed=5,block_front=0,block_end=0; 
    int fire=0,tankSpeed=0,pressing=0; 
    int topnose_x1,topnose_x2,topnose_y1,topnose_y2; 
    int bck_x=-500; 
    int init_x=500,tank_y=500; 
    int hand_x,hand_y,mouth_x,mouth_y; 
    double theta =0.785,angle; 
    int moveWithBck=0,glass_x=500; 
    Boolean shoot=false; 

    Image cannon[]=new Image[6]; 
    Image glass,glass_t;  
    Image background; 

    //String[] tankSeq=new String[6]; 

    ObjectInputStream input; 
    ObjectOutputStream output; 



    public void init() 
    { 
     setCursor (Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 
     try 
     { 
      connect(); 
     } catch (ClassNotFoundException e) { 

      e.printStackTrace(); 
     } 

      setSize(1350,640); 
      setBackground(Color.white); 



      cannon[0]=getImage (getCodeBase(), "1.png"); 
      cannon[1]=getImage (getCodeBase(), "2.png"); 
      cannon[2]=getImage (getCodeBase(), "3.png"); 
      cannon[3]=getImage (getCodeBase(), "4.png"); 
      cannon[4]=getImage (getCodeBase(), "5.png"); 
      cannon[5]=getImage (getCodeBase(), "6.png"); 




      glass = getImage (getCodeBase(), "glass.png"); 
      glass_t = makeColorTransparent(glass, Color.red); 


      background = getImage (getCodeBase(), "wall2.jpg"); 



    } 

    private void connect() throws ClassNotFoundException 
    { 
     int portNumber=1232; 
     server = null; 
     try 
     { 
      server=new ServerSocket(portNumber,1); 

     while(true) 
     { 
      System.out.println("wainting for clients"); 
      connection=server.accept(); 


      input=new ObjectInputStream(connection.getInputStream()); 
      output= new ObjectOutputStream(connection.getOutputStream()); 



      String message=(String)input.readObject(); 
      System.out.println("message "+message);  
      break;   
     } 

     } catch (IOException e) { 

      e.printStackTrace(); 
     } 

    } 

    public void start() 
    {  
     Thread t = null; 
     t = new Thread(this); 
     t.start(); 
     try { 
      SendTheChange(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 


    public void update (Graphics g)            //overriding the update for double buffering 
    {  
     // initialize buffer 
     dbImage = createImage (this.getSize().width, this.getSize().height); 
     dbg = dbImage.getGraphics(); 


     // clear screen in background 
     dbg.setColor (getBackground()); 
     dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); 


     // draw elements in background 
     dbg.setColor (getForeground()); 
     paint (dbg); 

     // draw image on the screen 
     g.drawImage (dbImage, 0, 0, this);  
    } 




    void drawTank(Graphics g) throws MalformedURLException, IOException 
    { 
     int tank_x=tankSpeed+init_x; 


     if(frameNumber<5) 
     { 
      if(pressing==1) 
       frameNumber=frameNumber+1;   
     } 
     else 
      frameNumber=0; 


     Image tank= ImageIO.read(new URL(getCodeBase(), "1.png")); 
     Image tank2 = makeColorTransparent(tank, new Color(0).white);    

     **g.drawImage (tank2, tank_x,tank_y, this)**; //I just cant get rid of this line   


     g.drawImage (cannon[frameNumber], tank_x,tank_y, this); 

     topnose_x1=tank_x+100; 
     topnose_x2=tank_x+150; 
     topnose_y1= tank_y+65; 
     topnose_y2=tank_y+50; 


     Graphics2D g2 = (Graphics2D) g; 
     g2.setStroke(new BasicStroke(7)); 


     double base,height; 


     base=hand_x-topnose_x1; 
     height=topnose_y1-hand_y; 



     if(base<2) 
     { 
      base=2; 
     } 

     if(height<4)  
     { 
      height=4; 
     } 

     double tantheta=height/base; 

     theta=-Math.atan(tantheta); 

     topnose_x2=(int) (53*Math.cos(theta)+topnose_x1); 
     topnose_y2=(int) (53*Math.sin(theta)+topnose_y1); 



     g2.drawLine(topnose_x1,topnose_y1 , topnose_x2, topnose_y2);   
     g2.drawLine(topnose_x1+5,topnose_y1+4 , topnose_x2+2, topnose_y2+1);   
     g2.drawLine(topnose_x1+10, topnose_y1+5, topnose_x2+4, topnose_y2+2); 
     pressing=0;  
    } 

    void drawBackground(Graphics g) 
    { 

     g.drawImage (background,bck_x,0, this); 
    } 




    public void paint(Graphics g)  
    {   
     drawBackground(g);  

     try { 

      drawTank(g); 



      } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } 

    public void run() 
    {  
     while (true) 
     { 
      try { 
       recieveChanges(); 
      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }{ 
      // repaint the applet 
      repaint(); 

      try 
      { 
       Thread.sleep (20); 
      } 
      catch (InterruptedException ex) 
      { 

      } 

      } 
     } 
    } 


    private void recieveChanges() throws IOException, ClassNotFoundException 
    {   
     data_recv=(dataToExchange)input.readObject(); 
     //System.out.println("ds"+data_recv.xOffsetTank); 
    } 
    int checkBckRange() 
    { 
     int p_bck_x = 0; 

     if(bck_x<0) 
     { 
      p_bck_x=bck_x*-1; 
      block_front=0; 
     } 
     else 
     { 
      block_front=1; 
     } 
     //if(bck_x+bck_length>) 

     return p_bck_x; 

    } 
    private void SendTheChange() throws IOException 
    { 

     dataToExchange data=new dataToExchange() ; 


     int p_bck_x=checkBckRange(); 

     int offset=p_bck_x+init_x; 


     data.frameIndex=frameNumber; 
     data.xOffsetTank=offset; 

     output.writeObject(data);  
     output.flush();  
    } 

    public boolean mouseMove (Event e, int x, int y) 
    { 
     hand_x=x; 
     hand_y=y;  


     return false; 
    } 

    public boolean mouseDrag (Event e, int x, int y) 
    { 


     hand_x=x; 
     hand_y=y;  

     return false; 

    } 
    public boolean mouseDown (Event e, int x, int y) 
    { 
     if(shoot==false) 
     { 
      shoot=true; 
      mouth_x=topnose_x1; 
      mouth_y=topnose_y1-2; 
      moveWithBck=0; 
      angle=-theta; 
      //System.out.println(x+"  "+y+"\n"); 
     } 
     return true; 
    } 


    public boolean keyDown(Event e, int key) 
    { 
     // user presses left cursor key 


      if (key == 'a'&& block_front==0) 
      {  

       bck_x=bck_x+speed; 
       moveWithBck=moveWithBck+speed; 
       pressing=1; 
      } 
      // user presses right cursor key 
      else if (key == 'd') 
      {  
       bck_x=bck_x-speed; 
       moveWithBck=moveWithBck-speed; 
       pressing=1; 
      } 
      else if (key == 'w') 
      {  
       tank_y=tank_y-200; 
      } 

      else if (key == 's') 
      {  
       tank_y=tank_y-200; 
      } 

     try { 
      SendTheChange(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 


     return true; 
    } 

    @Override 
    public void mouseDragged(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void mouseMoved(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void mouseClicked(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

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

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

    } 
    @Override 
    public void mousePressed(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void mouseReleased(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 



} 
+1

1)すぐに役立つように、[SSCCE](http://sscce.org/)を投稿してください。 2)アプレットは先進的なトピックですから、最初にフレーム内でこれを動作させることをお勧めします。 3)このミレニアムでは、AWTコンポーネントではなくSwing(例:JAppletまたはJFrame)を使用します。 4)ソケット接続によってGUIがフリーズしている可能性があります。問題が別のものであることを確認してください。 5)一般的な[Java命名規則(Java命名規則)](http://java.sun.com/docs/books/books/books/jls/second_edition/html/names.doc.html#73307)(具体的には名前に使用されるケース)を学びましょう、メソッドと属性の名前&一貫して使用します。 –

答えて

0

は私には非常に悪い考えのようになります。

tank= ImageIO.read(new URL(getCodeBase(), "1.png")); 

は、あなたが何をすべきことときに一度アプレットが起動します。画像を読んでいるだけですか?

+0

ok、私はinit()に1.pngをロードする行を移動しましたが、現在はペイントでイメージを描画する必要があります。それ以外の場合はハングします。 –

+0

@riyadas:どのような診断がありますか?あなたは例外を見ていますか?あなたがデバッガでそれを実行する場合は、あなたのペイント方法をステップ実行できますか?どんな 'drawXyz'メソッドも' repaint() 'を呼び出していますか? –

+0

例外は発生しません。コード全体を表示すると、コード全体をどこに投稿できますか? –

関連する問題