簡體   English   中英

將Java Applet轉換為應用程序

[英]Converting a Java Applet to an Application

我知道這個問題已經在其他地方回答了,但似乎都沒有。 我不知道解決方案是否取決於代碼,但這就是我的猜測。 無論如何,我要做的就是把這個簡單的Applet(一個Pong游戲)並將其更改為Application。 可能吧? 先感謝您! :)

package net.laserball.src;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.swing.JApplet;
import javax.swing.JComponent;

public class LaserBallMain extends JApplet 
{
private static final long serialVersionUID = 1L;

public static final int WIDTH = 400;
public static final int HEIGHT = 400;

private PaintSurface canvas;

public void init()
{
    this.setSize(WIDTH, HEIGHT);
    canvas = new PaintSurface();
    this.add(canvas, BorderLayout.CENTER);

    ScheduledThreadPoolExecutor executor = 
        new ScheduledThreadPoolExecutor(3);
    executor.scheduleAtFixedRate(
        new AnimationThread(this), 
        0L, 20L, TimeUnit.MILLISECONDS);

}
}

class AnimationThread implements Runnable
{
JApplet c;

public AnimationThread(JApplet c)
{
    this.c = c;
}

public void run()
{
    c.repaint();
}
}

class PaintSurface extends JComponent
{
private static final long serialVersionUID = 1L;

int paddle_x = 0;
int paddle_y = 360;

int score = 0;

float english = 1.0F;

Ball ball;

Color[] color = {Color.RED, Color.ORANGE,
                 Color.MAGENTA, Color.ORANGE,
                 Color.CYAN, Color.BLUE};

int colorIndex;

public PaintSurface()
{
    addMouseMotionListener(new MouseMotionAdapter()
    {
        public void mouseMoved(MouseEvent e)
        {
            if(e.getX() - 30 - paddle_x > 5)
                english = -1.5F;
            else if (e.getX() - 30 - paddle_x < -5)
                english = -1.5F;
            else
                english = 1.0F;
            paddle_x = e.getX() - 30;
        }
    });

    ball = new Ball(20);
}

public void paint(Graphics g)
{
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING, 
        RenderingHints.VALUE_ANTIALIAS_ON);

    Shape paddle = new Rectangle2D.Float(
        paddle_x, paddle_y, 60, 8);

    g2.setColor(color[colorIndex % 6]);

    if(ball.intersects(paddle_x, paddle_y, 60, 8)
        && ball.y_speed > 0)
    {
        ball.y_speed = -ball.y_speed;
        ball.x_speed = (int)(ball.x_speed * english);
        if(english != 1.0F)
            colorIndex++;
        score += Math.abs(ball.x_speed * 10);
    }

    if(ball.getY() + ball.getHeight()
        >= LaserBallMain.HEIGHT)
    {
        ball = new Ball(20);
        score -= 1000;
        colorIndex = 0;
    }

    ball.move();
    g2.fill(ball);

    g2.setColor(Color.BLACK);
    g2.fill(paddle);

    g2.drawString("Score: " + score, 250, 20);
}
}

class Ball extends Ellipse2D.Float
{
private static final long serialVersionUID = 1L;

public int x_speed, y_speed;
private int d;
private int width = LaserBallMain.WIDTH;
private int height = LaserBallMain.HEIGHT;

public Ball(int diameter)
{
    super((int)(Math.random() * (LaserBallMain.WIDTH - 20) + 1),
        0, diameter, diameter);
    this.d = diameter;
    this.x_speed = (int)(Math.random() * 5 + 5);
    this.y_speed = (int)(Math.random() * 5 + 5);
}

public void move()
{
    if(super.x < 0 || super.x > width - d)
        x_speed = -x_speed;
    if(super.y < 0 || super.y > height - d)
        y_speed = -y_speed;
    super.x += x_speed;
    super.y += y_speed;     
}
}
  • 創建一個名為Main類的新類
  • Main創建方法,稱為public static void main(String args[])
  • main方法中,使用EventQueue.invokeLater並將其傳遞給Runnable以執行事件調度線程中的初始程序邏輯。

例如...

EventQueue.invokeLater(new Runnable() {
    public void run() {
    }
});
  • 在run方法中,創建一個JFrame的新實例。
  • 將原始init方法中的邏輯也放置在run方法中,但不要將canvas添加到applet,而是將其添加到框架(在上一步中創建)。

您可能需要重寫PaintSurface類的getPreferredSize ,返回適當大小的游戲表面。

您還應該看看“ 執行自定義繪畫”,並且可能會發現如何使用Swing計時器也很有趣。

就個人而言,我會將整個程序邏輯移至其他類,例如可能是JPanel ,那么您只需要將此組件添加到所需的任何容器中

LaserBallMain laserBallMain = new LaserBallMain();

JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 1));
frame.add(laserBallMain);

// Set frame size and other properties
...

// Call applet methods
laserBallMain.init();
laserBallMain.start();

frame.setVisible(true);

暫無
暫無

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

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