gpt4 book ai didi

Java JFrame 绘制

转载 作者:行者123 更新时间:2023-12-01 18:33:25 28 4
gpt4 key购买 nike

我目前正在使用 JFrame 并尝试绘制一个矩形,但我不知道如何执行代码 paint(Graphics g),如何获取 Graphics 对象?

package com.raggaer.frame;

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;

public class Frame {

private JFrame frame;

public Frame() {

this.frame = new JFrame("Java Snake");
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setSize(new Dimension(500, 500));

// DRAW??

this.frame.setVisible(true);

}

public void paint(Graphics g) {

g.drawRect(10, 10, 200, 200);
}
}

最佳答案

只需调用frame.repaint()(应自动调用一次)即可重新绘制图形。无需提供您自己的 Graphics 对象。

旁注,您应该使用 JPanelpaintComponent(Graphics) 来代替。这将使事件处理变得更加容易,特别是对于像贪吃蛇这样的游戏。

<小时/>

这是 Stack Overflow 上的一个小代码示例:Java drawing on JPanel which on a JFrame

我自己用 Java 8 制作了一个:

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

/**
* @author Obicere
*/
public class PaintExample {

public PaintExample() {

final JFrame frame = new JFrame("Paint Example");
final MyPanel panel = new MyPanel();

frame.add(panel);

frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(final String[] args) {
SwingUtilities.invokeLater(PaintExample::new);
}


public class MyPanel extends JPanel {

@Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);

g.setColor(Color.YELLOW);
g.fillOval(0, 0, 50, 50);
g.setColor(Color.BLACK);
g.drawOval(0, 0, 50, 50);

g.drawLine(20, 10, 20, 20);
g.drawLine(30, 10, 30, 20);

g.drawArc(15, 15, 20, 20, 180, 180);


g.drawString("Drawing with swing!", 10, 100);
}

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

}
}

enter image description here

<小时/>

根据您的评论的请求,我还修改了程序以根据请求显示对象:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

/**
* @author Obicere
*/
public class PaintExample {


public PaintExample() {
final JFrame frame = new JFrame("Paint Example");
final MyPanel panel = new MyPanel();

frame.add(panel);

frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(final String[] args) {
SwingUtilities.invokeLater(PaintExample::new);
}


public class MyPanel extends JPanel {

private final LinkedList<SmileyFace> faces;

public MyPanel() {
faces = new LinkedList<>();
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
faces.add(new SmileyFace(e.getX(), e.getY()));
MyPanel.this.repaint(); // Refresh the display on the screen
}
});
}

@Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
faces.stream().forEach((e) -> e.render(g));
}

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

}

public class SmileyFace {

private final int x;
private final int y;

public SmileyFace(final int x, final int y) {
this.x = x;
this.y = y;
}

public void render(final Graphics g) {

g.setColor(Color.YELLOW);
g.fillOval(x, y, 50, 50);
g.setColor(Color.BLACK);
g.drawOval(x, y, 50, 50);

g.drawLine(x + 20, y + 10, x + 20, y + 20);
g.drawLine(x + 30, y + 10, x + 30, y + 20);

g.drawArc(x + 15, y + 15, 20, 20, 180, 180);
}

}

}

enter image description here

关于Java JFrame 绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23122492/

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