gpt4 book ai didi

java - 图形 - 如何使用方法create(int x, int y, int width, int height)和translate(int x, int y)?

转载 作者:行者123 更新时间:2023-12-01 14:58:01 26 4
gpt4 key购买 nike

我试图做我的计算机科学作业,但当我尝试使用以下方法时我陷入困境。

  1. 公共(public)图形创建(int x,int y,int width,int height)

    基于此 Graphics 对象创建一个新的 Graphics 对象,但具有新的平移和剪辑区域。

    参数:

    • x - x 坐标。
    • y - y 坐标。
    • width - 剪切矩形的宽度。
    • height - 剪切矩形的高度。
  2. 公共(public)抽象无效翻译(int x,int y)

    将图形上下文的原点平移到当前坐标系中的点 (x, y)。

谁能解释并举例说明如何使用它们?

我正在尝试这样做..

public Graphics drawPlayer1()
{
myPencil.up();
myPencil.move(-620,300);
myPencil.down();
myPencil.fillCircle(20);
myPencil.up();
myPencil.move(-590,300);
myPencil.drawString("Player1: " + player1);
p1.create(-620,300,40,40);
return p1;
}//end drawPlayer1

当涉及到 p1.create(-620,300,40,40); 时,它向我抛出了 nullPointerException;

最佳答案

我同意 Andrew 的观点,我从未使用过 Graphics#create(int, int, int, int)。不过我确实使用Graphics#create

基本上,创建方法将创建一个新的图形上下文,它是原始图形上下文的副本。这使您可以在不影响原始文件的情况下操作副本。如果您正在对无法(轻松)撤消的图形执行操作,这一点非常重要。

将图形上下文简单“清零”到新位置。 Swing 绘制过程对其绘制的每个组件执行此操作。基本上,在调用 paint 之前,图形上下文被转换到组件位置,这意味着组件内的所有绘制都是从 0x0

开始的

enter image description here

public class TestGraphics01 {

public static void main(String[] args) {
new TestGraphics01();
}

public TestGraphics01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestGraphicsPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestGraphicsPane extends JPanel {

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
FontMetrics fm = g.getFontMetrics();

// This creates a "copy" the graphics context, it's translated
// to the x, y position within the current graphics context
// and has a width and height. If the width or height is outside
// the current graphics context, then it is truncated...
// It's kind of like clip, except what ever you do to this copy
// does not effect the graphics context it came from...
// This would be simular to setting the clipping region, just it
// won't effect the parent Graphics context it was copied from...
Graphics create = g.create(100, 100, 200, 200);
create.setColor(Color.GREEN);
create.fillRect(0, 0, 200, 200);
create.setColor(Color.YELLOW);
create.drawString("I'm inside...", 0, fm.getAscent());
create.dispose();

// But I remain uneffected...
g.drawString("I'm outside...", 0, fm.getAscent());

// I will effect every thing draw afterwards...
g.setColor(Color.RED);
int y = 50 - (fm.getHeight() / 2) + fm.getAscent();
g.translate(50, y);
g.drawString("I'm half way", 0, 0);
// You must reset the translation if you want to reuse the graphics OR
// you didn't create a copy...
g.translate(-50, -y);

y = 350 - (fm.getHeight() / 2) + fm.getAscent();
g.translate(300, y);
g.drawString("I'm half way", 0, 0);
// You must reset the translation if you want to reuse the graphics OR
// you didn't create a copy...
g.translate(-300, -y);

}

}

}

关于java - 图形 - 如何使用方法create(int x, int y, int width, int height)和translate(int x, int y)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com