java - 绘制椭圆形的图形不会在java中增加大小

标签 java swing graphics 2d-games

我正在使用 OvalsGraphics 对象在 java 中编写一个简单的游戏。它被称为病毒,它的工作原理是:中间有一个椭圆,外面有六个椭圆。这些外面的椭圆应该会变大直到被点击,然后它们就会消失并且玩家得 10 分。如果椭圆接触到中央椭圆,则中央椭圆的健康状况会下降。当它归零时,游戏结束。我遇到的问题是外部椭圆的大小不会增加。为什么会这样?

这是我的代码:

package virus;


import java.awt.*;
import java.util.Random;
import javax.swing.JPanel;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class VirusGamePanel extends JPanel implements MouseListener{
    private static final long serialVersionUID = 1L;//serialVersionUID field
    Random colour = new Random();//the outside ovals will always be a random colour
    private int sizeX = 1;//the x size of the outside ovals 
    private int sizeY = 1;//the y size of the outside ovals
    int score = 0;
    static String scorestring = "Score: ";
    Color rand = new Color(colour.nextInt(255),colour.nextInt(255),colour.nextInt(255));//generate the random colour

    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.magenta);
        g.drawString(scorestring+score,275,250);
        g.setColor(Color.orange);
        g.drawOval(200,150,200,200);
        g.setColor(rand);
        g.drawOval(270,50,50,50);
        g.drawOval(100,100,50,50);
        g.drawOval(450,100,50,50);
        g.drawOval(100,400,50,50);
        g.drawOval(450,400,50,50);
        g.drawOval(275,450,50,50);
        g.fillOval(270,50,sizeX,sizeY);//these six ovals are supposed to increase in size
        g.fillOval(100,100,sizeX,sizeY);
        g.fillOval(450,100,sizeX,sizeY);
        g.fillOval(100,400,sizeX,sizeY);
        g.fillOval(450,400,sizeX,sizeY);
        g.fillOval(275,450,sizeX,sizeY);
        inc();
    }

    public static void main(String[] args) {
      JPanel panel = new VirusGamePanel();

      JFrame frame = new JFrame("Virus");
      frame.setSize(700, 700);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel);
      frame.setVisible(true);
    }

    private void inc()//increase the size of the ovals
    {
        for(int i = 0; i<25000; i++)
        {
            sizeX++;
            sizeY++;
            repaint();
        }
    }

带线程的新代码:

package virus;


import java.awt.*;
import java.util.Random;
import javax.swing.JPanel;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class VirusGamePanel extends JPanel implements MouseListener,Runnable{
    private static final long serialVersionUID = 1L;
    Random colour = new Random();
    private int sizeX = 1;
    private int sizeY = 1;
    int score = 0;
    Thread thr = new Thread();
    static String scorestring = "Score: ";
    Color rand = new Color(colour.nextInt(255),colour.nextInt(255),colour.nextInt(255));

    public void paint(Graphics g)
    {
        super.paint(g);
        thr.start();
        g.setColor(Color.magenta);
        g.drawString(scorestring+score,275,250);
        g.setColor(Color.orange);
        g.drawOval(200,150,200,200);
        g.setColor(rand);

        g.fillOval(270,50,sizeX,sizeY);
        g.fillOval(100,100,sizeX,sizeY);
        g.fillOval(450,100,sizeX,sizeY);
        g.fillOval(100,400,sizeX,sizeY);
        g.fillOval(450,400,sizeX,sizeY);
        g.fillOval(275,450,sizeX,sizeY);
        inc();
    }

    public static void main(String[] args) {}

    private void inc()
    {
        thr.run();
    }
    public void run(){
        for(int i = 0; i<25000; i++)
        {
            sizeX++;
            sizeY++;
            repaint();
            try
            {
                Thread.sleep(10);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }

最佳答案

这对我有用:

public class VirusGamePanel extends JPanel{
    private static final long serialVersionUID = 1L;//serialVersionUID field
    Random colour = new Random();//the outside ovals will always be a random colour
    private int sizeX = 0;//the x size of the outside ovals 
    private int sizeY = 0;//the y size of the outside ovals
    int score = 0;
    static String scorestring = "Score: ";
    Color rand = new Color(colour.nextInt(255), colour.nextInt(255), colour.nextInt(255)); //generate the random colour

    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.magenta);
        g.drawString(scorestring+score,275,250);
        g.setColor(Color.orange);
        g.drawOval(200, 150, 200, 200);
        g.setColor(rand);
        g.fillOval(270 - sizeX / 2, 50 - sizeY / 2, sizeX, sizeY);//these six ovals are supposed to increase in size
        g.fillOval(100 - sizeX / 2,100 - sizeY / 2, sizeX, sizeY);
        g.fillOval(450 - sizeX / 2,100 - sizeY / 2, sizeX, sizeY);
        g.fillOval(100 - sizeX / 2,400 - sizeY / 2, sizeX, sizeY);
        g.fillOval(450 - sizeX / 2,400 - sizeY / 2, sizeX, sizeY);
        g.fillOval(275 - sizeX / 2,450 - sizeY / 2, sizeX, sizeY);
        inc();
    }

    public static void main(String[] args) {
      JPanel panel = new VirusGamePanel();

      JFrame frame = new JFrame("Virus");
      frame.setSize(700, 700);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel);
      frame.setVisible(true);
    }

    private void inc()//increase the size of the ovals
    {
            sizeX++;
            sizeY++;
            repaint();
    }
}

只需固定椭圆的中心点并删除 inc 方法中的循环。如果要绘制椭圆形边界,只需添加参数与 fillOval 命令相同的 drawOval 命令即可。

编辑:

如果你想减缓增长过程,只需在 paint 方法的 inc() 调用之前添加以下内容:

try {
    Thread.sleep(100);
} catch (InterruptedException e) {
    e.printStackTrace();
}

关于java - 绘制椭圆形的图形不会在java中增加大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093547/

相关文章:

java - 用 Swagger 表示 ISO 8601 年月日期文档

java - 从另一个类访问 GUI 组件

c++ - Box2D 旋转中心

java - 扩展三角形文件

java - 将任何原始数据类型转换为字符串的最佳方法是什么

java - 无法从 CMD 运行 java

java - 在很多 jTextField 中选择所有焦点

java - 当分配新的子类时,JPanel 不会重新绘制

c++ - 带阴影的聚光灯变成方形

java - 如何在 JLabel 上绘图?