Java JFrame绘制 [英] Java JFrame draw

查看:175
本文介绍了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);
    }
}


推荐答案

Just调用 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.

注意,您应该使用 JPanel 替换为 paintComponent(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.

这是一个小代码示例堆栈溢出:在JFrame上的JPanel上的Java绘图

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天全站免登陆