我目前正在尝试了解更多关于绘画在swing中的信息,并且我正在创建一个小游戏,但我似乎被卡住了。我试图给ArrayList添加几个矩形(敌人),然后在paintComponent()
方法中绘制它们,但它似乎不起作用。无法在JPanel上绘制矩形
这里是我的敌人类
import utils.RandomUtils;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class Enemy
{
double x, y, w, h;
Shape enemy;
Toolkit tk = Toolkit.getDefaultToolkit();
double width = tk.getScreenSize().width;
double height = tk.getScreenSize().height;
RandomUtils utils = new RandomUtils();
public Enemy(int w, int h)
{
this.w = w;
this.h = h;
enemy = new Rectangle2D.Double(setX(), setY(), w, h);
}
private double setX()
{
return x = utils.randInt((int) w, (int) width);
}
private double setY()
{
return y = utils.randInt((int) h, (int) height);
}
public Shape getEnemy()
{
return enemy;
}
}
这是我局级
import players.Enemy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
public class Board extends JPanel
{
int x, y, speed = 1, height = 25, width = 25;
public Board()
{
addControls();
}
public void addControls()
{
Action upAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
y = y - speed;
repaint();
}
};
getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("W"), "Up Action");
getActionMap().put("Up Action", upAction);
Action downAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
y = y + speed;
repaint();
}
};
getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("S"), "Down Action");
getActionMap().put("Down Action", downAction);
Action leftAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
x = x - speed;
repaint();
}
};
getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("A"), "Left Action");
getActionMap().put("Left Action", leftAction);
Action rightAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
x = x + speed;
repaint();
}
};
getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("D"), "Right Action");
getActionMap().put("Right Action", rightAction);
}
boolean enemiesDrawn = false;
java.util.List<Enemy> enemies = new ArrayList<>();
java.util.List<Shape> enemyShapes = new ArrayList<>();
public void setEnemies()
{
for(int enemyCount = 0; enemyCount < 15; enemyCount++)
{
System.out.println(enemyCount);
enemies.add(new Enemy(15, 15));
enemyShapes.add(enemies.get(enemyCount).getEnemy());
}
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.RED);
g.fillOval(x, y, width, height);
if(!enemiesDrawn)
{
setEnemies();
g2.setColor(Color.BLUE);
g2.setBackground(Color.BLUE);
enemyShapes.forEach(g2::draw);
enemiesDrawn = true;
}
}
}
的Util类
import java.util.Random;
public class RandomUtils
{
public RandomUtils()
{
}
public int randInt(int min, int max)
{
Random rand = new Random();
return rand.nextInt((max - min) + 1) + min;
}
}
而且主类
import javax.swing.*;
public class MainFrame
{
JFrame mainFrame;
public MainFrame()
{
mainFrame = new JFrame("Game");
addComponents();
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainFrame.setLocationRelativeTo(null);
mainFrame.pack();
mainFrame.setVisible(true);
}
public void addComponents()
{
mainFrame.add(new Board());
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(MainFrame::new);
}
}
绘画椭圆形(玩家)的作品很好,我似乎无法吸引列表中的敌人。我应该做什么来解决这个问题?
什么是有两个的ArrayList点的电流大小的敌人?考虑提供一个演示你的问题的可运行示例 – MadProgrammer
一个用于敌人自己,另一个用于敌人的形状。我将发布完整的代码。 – Jonah
侧面推荐,永远不要处理由JVM给您的Graphics对象'g2.dispose();'。只能处理您自己创建的图像或其他图形对象。这样做有可能破坏绘画链。 –