簡體   English   中英

旋轉形狀未出現在Applet上

[英]Rotated shapes not appearing on an Applet

我正在制作彈球式小程序,但在解決之前遇到了一些問題。 但是,我遇到了另一個絆腳石。

為了為彈球機繪制鰭狀肢,我打算繪制一個成角度的圓角矩形,這將使我以后可以對其進行動畫處理,以便在我向小程序添加線程時可以看到它向上移動。 這一切都是在表格的paintComponent方法中完成的。 當我不進行任何旋轉以檢查是否一切正常時,它會很好地繪制形狀。 但是,當我應用旋轉(無論是通過現在的代碼中的Affine Transform對象還是通過Graphics2D.rotate方法)時,由於某種原因,形狀不會繪制。 我不確定為什么,但我希望自己只是忽略了一些東西。 我在網上四處張望,但要么輸入的關鍵詞不正確,要么就是我忽略了某些東西。

我的Pinball和Table類的代碼如下,請隨時指出您認為可能導致這種奇怪現象的任何內容。 請注意,如果您運行任何類似的代碼來嘗試和調試它,那么我還沒有完全給出正確的坐標,因為它在繪制時它的位置仍然可以看到。

作為PS會比在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;
    }
}

通常,要旋轉某物,請圍繞某個感興趣的點(正在旋轉的東西的錨點)而不是點0,0對其進行旋轉。 看來您是圍繞0、0旋轉,所以很可能會旋轉到屏幕之外。 要圍繞任意點旋轉,請先將該點平移為0(負平移),然后旋轉,然后再平移(正向平移)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM