2011-04-25 3 views
1

핀볼 스타일 애플릿을 만들고 고맙게도 해결되기 전에 몇 가지 문제가 발생했습니다. 그러나, 나는 또 다른 걸림돌을 쳤다.회전 된 모양이 애플릿에 나타나지 않습니다.

핀볼 머신을위한 오리발을 그리려면, 나중에 애플릿에 스레드를 추가 할 때 위로 움직일 수 있도록 나중에 애니메이션을 적용 할 수 있도록 각진 둥근 사각형을 그려야했습니다. 이 작업은 모두 Table의 paintComponent 메소드에서 수행됩니다. 모든 회전이 적용되어 있는지 확인하기 위해 회전을 적용하지 않으면 잘 모양이 그려집니다. 그러나 현재 (내 코드 에서처럼 Affine Transform 객체를 통해 또는 Graphics2D.rotate 메서드를 통해) 회전을 적용하면 모양이 어떤 이유로 그려지지 않습니다. 왜 그런지 모르겠지만 나는 단지 뭔가를 간과했기를 바라고 있습니다. 나는 온라인으로 주변을 둘러 보았지만 올바른 키워드를 넣지 않거나 단순히 뭔가를 간과하고있는 것입니다.

내 핀볼 및 테이블 클래스의 코드는 아래에 있습니다.이 낯설음을 유발할 수 있다고 생각되는 것을 지적하십시오. 내가 정확한 좌표를 부여하지 않았다는 것을 주목하라. 내가 그려 볼 때 그 위치를 tweek 할 수 있고 비슷한 코드를 실행하여 그것을 디버깅 할 수있는 경우를 볼 수 있기 때문이다.

P. 볼의 생성자를 테이블에서 호출하는 것이 애플릿에서 수행하는 것보다 더 좋은 방법일까요? 내가 한 일이 정말로 비효율적인지 궁금해. 여기

import javax.swing.*; // useful for the drawing side, also going to be a JApplet 
import java.awt.*; 
import java.net.*; 

public class Pinball extends JApplet 
{ 
    // variables go here 
    Table table; 
    Player player; 
    Ball ball; 
    Miosoft miosoft; 
    Miofresh miofresh; 
    Mioboost mioboost; 
    Miocare miocare; 
    Miowipe miowipe; 

    // further initialisation of the GUI 
    public void init() 
    { 
     setSize(300, 300); 
     table = new Table(this); 
     player = new Player(); 
     ball = new Ball(180, 175); // set the ball's initial position in the arguments 
     miosoft = new Miosoft(); 
     miocare = new Miocare(); 
     miowipe = new Miowipe(); 
     miofresh = new Miofresh(); 
     mioboost = new Mioboost(); 
     setContentPane(table); // makes our graphical JPanel container the content pane for the Applet 
     // createGUI(); // this has been moved onto the table class 
    } 

    public void stop() 
    { 
    } 

// little getters to make things easier on the table 
    public int getBallX() 
    { 
     return ball.getXPosition(); 
    } 

    public int getBallY() 
    { 
     return ball.getYPosition(); 
    } 

    public int getLives() 
    { 
     return player.getLives(); 
    } 

    public int getScore() 
    { 
     return player.getScore(); 
    } 
} 

그리고

이없는 점 0, 일반적으로 무언가를 회전, 당신이 관심의 어떤 점을 중심으로 (회전되어있는 물건의 앵커 포인트)를 회전 테이블 클래스

import java.awt.*; // needed for old style graphics stuff 
import java.awt.geom.AffineTransform; 
import java.awt.geom.RoundRectangle2D; 
import java.awt.image.BufferedImage; 
import javax.imageio.ImageIO; 
import javax.swing.*; // gives us swing stuff 
import sun.rmi.transport.LiveRef; 
import java.io.IOException; 
import java.net.*; // useful for anything using URLs 
    /*-------------------------------------------------------------- 
Auto-generated Java code for class Table 

Generated by QSEE-SuperLite multi-CASE, QSEE-Technologies Ltd 
www.qsee-technologies.com 
Further developed to be the Content pane of this application by Craig Brett 
----------------------------------------------------------------*/ 

public class Table extends JPanel 
{ 
    // attributes go here 
    Pinball pb; 
    Color bgColour; 
    Color launcherColour; 
    Color ballColour; 
    Image logo; 
    Image freshBonus; 
    Image midCircle; 
    Image leftBumper; 
    Image rightBumper; 
    Image nappy1; 
    Image nappy2; 
    Image nappy3; 
    int ballX; 
    int ballY; 
    int playerLives; 
    int playerScore; 

    // constructor goes here 
    public Table(Pinball pb) 
    { 
     setPreferredSize(new Dimension(300, 300)); 
     this.pb = pb; 
     // this is not needed anymore, with the new loadImage class down the bottom 
     // Toolkit tk = Toolkit.getDefaultToolkit(); // needed to get images 
     // logo = tk.getImage(base + "images/bambinomio.jpg"); 
     logo = loadImage("bambinomio.jpg"); 
     freshBonus = loadImage("miofresh circle.jpg"); 
     midCircle = loadImage("middle circle.jpg"); 
     leftBumper = loadImage("left bumper.jpg"); 
     rightBumper = loadImage("right bumper.jpg"); 
     nappy1 = loadImage("nappy1.jpg"); 
     nappy2 = loadImage("nappy2.jpg"); 
     nappy3 = loadImage("nappy3.jpg"); 
     createGUI(); 
    } 

    // public methods go here 
    // all GUI creation stuff goes here 
    public void createGUI() 
    { 
     // setting the background colour 
     bgColour = new Color(190, 186, 221); // makes the sky blue colour for the background. 
     setBackground(bgColour); 
     launcherColour = new Color(130, 128, 193); 
     ballColour = new Color(220, 220, 220); // the color of the launch spring and ball 
     // setOpaque(false); 
    } 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     // to give us access to the 2D graphics features 
     Graphics2D g2d = (Graphics2D) g; 
     // creating the panels 
     g2d.setColor(Color.WHITE); 
     g2d.fillRect(200, 20, 100, 280); // the logo Panel 
     g2d.fillRect(0, 0, 300, 20); 
     g2d.setColor(Color.BLACK); 
     g2d.drawRoundRect(230, 125, 40, 120, 20, 20); // the lives panel 
     g2d.drawRoundRect(210, 255, 80, 40, 20, 20); 
     g2d.drawString("Score", 215, 270); 
     g2d.drawString(displayScore(), 215, 290); 
     // now drawing the graphics 
     g2d.drawImage(logo, 205, 25, 90, 90, null); 
     g2d.drawImage(freshBonus, 10, 40, 20, 20, this); 
     g2d.drawImage(leftBumper, 40, 200, 10, 20, this); 
     g2d.drawImage(rightBumper, 150, 200, 10, 20, this); 
     // now the three mid circles 
     g2d.drawImage(midCircle, 55, 120, 25, 25, this); 
     g2d.drawImage(midCircle, 95, 90, 25, 25, this); 
     g2d.drawImage(midCircle, 95, 150, 25, 25, this); 
     // now filling out the lives depending on how many the players have 
     playerLives = pb.getLives(); 
     if(playerLives >= 1) 
      g2d.drawImage(nappy1, 235, 135, 32, 30, this); 
     if(playerLives >= 2) 
      g2d.drawImage(nappy2, 235, 170, 32, 30, this); 
     if(playerLives >= 3) 
      g2d.drawImage(nappy3, 235, 205, 32, 30, this); 
     // now to handle the white lines 
     g2d.setColor(Color.WHITE); 
     g2d.drawLine(20, 250, 20, 60); // the left edge 
     g2d.drawArc(10, 40, 20, 20, 45, 225); // the top left corner 
     g2d.drawLine(28, 43, 160, 43); // the top of the table 
     g2d.drawArc(150, 41, 40, 30, 0, 120); // the top right corner 
     g2d.drawLine(20, 250, 35, 270); // the bottom left corner 
     // now for the launcher, we draw here 
     g2d.setColor(launcherColour); 
     g2d.fillRoundRect(170, 75, 30, 175, 20, 20); // the blue tube 
     g2d.setColor(ballColour); 
     g2d.fillRoundRect(175, 210, 20, 40, 20, 20); // the first bit of the launcher 
     g2d.fillRect(175, 210, 20, 20); // the top of the launcher's firer 
     g2d.fillRoundRect(175, 195, 20, 10, 20, 20); // the bit that hits the ball 
     // now drawing the ball wherever it is 
     ballX = pb.getBallX(); 
     ballY = pb.getBallY(); 
     g2d.fillOval(ballX, ballY, 10, 10); 
     // now the flippers 
     g2d.setPaint(Color.WHITE); 
     // more precise coordinates can be given when the transformation below works 
     // RoundRectangle2D leftFlipper = new RoundRectangle2D.Double(43.0, 245.0, 20.0, 50.0, 30.0, 30.0); // have to make this using shape parameters 
     // now using Affine Transformation to rotate the rectangles for the flippers 
     AffineTransform originalTransform = g2d.getTransform(); 
     AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(120)); 
     g2d.transform(transform); // apply the transform 
     g2d.fillRoundRect(33, 260, 20, 40, 10, 10); 
     g2d.setTransform(originalTransform); // resetting the angle 
     // g2d.drawString("The flipper should be drawn", 80, 130); // for debugging if the previous draw instructions have been carried out 
    } 

    // a little useful method for handling loading of images and stuff 
    public BufferedImage loadImage(String filename) 
    { 
     BufferedImage image = null; 
     try 
     { 
      URL url = new URL(pb.getCodeBase(), "images/" + filename); 
      image = ImageIO.read(url); 
     }catch(IOException e) 
     { 
      System.out.println("Error loading image - " + filename + "."); 
     } 
     return image; 
    } 

    private String displayScore() 
    { 
     playerScore = pb.getScore(); 
     String scoreString = Integer.toString(playerScore); 
     String returnString = ""; 
     if(scoreString.length() < 8) 
     { 
      for(int i = 0; i < (8 - scoreString.length()); i++) 
      { 
       returnString = returnString + "0"; 
      } 
     } 
     returnString = returnString + scoreString; 
     return returnString; 
    } 
} 
+3

권장 사항 1) 거의 200 줄의 호환되지 않는 코드로 구성된 두 개의 원본 파일 대신 [SSCCE] (http://pscode.org/sscce.html)를 게시하십시오. 2) 코드 블록을 시작하고 끝내는 위치를 명확하게하기 위해 일관된 & 논리적 들여 쓰기 및 브라케팅을 사용하십시오. 3) 'JFrame'에 표시되는 문제처럼 들리므로 대신 프레임을 사용하십시오. 대부분의 사람들은 앱을 실행할 수 있습니다. 애플릿보다 쉽습니다. 4) 이미지를 고려해보십시오. 모양에 관한 것 같습니다. –

+0

1. 내 코드와 호환되지 않는 것은 무엇입니까? –

+0

삭제할 수없는 이전 주석을 무시하십시오. 좋아, 나중에 사양에 대한 코드를 다시 작성하겠습니다. 하지만 3과 4는 취소 할 수 있습니다. 클라이언트가 웹 사이트에서 다운로드가 필요없는 항목을 원하기 때문에 3이 클라이언트 제한 조건과 같고 앱이 적합하지 않을 것으로 생각하기 때문입니다. 그리고 그것은 광고 게임이기 때문에 모든 이미지가 중요합니다. 그래도 모두 고마워. –

답변

3

입니다, 0. 당신이 0, 0 주위를 돌고있는 것처럼 보입니다. 그래서 이것은 화면에서 회전하게됩니다. 임의의 점을 중심으로 회전하려면 먼저 해당 점을 0 (음의 번역)으로 변환 한 다음 회전 한 다음 역으로 번역하십시오 (사전 변환).

+2

+1 [getRotateInstance()] (http://download.oracle.com/javase/6/docs/api/java/awt/geom/AffineTransform.html#getRotateInstance%28double,%20double,%20double%29)) 앵커도 포함됩니다. – trashgod

관련 문제