当用户点击按钮时,我有一个正在旋转的图像.但它没有用.
我想看到图像逐渐旋转到90度直到它停止但它没有.单击按钮时,图像必须逐渐旋转90度.
我创建了一个SSCCE来演示这个问题.请使用CrossingPanelSSCE您选择的任何图像替换班级中的图像.只需将图像放入您的images文件夹并命名即可images/railCrossing.JPG.
RotateButtonSSCE
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
public class RotateButtonSSCE extends JPanel implements ActionListener{
private JButton rotate = new JButton("Rotate");
private VisualizationPanelSSCE vis = new VisualizationPanelSSCE();
public RotateButtonSSCE() {
this.setBorder(BorderFactory.createTitledBorder("Rotate Button "));
this.rotate.addActionListener(this);
this.add(rotate);
}
public void actionPerformed(ActionEvent ev) {
vis.rotatetheCrossing();
}
}
Run Code Online (Sandbox Code Playgroud)
CrossingPanelSSCE
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import …Run Code Online (Sandbox Code Playgroud) 我上课了Airplane.在这个类里面我有变量img这是一个BufferedImage类型.更有甚至,我有WorldMap一个覆盖功能的类paintComponent(Graphics g):
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(mapa, 0, 0, getWidth(), getHeight(), null);
drawAirplanes(g2d);
}
Run Code Online (Sandbox Code Playgroud)
功能drawAirplanes()如下:
private void drawAirplane(Graphics2D g){
for(Samolot i: s){
i.rotateAirplane();
g.drawImage(i.getImg(),i.getX(),i.getY(),(int)i.getDim().getWidth(),(int)i.getDim().getHeight(), null);
}
}
Run Code Online (Sandbox Code Playgroud)
它只需要1)旋转飞机(飞机物体内的BufferedImage)2)画他.
我的Airplane.rotateAirplane()函数如下所示:
public void rotateSamolot() {
AffineTransform tx = new AffineTransform();
tx.translate(10,10); //10, 10 is height and width of img divide by 2
tx.rotate(Math.PI / 2);
tx.translate(-10,-10);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage …Run Code Online (Sandbox Code Playgroud) 我制作了一个GUI,它使用滑块来上下缩放对象.(在本例中为矩形).我想知道是否还有一种方法可以使用滑块来指定旋转度.因此,将有2个滑块用于控制比例,另一个用于控制旋转.如果有人能帮助我做到这一点,这将是我迄今为止只有比例滑块.
import javax.swing.*;
public class Parker
{
public static void main(String[] args)
{
TheWindow w = new TheWindow();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //X wont close the window with out this line
w.setSize(1280,720);
w.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class TheWindow extends JFrame
{
private JSlider slider; //declare slider
private drawRect myPanel; //declare/ create panel
public TheWindow()
{
super("Slider Example"); //make title
myPanel = new drawRect();
myPanel.setBackground(Color.cyan); //change background color
slider = new JSlider(SwingConstants.VERTICAL, 0, 315, 10);// restrains the slider …Run Code Online (Sandbox Code Playgroud) 您好我很好奇如何解决这个问题:我在Java中使用fillArc,drawArc方法创建了一个pacman,我现在屏幕上有一个pacman人,无论方向如何,总是向右看..我的问题是..有没有办法逐个改变对象或在Java中水平翻转它?
我试图使用AffineTransform,但我没有得到我想要的文档...我应该如何使用switch语句实现这一点?我试着做以下事情但是我被困在这个部分,因为我不知道如何继续.
DrawPacMan pacman = new DrawPacMan();
DrawPacMan ghost1 = new DrawPacMan();
DrawPacMan ghost2 = new DrawPacMan();
AffineTransform pac = new AffineTransform();
public void setPacManView(int waarde) {
// set the view of pacman
switch (waarde) {
case 0 :
// here one view of pacman
break;
case 1 :
// here one view of pacman
break;
case 2 :
// here one view of pacman
break;
case 3 :
// here one view of pacman
break;
}
}
public void …Run Code Online (Sandbox Code Playgroud)