gpt4 book ai didi

java - 关于为什么按下按键时三角形不旋转有什么建议吗?

转载 作者:行者123 更新时间:2023-12-01 16:37:18 25 4
gpt4 key购买 nike

我正在寻找为什么按下按键时三角形不旋转的答案。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.GeneralPath;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Beta extends JPanel implements KeyListener
{
private static final long serialVersionUID = 1L;
private int LEFT = 0, RIGHT = 1;

Beta()
{
addKeyListener(this);
}

public void paintComponent( Graphics g )
{
repaint(); // call superclass's paintComponent

三角形的点

        int[] xPoints = {-50, 0, 50};
int[] yPoints = {0, -50, 0};

Graphics2D g2d = ( Graphics2D ) g;
GeneralPath star = new GeneralPath(); // create GeneralPath object

// set the initial coordinate of the General Path
star.moveTo(-50, 0);

// create the star--this does not draw the star
for ( int count = 1; count < xPoints.length; count++ )
star.lineTo( xPoints[ count ], yPoints[ count ] );

star.closePath(); // close the shape

g2d.translate( getWidth()/2, getHeight()/2 ); // translate the origin to (150, 150)

// rotate around origin and draw stars in random colors

旋转

        g2d.rotate( Math.PI / 40.0 );

g2d.setColor(Color.RED);

g2d.fill( star );
}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

按下按键即可绘制

public void keyPressed(KeyEvent ke)
{
switch(ke.getKeyCode())
{
default: repaint();
}
}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

public static void main( String[] args )
{
JFrame frame = new JFrame( "Space Battle Beta" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

//add Panel to frame
Beta shapes2JPanel = new Beta();
frame.add( shapes2JPanel ); // add shapes2JPanel to frame
frame.setBackground(Color.BLACK);
frame.setSize( 315, 330 ); // set frame size
frame.setVisible( true ); // display frame
}
} // end class Shapes2JPanel

无法使三角形旋转。是 repaint() 不起作用还是有其他原因?

最佳答案

repaint()不会调用父类(super class)的paint方法,它会调用此类的方法。您应该执行 super.paintComponent(g) 来调用父类(super class)的 PaintComponent() 而不是 repaint()

关于java - 关于为什么按下按键时三角形不旋转有什么建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7780994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com