java 在 JFrame 中绘制 Graphics2D
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6260436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Painting Graphics2D in a JFrame
提问by Will
I'm making a 2d vertical shooter game, in which everything is coded (and working) but the graphics. I have not used the Graphics classes before, so this is all new to me. The following is the code I use to paint everything to the JFrame:
我正在制作一个 2d 垂直射击游戏,其中除了图形之外的所有内容都已编码(和工作)。我以前没有使用过 Graphics 类,所以这对我来说是全新的。以下是我用来将所有内容绘制到 JFrame 的代码:
public void paintAll()
{
Graphics h = new Graphics2D();
for(Bullet j : GameState.getEnBullets()){
h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
for(Enemy j : GameState.getEnemies()){
h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
for(Bullet j : GameState.getPlayBullets()){
h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
this.paint(h);
}
The first line "Graphics h = new Graphics2D();" produces an error because Graphics2d is abstract, but I have no idea where to go from here.
第一行“Graphics h = new Graphics2D();” 产生错误,因为 Graphics2d 是抽象的,但我不知道从哪里开始。
I need the code to take all the images that I have and paint them to the points in the JFrame. I remind you that I have never done this before, so please tell me if this is the wrong way to do this.
我需要代码来获取我拥有的所有图像并将它们绘制到 JFrame 中的点。我提醒您,我以前从未这样做过,所以请告诉我这是否是错误的做法。
回答by trashgod
Override paintComponent()
instead; it will supply the Graphics
context. You can castit to a Graphics2D
.
改写paintComponent()
;它将提供Graphics
上下文。您可以将其转换为Graphics2D
.
Graphics2D g2d = (Graphics2D) g;
Addendum: This assumes that you are overriding paintComponent()
in a JComponent
, which is then added to the JFrame
.
附录:这假设您paintComponent()
在 a中覆盖,JComponent
然后将其添加到JFrame
.
回答by mKorbel
in connections with Will's second thread (my helicopter view) about same thing Error with timer and JFrame
与 Will 的第二个线程(我的直升机视图)有关相同的事情Error with timer and JFrame
and correct intuition by Andrew Thompson's magics globe then
和安德鲁汤普森的魔法地球仪的正确直觉然后
I added (I hope that's correctly, because I'm not familair with paint, paintComponent or paintComponents together with custom Graphics)
我添加了(我希望这是正确的,因为我不熟悉paint、paintComponent 或paintComponents 以及自定义图形)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MinimumSize extends JFrame {
private static final long serialVersionUID = 1L;
public MinimumSize() {
setTitle("Custom Component Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void display() {
add(new CustomComponent());
pack();
setMinimumSize(getSize());// enforces the minimum size of both frame and component
setVisible(true);
}
public static void main(String[] args) {
MinimumSize main = new MinimumSize();
main.display();
}
}
class CustomComponent extends JComponent {
private static final long serialVersionUID = 1L;
@Override
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
@Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
}