简体   繁体   中英

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

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();
    }
}

要重绘图形区域,请勿直接调用paint方法,而应使用invalidate方法。

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
}

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);

   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM