简体   繁体   English

Java JPanel Graphics - 了解如何绘制简单形状

[英]Java JPanel Graphics - Understanding how to draw a simple shape

Disclaimer: I'm new to Java.免责声明:我是 Java 的新手。 I'm new to Swing.我是 Swing 的新手。 And I'm sure it shows.我确信它显示了。

I've viewed quite a number of examples/tutorials of how to draw on a jpanel "canvas".我已经查看了很多关于如何在 jpanel“画布”上绘图的示例/教程。 But they mostly have the same basic format and put all of their drawLine/drawRect/drawArc inside the paintComponent() method.但它们大多具有相同的基本格式,并将它们的所有 drawLine/drawRect/drawArc 放在 paintComponent() 方法中。 It seems assumed that people want to draw static things to the jpanel one time.似乎假设人们想一次将 static 的东西绘制到 jpanel 上。 But what if I want to change the jpanel object over the course of the program's runtime, like a paint program, or a game?但是,如果我想在程序的运行过程中更改 jpanel object,比如绘画程序或游戏,该怎么办?

I suppose I need to be able to access the jpanel object, and internal methods to paint.我想我需要能够访问 jpanel object 和内部绘制方法。 I'm betting what I'm doing isn't best practices, but here is what I have:我打赌我正在做的不是最佳实践,但这是我所拥有的:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PaintPanel extends JPanel {
    public static JFrame frame;
    private Graphics g = getGraphics();

    public static void main(String[] args) {
        frame = new JFrame();
        frame.getContentPane().setBackground(new Color(32, 32, 32));
        frame.setResizable(false);
        frame.setBounds(1, 1, 800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null); // center frame on screen
        
        PaintPanel paintPanel = new PaintPanel();
        paintPanel.setBounds(10, 10, 100, 100);
        paintPanel.setBackground(Color.red);
        frame.add(paintPanel);
    }
    
    // constructor
    public PaintPanel() {

    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
    
    public void DrawRect(Integer x, Integer y, Integer w, Integer h, Color color) {
        g.setColor(color);
        g.fillRect(x, y, w, h);
        this.repaint(); // doesn't seem to do anything
    }
}

This code results in a red panel box, but my user method DrawRect() doesn't draw anything.此代码生成一个红色面板框,但我的用户方法 DrawRect() 不绘制任何内容。

  1. I've read in some places that it's necessary to override the paintComponent() method.我在某些地方读到有必要重写 paintComponent() 方法。 If there's nothing in it, what's the purpose?如果里面什么都没有,目的是什么?
  2. How can I get my DrawRect() method to work?如何让我的 DrawRect() 方法工作?

The piece of the puzzle you're missing is the model object.您缺少的拼图是model object。 There should be an external object that describes what should be drawn.应该有一个外部 object 来描述应该绘制的内容。 In a game for example, it would be something that describes the current state of the game.例如,在游戏中,它将描述游戏当前的 state。

Your custom component looks at this model and takes the necessary steps to paint it.您的自定义组件查看此 model 并采取必要的步骤来绘制它。 This is implemented in paintComponent and in helper methods you see fit to add.这是在您认为适合添加的paintComponent 和辅助方法中实现的。

To make an animation, you make a loop that modifies the model over time, and asks the custom component to redraw itself with repaint().要制作 animation,您需要创建一个循环,随着时间的推移修改 model,并要求自定义组件使用 repaint() 重新绘制自身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM