如何在Java中旋转方形图像?

时间:2016-01-30 04:40:16

标签: java 2d-games

我正在玩像几何游戏这样的游戏在java上,我得到了所有的机制,如波,球,不明飞行物,船......无论如何,我试图实现方形旋转的效果,当它像跳跃一样原来的游戏是尝试使用affineTransform这样做,但它不像原始游戏那样工作 我用这个代码旋转,但看起来很奇怪

<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>

    <link href="https://raw.githubusercontent.com/Microsoft/PowerBI-visuals/master/lib/visuals.css" rel="stylesheet">
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>

    <script type="text/javascript" src="http://microsoft.github.io/PowerBI-visuals/playground/externals.min.js"> </script>
    <script type="text/javascript" src="http://microsoft.github.io/PowerBI-visuals/playground/powerbi-visuals.js" ></script>
    <script type="text/javascript" src="http://microsoft.github.io/PowerBI-visuals/playground/PowerBIVisualsPlayground.js"></script>


    <style>
        .visual {
            'background-color' : 'white',
            'padding' : '10px',
            'margin' : '5px'
        }
    </style>
</head>
<body>
<h1> hello </h1>

<div class="visual"></div>
<script type="text/javascript">
    var createDataView = function () {
        var DataViewTransform = powerbi.data.DataViewTransform;

        var fieldExpr = powerbi.data.SQExprBuilder.fieldExpr({ column: { entity: "table1", name: "country" } });

        var categoryValues = ["Australia", "Canada", "France", "Germany", "United Kingdom", "United States"];
        var categoryIdentities = categoryValues.map(function (value) {
            var expr = powerbi.data.SQExprBuilder.equal(fieldExpr, powerbi.data.SQExprBuilder.text(value));
            return powerbi.data.createDataViewScopeIdentity(expr);
        });

        // Metadata, describes the data columns, and provides the visual with hints
        // so it can decide how to best represent the data
        var dataViewMetadata = {
            columns: [
                {
                    displayName: 'Country',
                    queryName: 'Country',
                    type: powerbi.ValueType.fromDescriptor({ text: true })
                },
                {
                    displayName: 'Sales Amount (2014)',
                    isMeasure: true,
                    format: "$0",
                    queryName:'sales1',
                    type: powerbi.ValueType.fromDescriptor({ numeric: true }),
                }
                ,
                {
                    displayName: 'Sales Amount (2013)',
                    isMeasure: true,
                    format: "$0",
                    queryName:'sales2',
                    type: powerbi.ValueType.fromDescriptor({ numeric: true })
                }
            ],
        };

        var columns = [
            {
                source: dataViewMetadata.columns[1],
                // Sales Amount for 2014
                values: [742731.43, 162066.43, 283085.78, 300263.49, 376074.57, 814724.34],
            },
            {
                source: dataViewMetadata.columns[2],
                // Sales Amount for 2013
                values: [742731.43, 162066.43, 283085.78, 300263.49, 376074.57, 814724.34].reverse()
            }
        ];

        var dataValues = DataViewTransform.createValueColumns(columns);

        var dataView = {
            metadata: dataViewMetadata,
            categorical: {
                categories: [{
                    source: dataViewMetadata.columns[0],
                    values: categoryValues,
                    identity: categoryIdentities,
                }],
                values: dataValues
            }
        };

        return dataView;
    };

    function createDefaultStyles(){
        var dataColors = new powerbi.visuals.DataColorPalette();

        return {
            titleText: {
                color: { value: 'rgba(51,51,51,1)' }
            },
            subTitleText: {
                color: { value: 'rgba(145,145,145,1)' }
            },
            colorPalette: {
                dataColors: dataColors,
            },
            labelText: {
                color: {
                    value: 'rgba(51,51,51,1)',
                },
                fontSize: '11px'
            },
            isHighContrast: false,
        };
    }

    function createVisual() {
        var pluginService = powerbi.visuals.visualPluginFactory.create();
        var defaultVisualHostServices = powerbi.visuals.defaultVisualHostServices;
        var width = 600;
        var height = 400;

        var element = $('.visual');
        element.height(height).width(width);


        // Get a plugin
        var visual = pluginService.getPlugin('columnChart').create();

        powerbi.visuals.DefaultVisualHostServices.initialize(); 

        visual.init({
            // empty DOM element the visual should attach to.
            element: element,
            // host services
            host: defaultVisualHostServices,
            style: createDefaultStyles(),
            viewport: {
                height:height,
                width: width
            },
            settings: { slicingEnabled: true },
            interactivity: { isInteractiveLegend: false, selection: false },
            animation: { transitionImmediate: true }
        });

        // Call update to draw the visual with some data
        visual.update({
            dataViews: [createDataView()] ,
            viewport: {
                height: height,
                width: width
            },
            duration: 0
        });
    }
    createVisual();

</script>




</body>
</html>

}

here is a capture of my game looking right now

here is a link of geometry dash were you can see how the square rotate in middle air(what i am trying to do)

有什么想法吗?请帮帮我:c

1 个答案:

答案 0 :(得分:0)

hmmm,我实际上会使用AffineTransform,但以另一种方式... 如果您有两个功能

public void tick()
{

}
public void render(Graphics g)
{

}

我会做类似的事情:

AffineTransform at;
rotation = 0;
public Player()
{
  at = AffineTransform.getTranslateInstance(x, y);
}
public void tick()
{
  at = AffineTransform.getTranslateInstance(x, y);
  if(isInAir)
  {
    rotation++;
    at.rotate(Math.toRadians(rotation), width / 2, height / 2);
  }
  else
  {
    rotation = 0;
  }
}
public void render(Graphics g)
{
  Graphics2D g2d = (Graphics2D) g;
  g2d.drawImage(sprite, at, null);
}

我知道这并不实际,总是创建另一个AffineTransform,但对我有用。在render方法中不使用任何AffineTransform的另一种方法是:

Graphics2D g2d = (Graphics2D) g;
g2d.rotate(Math.toRadians(rotation));
g.drawImage(sprite, x, y, null);
g2d.rotate(Math.toRadians(-rotation));

我不太确定您是否需要在这里使用Math.toRadians,但不是就删除它即可。 希望我能帮忙!