2011-04-25 53 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