2011-04-25 51 views
1

我正在做一個彈球風格的小程序,我跑進前已經謝天謝地得到糾正一些問題。但是,我又碰到了另一個絆腳石。旋轉的形狀,而不會出現在一個Applet

要繪製的鰭狀肢的彈球機,我打算畫一個角度的圓角矩形這將讓我有可能製作動畫以後,這樣你可以看到它動起來當我添加線程的小程序。這全部在Table的paintComponent方法中完成。當我沒有應用任何旋轉來檢查它是否全部正常工作時,它就會很好地繪製出形狀。但是,當我應用旋轉(無論是通過仿射變換對象(如現在的代碼還是Graphics2D.rotate方法),由於某種原因,該形狀不會繪製。我不知道爲什麼,但我希望我只是忽略了一些東西。我在網上查了一下,但是我沒有把正確的關鍵詞放進去,或者是我忽略了一些東西。

的代碼爲我彈球和表類下面是,請隨時指出任何你認爲可能會導致此陌生感。請注意,我還沒有完全給出正確的座標,因爲我可以在畫出它的位置時調整它的位置,並且我可以看到它,以防萬一您運行任何類似的代碼來嘗試和調試它。

作爲P.S.在桌面上調用球的構造函數比在applet上執行它更好嗎?只是想知道我做了什麼,真的是效率低下。

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(); 
    } 
} 

這裏是表類

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)發佈一個[SSCCE](http://pscode.org/sscce.html),而不是包含幾乎200的不可編譯的代碼線的兩個源文件。 2)使用一致的&邏輯縮進和包圍代碼來清楚地說明代碼塊在哪裏開始和結束。 3)由於這聽起來像是一個'JFrame'中顯示的問題,所以請使用該框架。多數人可以運行應用程序。比applets更容易。 4)分解圖像,因爲這似乎是關於形狀。 – 2011-04-25 14:45:23

+0

1.什麼是我的代碼不可編譯的? – 2011-04-25 17:37:03

+0

無視以前的不可評論的評論。好的,稍後我會重寫該規範的代碼。但是3和4是可撤銷的,因爲3和客戶端約束一樣好,因爲客戶想要在他們的網站上不需要下載的東西,我不相信一個應用會適合這個。這是一個廣告遊戲,所以圖像對所有這些都很重要。儘管如此,謝謝你們。 – 2011-04-25 17:58:02

回答

3

通常旋轉的東西,你旋轉它周圍的一些感興趣的時刻(即旋轉的東西錨點),而不是0點, 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 2011-04-25 17:28:46