簡體   English   中英

如何在Java中以其他方法調用Graphics2D方法?

[英]How to call a Graphics2D method in other method, in java?

如何在游戲方法中調用繪畫方法?

如果我用命令調用“ new paint();” 它不起作用

// Main
public Game() {
    super();
    setTitle("Breakout");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(500, 500);
    setLocationRelativeTo(null);
    setIgnoreRepaint(true);
    setResizable(false);
    setVisible(true);
}

// Grafica
class paint extends JPanel {

    public paint(Graphics2D g) {
        super.paint(g);
        g.setColor(Color.black);

    }

}

大多數程序員在JPanel超類中使用受保護的swing方法paintComponent(Graphics g)匿名調用repaint()來更改游戲值並更改組件的外觀。 使用SwingTimer將在預定的時間內更新結果。

public class game extends JPanel implements ActionListener{

      int camX, camY;
      int update_time = 5;// in milliseconds...
      Graphics2D g2d;
      Image image;
      Timer t;

     public game(){

          t = new Timer(update_time, this);// given "this" is an ActionListener...
          t.start();// calls actionPerformed method every 5ms...

     }


     public void actionPerformed(ActionEvent e){

            repaint();//calls paintComponent() method...

     }


      public void paintComponent(Graphics g){

          super.paintComponent(g);
          g2d = (Graphics2D)g;

          g2d.drawImage(image, camX, camY, this);
          //Do graphical stuff
      }



}

暫無
暫無

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

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