Java JFrame 绘制 [英] Java JFrame draw

查看:45
本文介绍了Java JFrame 绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently working with JFrame and I'm trying to draw a rectangle but I don't know how to execute the code paint(Graphics g), how do I get the Graphics object?

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 对象.

Just call frame.repaint() (which should be called once automatically) to make it repaint the graphics. No need to provide your own Graphics object.

旁注,您应该使用 JPanelpaintComponent(Graphics) 代替.这将使事件处理变得更加容易,尤其是对于像蛇这样的游戏.

Side note, you should be using a JPanel with paintComponent(Graphics) instead. This will make handling of events a lot easier, especially for a game like snake.

这是 Stack Overflow 上的一个小代码示例:Java 绘图JFrame 上的 JPanel

Here is a small code example on Stack Overflow: Java drawing on JPanel which on a JFrame

还有一个我自己使用 Java 8 制作的:

And one I made myself with usage of 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);
        }

    }
}

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

As request of your comment, I also modified the program to display objects upon request:

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

    }

}

这篇关于Java JFrame 绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆