如何使用来自另一个 class 的图形对象调用函数?
How do I call a function with a graphics object from another class?
我想从 main()
调用 paint()
但我需要一个 parameter.I 不知道要传递哪个参数而且我似乎无法使用 Graphics
对象,当我在参数外定义 g
时,因为它无法初始化。
我尝试在 main()
中创建图形 class 的对象,然后将其作为参数传递,但是每当我尝试使用 g 时,它都会给我一个 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
方法。
您需要致电 repaint。来自文档:
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.
编辑
间接参数化示例:
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
}
我建议您使用 Swing
扩展 JPanel
并覆盖 paintComponent
。 Canvas
是 AWT
class,您不应混合使用 Swing
和 AWT
功能。
并确保将 super.paintComponent() 作为第一个语句调用。要重新绘制面板,您需要做的就是调用 repaint()
.
您不应该直接调用绘画方法,因为绘画应该在事件调度线程 (EDT) 中完成。您也不需要保护图形上下文。
这里有一个非常简单的例子来演示运动。
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);
}
}
我想从 main()
调用 paint()
但我需要一个 parameter.I 不知道要传递哪个参数而且我似乎无法使用 Graphics
对象,当我在参数外定义 g
时,因为它无法初始化。
我尝试在 main()
中创建图形 class 的对象,然后将其作为参数传递,但是每当我尝试使用 g 时,它都会给我一个 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
方法。
您需要致电 repaint。来自文档:
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.
编辑
间接参数化示例:
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
}
我建议您使用 Swing
扩展 JPanel
并覆盖 paintComponent
。 Canvas
是 AWT
class,您不应混合使用 Swing
和 AWT
功能。
并确保将 super.paintComponent() 作为第一个语句调用。要重新绘制面板,您需要做的就是调用 repaint()
.
您不应该直接调用绘画方法,因为绘画应该在事件调度线程 (EDT) 中完成。您也不需要保护图形上下文。
这里有一个非常简单的例子来演示运动。
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);
}
}