我正在努力创造和理解一个简单射击游戏程序的具体方法

时间:2016-10-21 16:55:48

标签: java methods graphics

这些是具体的指示,但它们让我感到困惑(指令令人困惑/模棱两可,还是我没有得到它?)

写一个方法public static void draw shooter(Graphics g,Color c); 使用射手颜色作为drawAll(?)中的最后一个参数来调用绘制射手 测试程序你应该看到一个红色磁盘居中靠近屏幕的底部(?)

import java.awt.*;

public class Project2{
  public static final int PANEL_WIDTH = 300;
  public static final int PANEL_HEIGHT = 300;
  public static final int SLEEP_TIME = 50;
  public static Color SHOOTER_COLOR = Color.RED;
  public static Color BACKGROUND_COLOR = Color.WHITE;
  public static final int SHOOTER_SIZE = 20; //diameter of the shooter
  public static final int GUN_SIZE = 10; //length og the gun
  public static final int SHOOTER_POSITION_Y = PANEL_HEIGHT - SHOOTER_SIZE;
  public static final int SHOOTER_INITIAL_POSITION_X = 150;
  int shooterPosition;

  public static void initialize(){
    int shooterPositionX = SHOOTER_INITIAL_POSITION_X;
  }

  public static void main(String[] args) {
    DrawingPanel panel = new DrawingPanel(PANEL_WIDTH, PANEL_HEIGHT);
    Graphics g = panel.getGraphics( );
    initialize();
    startGame(panel, g);
    drawShooter(g, SHOOTER_COLOR);
  }

  public static void drawShooter(Graphics g, Color C){
    g.setColor(Color);
    g.fillOval(shooterPosition, SHOOTER_POSITION_Y, SHOOTER_SIZE, SHOOTER_SIZE);
  }

  public static void drawAll(Graphics g){
    g.drawString("Project 2 by Jasmine Ramirez", 10, 15);
  }

  public static void startGame(DrawingPanel panel, Graphics g) {

    for (int i = 0; i <= 10000; i++) {
      panel.sleep(SLEEP_TIME);
      drawAll(g);
    }
  }
}

这是我的代码我猜我需要在方法内的圆圈中绘制和着色,但是我在方法中遇到了g.setColor错误,我不确定第二步意味着什么。谢谢你刚开始学习编程。

绘图面板

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.ArrayList;

public class DrawingPanel implements ActionListener {
 private static final String versionMessage = 
    "Drawing Panel version 1.1, January 25, 2015";
 private static final int DELAY = 100;  // delay between repaints in millis
 private static final boolean PRETTY = false;  // true to anti-alias
 private static boolean showStatus = false;
 private static final int MAX_KEY_BUF_SIZE = 10;

 private int width, height;    // dimensions of window frame
 private JFrame frame;         // overall window frame
 private JPanel panel;         // overall drawing surface
 private BufferedImage image;  // remembers drawing commands
 private Graphics2D g2;        // graphics context for painting
 private JLabel statusBar;     // status bar showing mouse position
 private volatile MouseEvent click;     // stores the last mouse click
 private volatile boolean pressed;      // true if the mouse is pressed
 private volatile MouseEvent move;      // stores the position of the mouse
 private ArrayList<KeyInfo> keys;

 // construct a drawing panel of given width and height enclosed in a window
 public DrawingPanel(int width, int height) {
   this.width = width;
   this.height = height;
   keys = new ArrayList<KeyInfo>();
   image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

   statusBar = new JLabel(" ");
   statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));
   statusBar.setText(versionMessage);

   panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
   panel.setBackground(Color.WHITE);
   panel.setPreferredSize(new Dimension(width, height));
   panel.add(new JLabel(new ImageIcon(image)));

   click = null;
   move = null;
   pressed = false;

   // listen to mouse movement
   MouseInputAdapter listener = new MouseInputAdapter() {
     public void mouseMoved(MouseEvent e) {
       pressed = false;
       move = e;
       if (showStatus)
          statusBar.setText("moved (" + e.getX() + ", " + e.getY() + ")");
     }

     public void mousePressed(MouseEvent e) {
       pressed = true;
       move = e;
       if (showStatus)
          statusBar.setText("pressed (" + e.getX() + ", " + e.getY() + ")");
     }

     public void mouseDragged(MouseEvent e) {
       pressed = true;
       move = e;
       if (showStatus)
          statusBar.setText("dragged (" + e.getX() + ", " + e.getY() + ")");
     }

     public void mouseReleased(MouseEvent e) {
       click = e;
       pressed = false;
       if (showStatus)
          statusBar.setText("released (" + e.getX() + ", " + e.getY() + ")");
     }

     public void mouseEntered(MouseEvent e) {
//       System.out.println("mouse entered");
       panel.requestFocus();
     }

   };
   panel.addMouseListener(listener);
   panel.addMouseMotionListener(listener);
   new DrawingPanelKeyListener();

   g2 = (Graphics2D)image.getGraphics();
   g2.setColor(Color.BLACK);
   if (PRETTY) {
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     g2.setStroke(new BasicStroke(1.1f));
   }

   frame = new JFrame("Drawing Panel");
   frame.setResizable(false);
   try {
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // so that this works in an applet
   } catch (Exception e) {}
   frame.getContentPane().add(panel);
   frame.getContentPane().add(statusBar, "South");
   frame.pack();
   frame.setVisible(true);
   toFront();
   frame.requestFocus();

   // repaint timer so that the screen will update
   new Timer(DELAY, this).start();
 }

 public void showMouseStatus(boolean f) {
   showStatus = f;
 }

 public void addKeyListener(KeyListener listener) {
   panel.addKeyListener(listener);
   panel.requestFocus();
 }

 // used for an internal timer that keeps repainting
 public void actionPerformed(ActionEvent e) {
   panel.repaint();
 }

 // obtain the Graphics object to draw on the panel
 public Graphics2D getGraphics() {
   return g2;
 }

 // set the background color of the drawing panel
 public void setBackground(Color c) {
   panel.setBackground(c);
 }

 // show or hide the drawing panel on the screen
 public void setVisible(boolean visible) {
   frame.setVisible(visible);
 }

 // makes the program pause for the given amount of time,
 // allowing for animation
 public void sleep(int millis) {
   panel.repaint();
   try {
     Thread.sleep(millis);
   } catch (InterruptedException e) {}
 }

 // close the drawing panel
 public void close() {
   frame.dispose();
 }

 // makes drawing panel become the frontmost window on the screen
 public void toFront() {
   frame.toFront();
 }

 // return panel width
 public int getWidth() {
    return width;
 }

 // return panel height
 public int getHeight() {
    return height;
 }

 // return the X position of the mouse or -1
 public int getMouseX() {
   if (move == null) {
     return -1;
   } else {
     return move.getX();
   }
 }

 // return the Y position of the mouse or -1
 public int getMouseY() {
   if (move == null) {
     return -1;
   } else {
     return move.getY();
   }
 }

 // return the X position of the last click or -1
 public int getClickX() {
   if (click == null) {
     return -1;
   } else {
     return click.getX();
   }
 }

 // return the Y position of the last click or -1
 public int getClickY() {
   if (click == null) {
     return -1;
   } else {
     return click.getY();
   }
 }

 // return true if a mouse button is pressed
 public boolean mousePressed() {
   return pressed;
 }

 public synchronized int getKeyCode() {
   if (keys.size() == 0)
     return 0;
   return keys.remove(0).keyCode;
 }

  public synchronized char getKeyChar() {
   if (keys.size() == 0)
     return 0;
   return keys.remove(0).keyChar;
 }

  public synchronized int getKeysSize() {
    return keys.size();
  }

 private synchronized void insertKeyData(char c, int code) {
   keys.add(new KeyInfo(c,code));
   if (keys.size() > MAX_KEY_BUF_SIZE) {
     keys.remove(0);
//     System.out.println("Dropped key");
   }
 }

 private class KeyInfo {
   public int keyCode;
   public char keyChar;

   public KeyInfo(char keyChar, int keyCode) {
     this.keyCode = keyCode;
     this.keyChar = keyChar;
   }
 }

 private class DrawingPanelKeyListener implements KeyListener {

   int repeatCount = 0;

   public DrawingPanelKeyListener() {
     panel.addKeyListener(this);
     panel.requestFocus();
   }

   public void keyPressed(KeyEvent event) {
//     System.out.println("key pressed");
     repeatCount++;
     if ((repeatCount == 1) || (getKeysSize() < 2))
        insertKeyData(event.getKeyChar(),event.getKeyCode());
   }

   public void keyTyped(KeyEvent event) {
   }

   public void keyReleased(KeyEvent event) {
     repeatCount = 0;
   }

 }

}

3 个答案:

答案 0 :(得分:2)

第一次错误

使用drawShooter()方法:

  

g.setColor(Color)

这是不正确的,因为你需要传递类Color的实例而不是类本身。

所以改为使用:

g.setColor(C);

第二次错误

shooterPosition更改为static,以便可以通过静态方法访问它。 我假设方法initialize()也是错误的,因为你无缘无故地声明一个新的shooterPosition int,所以这些更改:

  

int shooterPosition;

要:

public static int shooterPosition;

  

public static void initialize(){ int shooterPositionX = SHOOTER_INITIAL_POSITION_X; }

要:

public static void initialize() {
    shooterPosition = SHOOTER_INITIAL_POSITION_X;
}

第三次错误

startGame()中,你循环10000次,每次等待的时间超过1/20秒,这意味着你必须等待近10分钟,直到红圈为画。所以你有两个选择。

第一个选项:减少迭代次数,甚至更好地删除循环。

  

public static void startGame(DrawingPanel panel, Graphics g) { for (int i = 0; i <= 10000; i++) { panel.sleep(SLEEP_TIME); drawAll(g); } }

要:

public static void startGame(DrawingPanel panel, Graphics g) {
    for (int i = 0; i <= 1; i++) {
        panel.sleep(SLEEP_TIME);
        drawAll(g);
    }
}

public static void startGame(DrawingPanel panel, Graphics g) {
        panel.sleep(SLEEP_TIME);
        drawAll(g);
}

第二个选项:在drawShooter()方法之前执行startGame()方法,或者根本不执行startGame()

  

startGame(panel, g); drawShooter(g, SHOOTER_COLOR);

要:

drawShooter(g, SHOOTER_COLOR);
startGame(panel, g);

drawShooter(g, SHOOTER_COLOR);

答案 1 :(得分:1)

你昨天没有问同样的问题或类似的问题吗?并且您将类名传递给g.setColor(Color)方法,并且需要传入包含该对象的参数:g.setColor(C)

你对图形的使用并不好,因为你不应该通过getGraphics()使用从组件中获得的图形,但我猜测它是因为那个&#39;你的导师告诉你的事情。使用while (true)循环也是如此。相反,你应该使用Swing Timer。

答案 2 :(得分:0)

如前所述,g.setcolor(c)是必需的

检查你的错误消息,因为它允许你收集错误和shooterPosition错误(它不公开,所以不能在方法中使用)

相关问题