简体   繁体   English

在applet上绘制一个旋转矩形

[英]Draw a rotating rectangle on applet

I have to make a rotating rectangle on my applet, how is it done? 我必须在我的applet上制作一个旋转矩形,它是如何完成的? The rectangle should rotate around one of its conner on the plane. 矩形应围绕平面上的其中一个旋转。 This is what I have so far: 这是我到目前为止:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JApplet;

public class MainApplet extends JApplet {
    Font bigFont;
     Color redColor; 
     Color weirdColor; 
     Color bgColor;

    @Override
     public void init()  
     { 
          bigFont = new Font("Arial",Font.BOLD,16);
          redColor = Color.red;
          weirdColor = new Color(60,60,122);
      setBackground(bgColor);
     }

    @Override
     public void stop() { }

    @Override
     public void paint(Graphics g)  
     { 
      g.setFont(bigFont); 
      g.drawString("Shapes and Colors",80,20);     
      g.setColor(redColor);
      g.drawRect(100,100,100,100);
      g.fillRect(100,100,100,100);
     }
}

I am not going to write your applet for you, but I'll give you some steps to get you started: 我不会为你编写你的applet,但我会给你一些步骤来帮助你入门:

In your init: 在你的init:

  • Set a timer, that calls the refresh method every time. 设置一个定时器,每次调用刷新方法。
  • Set a global counter to 0 将全局计数器设置为0

In your refresh method: 在您的刷新方法中:

  • Increase the counter by 1 (possibly mod 360 to keep it in the 0-359 range) 将计数器增加1(可能是mod 360以使其保持在0-359范围内)
  • Call the repaint method 调用重绘方法

In your paint method: 在你的绘画方法:

  • Turn the Canvas the number of degrees the counter is on (possibly using an AffineTransform object) 将Canvas旋转计数器所在的度数(可能使用AffineTransform对象)
  • Paint your image/square/shape/anything 绘制图像/方形/形状/任何东西

Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM