How do I call a function with a graphics object from another class?

断了今生、忘了曾经 提交于 2021-02-20 03:59:28

问题


I want to call paint() from main() but I need a parameter.I don't know which parameter to pass and I can't seem to use the Graphics object when I define g outside the parameters since it can't be initialized.

I tried creating an object of the Graphics class in main() and then passing it as a parameter but then whenever I try to use g it gies me a nullException

import java.util.*;
import java.awt.*;
import javax.swing.JFrame;
 class Boards extends Canvas
{
    JFrame frame;    
     void frame()
    {
        JFrame frame = new JFrame("SNAKES AND LADDERS");
        Canvas canvas= new Boards();
        canvas.setSize(1000,1000);
        frame.add(canvas);
        frame.pack();
        frame.setVisible(true); 
        frame.getGraphics();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        Graphics g;

    }
    public void paint(Graphics g)
    {
      g.fillRect(0,0,100,1000);
    }
}

 class snakes_and_ladders
{
    Scanner s= new Scanner (System.in);
    Boards board= new Boards();
     void main()    
    {
        board.frame();
        board.paint();
    }
}

回答1:


You will need to call repaint. From the docs:

public void repaint(long tm, int x, int y, int width, int height)

Adds the specified region to the dirty region list if the component is showing. The component will be repainted after all of the currently pending events have been dispatched.

EDIT

An example of indirect parameterization:

protected String test = "foo";

public void myRepaint(long tm, int x, int y, int width, int height, test) {
    this.test = test;
    repaint(tm, x, y, width, height);
}

public void paint(Graphics g) {
    //do something with this.test
}



回答2:


To redraw your graphic area, don't call the paint method directly but use the invalidate method instead.




回答3:


I would recommend you use Swing extend JPanel and override paintComponent. Canvas is an AWT class and you should not mix Swing and AWT capabilities.

And make certain that you call super.paintComponent() as the first statement. To repaint the panel all you need to do is call repaint().

You should never call a paint method directly as it painting should be done in the Event Dispatch Thread (EDT). Nor do you need to safe the graphics context.

Here is a very simple example that demonstrates movement.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MovementDemo extends JPanel implements ActionListener {

   JFrame frame      = new JFrame("Movement Demo");
   int    size       = 500;
   int    x          = 50;
   int    y          = 200;
   int    diameter   = 50;
   int    yinc       = 2;
   int    xinc       = 2;
   int    xdirection = 1;
   int    ydirection = 1;

   public MovementDemo() {
      setPreferredSize(new Dimension(size, size));
      frame.add(this);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> new MovementDemo().start());
   }

   public void start() {

      Timer timer = new Timer(100, this);
      timer.setDelay(5);
      timer.start();
   }

   public void actionPerformed(ActionEvent ae) {

      if (x < 0) {
         xdirection = 1;
      }
      else if (x > size - diameter) {
         xdirection = -1;
      }
      if (y < 0) {
         ydirection = 1;
      }
      else if (y > size - diameter) {
         ydirection = -1;
      }
      x = x + xdirection * xinc;
      y = y + ydirection * yinc;
      repaint();
   }

   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g.create();
      g2d.setColor(Color.BLUE);
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2d.fillOval(x, y, diameter, diameter);

   }
}


来源:https://stackoverflow.com/questions/57124471/how-do-i-call-a-function-with-a-graphics-object-from-another-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!