简单的 Java 2D 图形:绘制一个矩形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21964768/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:42:08  来源:igfitidea点击:

Simple Java 2D graphics: draw a rectangle?

javaswing

提问by McGarnagle

I'm trying to get a Java 2D graphics "hello world" going, and am finding it strangely difficult (ie, I'm Googling variations of "java hello world example" and coming up empty). Can anyone help me with a minimal hellow world example?

我试图让 Java 2D 图形“hello world”运行起来,但我发现它出奇地困难(即,我在谷歌上搜索“java hello world example”的变体,结果却是空的)。任何人都可以用一个最小的地狱世界示例来帮助我吗?

Edit

编辑

This is a good starting point though, "The Java Tutorials: Performing Custom Painting".

不过,这是一个很好的起点,“Java 教程:执行自定义绘画”

采纳答案by Hovercraft Full Of Eels

To draw a rectangle in Swing you should:

要在 Swing 中绘制矩形,您应该:

  • First of all, never draw directly in the JFrame or other top-level window.
  • Instead draw in a JPanel, JComponent or other class that eventually extends from JComponent.
  • You should override the paintComponent(Graphics g)method.
  • You should be sure to call the super method
  • You should draw your rectangle with the Graphics object provided to the method by the JVM.
  • You should read the painting in Swing tutorial.
  • 首先,永远不要直接在 JFrame 或其他顶级窗口中绘制。
  • 而是绘制 JPanel、JComponent 或其他最终从 JComponent 扩展的类。
  • 您应该覆盖该paintComponent(Graphics g)方法。
  • 一定要调用super方法
  • 您应该使用 JVM 提供给该方法的 Graphics 对象绘制矩形。
  • 您应该阅读 Swing 教程中的绘画。

Clear?

清除?

e.g.,

例如,

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

public class DrawRect extends JPanel {
   private static final int RECT_X = 20;
   private static final int RECT_Y = RECT_X;
   private static final int RECT_WIDTH = 100;
   private static final int RECT_HEIGHT = RECT_WIDTH;

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      // draw the rectangle here
      g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT);
   }

   @Override
   public Dimension getPreferredSize() {
      // so that our GUI is big enough
      return new Dimension(RECT_WIDTH + 2 * RECT_X, RECT_HEIGHT + 2 * RECT_Y);
   }

   // create the GUI explicitly on the Swing event thread
   private static void createAndShowGui() {
      DrawRect mainPanel = new DrawRect();

      JFrame frame = new JFrame("DrawRect");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

回答by ArtOfWarfare

You need to create a class that extends from JComponent(or one of its subclasses, like JPanelin the below example) and overrides paintComponent(Graphics g). Here's an example:

您需要创建一个从JComponent(或其子类JPanel之一,如下例所示)扩展并覆盖的类paintComponent(Graphics g)。下面是一个例子:

class MyPanel extends JPanel {

    private int squareX = 50;
    private int squareY = 50;
    private int squareW = 20;
    private int squareH = 20;

    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // do your superclass's painting routine first, and then paint on top of it.
        g.setColor(Color.RED);
        g.fillRect(squareX,squareY,squareW,squareH);
    }
}

回答by Benjamin

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g);
    g2.setColor(Color.red);
    g2.drawRect(10, 10, 100, 100);
}

enter image description here

在此处输入图片说明