如何更改java.awt.Graphics绘制对象的方式?
我正在创建一个游戏,有一个玩家类,一个敌人类,一个子弹类。在我的引擎设计中,通过简单地调用如何更改java.awt.Graphics绘制对象的方式?,java,graphics,awt,draw,Java,Graphics,Awt,Draw,我正在创建一个游戏,有一个玩家类,一个敌人类,一个子弹类。在我的引擎设计中,通过简单地调用 用于(形状:形状) { g、 填海 } 其中shapes是我游戏中每个对象的现有列表。这很好,但是我希望子弹的颜色不同于玩家,玩家不同于敌人。我如何修改我的玩家、子弹和敌人类,以便awt.Graphics知道将它们绘制为哪种颜色 编辑:另一个可接受的答案可能包括找到一种方法来对形状进行排序,并按类别将其分开,尽管这似乎与已经存在的完整列表背道而驰。您需要使用OOP 范例 您需要使用OOP 范例 如果您只需
用于(形状:形状)
{
g、 填海
}
其中shapes
是我游戏中每个对象的现有列表。这很好,但是我希望子弹的颜色不同于玩家,玩家不同于敌人。我如何修改我的玩家、子弹和敌人类,以便awt.Graphics知道将它们绘制为哪种颜色
编辑:另一个可接受的答案可能包括找到一种方法来对形状进行排序,并按类别将其分开,尽管这似乎与已经存在的完整列表背道而驰。您需要使用OOP
范例
您需要使用OOP
范例
如果您只需要颜色区分,请在调用g.fill(s)
之前调用g.setColor(color)
如果您只需要颜色区分,请在调用g.fill(s)之前调用g.setColor(color)
您可以使用某种地图将每个形状与颜色关联起来。然后在绘制每个形状时,您只需执行以下操作:
for (Shape shape : shapes) {
Color color = mapColors.get(shape)(
if (color != null) {
g.setColor(color);
} else{
// set the color to a default value
}
g.fill(shape);
}
对我来说,这种方法的问题在于它将形状与颜色分离。它还限制了绘制资产的方式
更好的解决方案是设计一个Asset
类,该类提供了一个简单的paint
方法
public interface Asset {
public void paint(Graphics g);
}
然后,您只需使用资产的列表
,类似于您的形状列表,但调用paint(g)
(在资产的实例上)相反,允许资源决定它应该如何绘制…您可以使用某种地图将每个形状与颜色关联。然后,在绘制每个形状时,您只需执行以下操作
for (Shape shape : shapes) {
Color color = mapColors.get(shape)(
if (color != null) {
g.setColor(color);
} else{
// set the color to a default value
}
g.fill(shape);
}
对我来说,这种方法的问题在于它将形状与颜色分离。它还限制了绘制资产的方式
更好的解决方案是设计一个Asset
类,该类提供了一个简单的paint
方法
public interface Asset {
public void paint(Graphics g);
}
然后,您只需使用资产的列表
,类似于您的形状列表,但调用绘制(g)
(在资产的实例上)
,允许资产决定如何绘制…类似以下内容:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
class ColoredShapes {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JPanel gui = new JPanel(new BorderLayout(5,5));
final ArrayList<Drawable> drawables = new ArrayList<Drawable>();
final PaintPanel paintPanel = new PaintPanel(drawables);
gui.add(paintPanel, BorderLayout.CENTER);
final Random r = new Random();
JToolBar tools = new JToolBar();
gui.add(tools, BorderLayout.PAGE_START);
JButton addCircle = new JButton("Add Circle");
ActionListener addCircleListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Point p = new Point(r.nextInt(400), r.nextInt(400));
int s = r.nextInt(50) + 50;
Color c = new Color(
r.nextInt(255), r.nextInt(255), r.nextInt(255));
drawables.add(new DrawableCircle(p, s, c));
paintPanel.repaint();
}
};
tools.add(addCircle);
addCircle.addActionListener(addCircleListener);
JButton addSquare = new JButton("Add Square");
ActionListener addSquareListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Point p = new Point(r.nextInt(400), r.nextInt(400));
int s = r.nextInt(50) + 50;
Color c = new Color(
r.nextInt(255), r.nextInt(255), r.nextInt(255));
drawables.add(new DrawableSquare(p, s, c));
paintPanel.repaint();
}
};
tools.add(addSquare);
addSquare.addActionListener(addSquareListener);
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
interface Drawable {
public abstract void draw(Graphics2D g);
public abstract Color getColor();
}
class DrawableCircle implements Drawable {
private Color color;
private Shape shape;
DrawableCircle(Point topLeft, int size, Color color) {
shape = new Ellipse2D.Double(topLeft.x, topLeft.y, size, size);
this.color = color;
}
public void draw(Graphics2D g) {
g.setColor(getColor());
g.fill(shape);
}
public Color getColor() {
return color;
}
}
class DrawableSquare implements Drawable {
private Color color;
private Shape shape;
DrawableSquare(Point topLeft, int size, Color color) {
shape = new Rectangle2D.Double(topLeft.x, topLeft.y, size, size);
this.color = color;
}
public void draw(Graphics2D g) {
g.setColor(getColor());
g.fill(shape);
}
public Color getColor() {
return color;
}
}
class PaintPanel extends JPanel {
Dimension preferredSize = new Dimension(400,400);
ArrayList<Drawable> drawables;
PaintPanel(ArrayList<Drawable> drawables) {
this.drawables = drawables;
}
public Dimension getPreferredSize() {
return preferredSize;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Drawable drawable : drawables) {
drawable.draw(g2);
}
}
}
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.geom.Ellipse2D;
导入java.awt.geom.Rectangle2D;
导入java.util.ArrayList;
导入java.util.Random;
导入javax.swing.*;
类彩色形状{
公共静态void main(字符串[]args){
Runnable r=新的Runnable(){
@凌驾
公开募捐{
JPanel gui=新JPanel(新边界布局(5,5));
最终ArrayList drawables=新ArrayList();
最终漆面漆面=新漆面(可拉伸);
添加(paintPanel,BorderLayout.CENTER);
最终随机r=新随机();
JToolBar工具=新的JToolBar();
添加(工具、边框布局、页面开始);
JButton addCircle=新JButton(“添加圆”);
ActionListener addCircleListener=新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
点p=新点(r.nextInt(400),r.nextInt(400));
int s=r.nextInt(50)+50;
颜色c=新颜色(
r、 nextInt(255)、r.nextInt(255)、r.nextInt(255));
添加(新的可绘制圆圈(p、s、c));
paintPanel.repaint();
}
};
工具。添加(addCircle);
addCircle.addActionListener(addCircleListener);
JButton addSquare=新JButton(“addSquare”);
ActionListener addSquareListener=新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
点p=新点(r.nextInt(400),r.nextInt(400));
int s=r.nextInt(50)+50;
颜色c=新颜色(
r、 nextInt(255)、r.nextInt(255)、r.nextInt(255));
添加(新的可绘制正方形(p、s、c));
paintPanel.repaint();
}
};
工具。添加(addSquare);
addSquare.addActionListener(addSquareListener);
showMessageDialog(null,gui);
}
};
//应在EDT上创建和更新Swing GUI
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
界面可拉伸{
公开摘要作废图(图2d g);
公共抽象颜色getColor();
}
类DrawableCircle实现了DrawableCircle{
私人色彩;
私人造型;
可绘制圆(点左上角,整数大小,颜色){
形状=新的椭圆2d.Double(左上角x,左上角y,大小,大小);
这个颜色=颜色;
}
公共作废图纸(图形2D g){
g、 setColor(getColor());
g、 填充(形状);
}
公共颜色getColor(){
返回颜色;
}
}
类DrawableSquare实现了DrawableSquare{
私人色彩;
私人造型;
DrawableSquare(点左上角,整数大小,颜色){
形状=新矩形2D.Double(topLeft.x,topLeft.y,大小