2013-11-03 7 views
0

2 개의 jcomponents, animation 및 gardenpanel이 있습니다. jlayeredpane에 포함 시켰지만 애니메이션 클래스는 항상 움직이는 캐릭터입니다. 주된 방법으로 다시 그리기를 호출 할 때마다 매우 고르지 않고 느립니다. 그래서 Runnable을 구현하고 animation.repaint() 만 호출하는 새 스레드를 시작하는 Update라는 새 클래스를 만들었습니다. 그러나 그것은 아직도 칙칙하고 고르지. 어떤 도움?스레드로 다시 그네 넣기

public class Driver { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run(){ 

       JFrame frame = new JFrame(); 
        JLayeredPane pane = new JLayeredPane(); 
        Animation animation = new Animation(); 

       GardenPanel gPanel = new GardenPanel(6,4,600,800); 



       frame.setSize(800,600); 
       frame.add(pane); 
       // establish transparent background 
       //Color transparent = new Color(0,0,0,0); 

       // Initilize jcomponents 
       //Garden garden = new Garden(6,4); 

       //making jcomponents visible 
       animation.setSize(frame.getSize()); 
       gPanel.setSize(frame.getSize()); 


       animation.setOpaque(true); 
       pane.add(animation); 
       pane.add(gPanel); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true); 


      } 

     }); 

------- 애니메이션 클래스 -------

package view; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 



    public class Animation extends JComponent implements MouseMotionListener, MouseListener{ 
    final int frameCount = 10; 
    int picNum = 0; 
    BufferedImage[][] pics; 
    int xloc = 0; 
    int yloc = 0; 
    final int xIncr = 8; 
    final int yIncr = 2; 
    final static int frameWidth = 900; 
    final static int frameHeight = 600; 
    final static int imgWidth = 165; 
    final static int imgHeight = 165; 
    //basic info about the orc 
    Color c = new Color(0, 0, 0, 0); 

    BufferedImage seedImage; 
    int mouseX; 
    int mouseY; 
    int seedX = xloc+imgWidth/2; 
    int seedY = yloc+imgHeight/2; 
    int shootX; 
    int shootY; 
    boolean seedShooting = false; 
    boolean mouseholding; 
    boolean moving = false; 
    int power; 
    public enum stage { 
     MOVE, POWER 
    } 
    stage s; 
    int seedPower; 
    long pSeedTime; 
    long cSeedTime; 
    double seedPowerRatio = 0.75; 

    //Override this JPanel's paint method to cycle through picture array and draw images 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     picNum = (picNum + 1) % frameCount; 
     if(mouseholding && (s == s.POWER)){ 
      System.out.println(power++); 
     } 
     if(seedShooting){ 
      cSeedTime = System.currentTimeMillis(); 

      seedX = (int) (seedPowerRatio * 1*seedPower*1.5*(cSeedTime - pSeedTime)/50 + shootX); // calculate x-coor of seed 
      seedY = (int) (seedPowerRatio * (-1)*seedPower*1.5*((int)(cSeedTime - pSeedTime))/50 + 0.5*0.02*(cSeedTime - pSeedTime)*(cSeedTime - pSeedTime)/(50^2) +shootY); 
      //calculate y-coor of seed 
     } 
     if(seedY>shootY+imgWidth/4){ 
      seedShooting = false; 
      seedX = xloc+imgWidth/2; 
      seedY = yloc+imgHeight/2; 
     } 
     g.drawImage(pics[1][picNum], xloc, yloc, c, this); 
     g.drawImage(seedImage, seedX, seedY, imgWidth/8, imgHeight/8, this); 
    } 



    //Constructor: get image, segment and store in array 
    public Animation(){ 
     seedImage = createImage(); ; 
     BufferedImage[] img = createAnimation(); 
     pics = new BufferedImage[img.length][10]; 
     for(int j = 0; j < img.length; j++){ 
      for(int i = 0; i < frameCount; i++) 
       pics[j][i] = img[j].getSubimage(imgWidth*i, 0, imgWidth, imgHeight); 
     } 
     addMouseMotionListener(this); 
     addMouseListener(this); 

     Thread background = new Thread(new Background()); 
     //background.setDaemon(true); 
     background.start(); 

    } 

    //Read image from file and return 
    private BufferedImage[] createAnimation(){ 
     BufferedImage[] bufferedImage = new BufferedImage[4]; 
     try { 
      bufferedImage[0] = ImageIO.read(new File("images/orc_forward_southeast.png")); 
      bufferedImage[1] = ImageIO.read(new File("images/orc_forward_southwest.png")); 
      bufferedImage[2] = ImageIO.read(new File("images/orc_forward_northeast.png")); 
      bufferedImage[3] = ImageIO.read(new File("images/orc_forward_northwest.png")); 
      return bufferedImage; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    private BufferedImage createImage(){ 
     BufferedImage bufferedImage; 
     try { 
      bufferedImage = ImageIO.read(new File("images/seed.png")); 
      return bufferedImage; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 

     // TODO: Change this method so you can load other orc animation bitmaps 
    } 

    // implement the method in mouseListener and mouseMotionListener interface 
    @Override 
    public void mouseDragged(MouseEvent event) { 
     mouseX = event.getX(); 
     mouseY = event.getY(); 
     if((xloc<mouseX) && (xloc+imgWidth >mouseX) && (yloc<mouseY) && (yloc+imgHeight>mouseY) && (s==stage.MOVE)){ 
      //System.out.println("Imagecoor:("+xloc+", "+yloc+")"+" mousecoor:("+mouseX+","+mouseY+")"); 
      // you can print the coordinate if you want  
      //xloc = mouseX-imgWidth/2; 
      yloc = mouseY-imgHeight/2; 
      seedX = xloc + imgWidth/2; 
      seedY = yloc + imgHeight/2; 
     } 
    } // draging the image 
    @Override 
    public void mouseMoved(MouseEvent arg0) { 
    } 
    @Override 
    public void mouseClicked(MouseEvent e) { 
    } 
    @Override 
    public void mouseEntered(MouseEvent e) {  
    } 
    @Override 
    public void mouseExited(MouseEvent e) { 
    } 

    // for increasing the power. 
    @Override 
    public void mousePressed(MouseEvent event) { 
     mouseholding = true; 
     mouseX = event.getX(); 
     mouseY = event.getY(); 
     if((xloc+imgWidth/2+1> mouseX) && (xloc-imgWidth/2-1< mouseX)&&(yloc+imgHeight/2+1 > mouseY)&&(yloc-imgHeight/2-1 < mouseY) ){  
      s = stage.POWER; 
     } 
    } 
    @Override 
    public void mouseReleased(MouseEvent event) { 
     mouseholding = false; 
     if(s == stage.POWER){ 
      pSeedTime = System.currentTimeMillis(); 
      seedPower = power; 
      shootX = seedX; 
      shootY = seedY; 
      seedShooting = true; 
     } 
     s = stage.MOVE; 
     power = 0; 
    } 

    protected class Background implements Runnable{ 

     @Override 
     public void run() { 
      while(true){ 

       repaint(); 


      } 
     } 


    } 
+1

코드를 표시하고 있지만 애니메이션의 품질에 영향을 미치는 코드는 아닙니다. 이를위한 관련 코드를 보여줄 것을 고려하십시오. –

+1

또한 Update 클래스가 참조를 전달하지 않고 작동하는 경우에는 정적이 아니어야하는 정적 항목이 있음을 알 수 있습니다. 또한 모든 스윙 호출은 이벤트 스레드에서 수행되어야합니다. –

+0

얼마나 자주 업데이트 하시겠습니까? 규칙적인 간격입니까? 간격 사이를 얼마나 멀리 움직입니까? 그들은 규칙적인 움직임인가? – MadProgrammer

답변

1
Thread background = new Thread(new Background()); 
    //background.setDaemon(true); 
    background.start(); 

하고 Background() 실행 가능한 코드 :

protected class Background implements Runnable{ 

     @Override 
     public void run() { 
      while(true){ <-- it is true, true and true, who is changing it? 

       repaint(); 


      } 
     } 

영원히 repaint() 요청을 호출 , 종료 없음, 아니 sleep 통제. 프로세서에서 몇 사이클이 발생합니까? 귀하의 프로세서는 언제 다른 계산 유닛과 이야기하기 위해 어느 정도 휴식을 취할 것입니까?

+0

나는 당신의 예제에서했던 것처럼 내 코드에 thread.sleep을 삽입했습니다. 내 문제는 캐릭터가 잘 움직이고 있지만 계속해서 그 위에 그려지기 때문에 계속해서 여러 번 그 이미지가 번식하고 있다는 것입니다. 나는 내가 재 페인트를 부를 때 그것은 성격을 다시 채색 할 것이고, 오래된 것을 다시 페인트하지 않을 것이라고 생각했다. 어떤 도움? – user2510809

+0

@ user2510809 네, 우리가 도와주고 싶습니다. 그러나 이전 게시물에서 주어진 답변에 응답하지 않으면 아무런 답변도받지 못해서 답장을 보내지 않아 많은 사람들이 귀하의 질문에 답변하지 않기 시작합니다. 제발, 제 잘못 이해하지 마세요. 그러나 이것은이 사이트가 작동하는 방식입니다 -'서로 도움을주고 응답합니다. ' – Sage