简体   繁体   English

用Java(不是OpenGL)绘制图元的最简单方法

[英]Simplest way to draw primitives in Java (not OpenGL)

Just started to getting familiar with graphics in Java. 刚开始熟悉Java中的图形。

What is the simplest way to draw primitives (lines, rectangles) in Java without OpenGL (JOGL)? 在没有OpenGL(JOGL)的Java中,最简单的绘制图元(线,矩形)的方法是什么?

Looking for methods like putPixel , lineTo , etc. 寻找诸如putPixellineTo等方法。

UPD. UPD。
Where to paint? 在哪里上漆? Canvas? 帆布? Panel? 面板? Bitmap? 位图?

The built in interface is called "Graphics2D". 内置接口称为“ Graphics2D”。

Here's a link to a Java Graphics2D Tutorial: http://java.sun.com/docs/books/tutorial/2d/index.html 这是Java Graphics2D教程的链接: http : //java.sun.com/docs/books/tutorial/2d/index.html

You can get a Graphics/Graphics2D instance from any AWT/Swing component via the paint method. 您可以通过paint方法从任何AWT / Swing组件获取Graphics / Graphics2D实例。 JPanel is probably best since it fits well with swing and is lightweight, meaning that only one native window is created - for the top level window. JPanel可能是最好的,因为它非常适合摆动并且重量轻,这意味着只为顶层窗口创建一个本机窗口。 Swing components can also be double-buffered, meaning that the painting is done to an offscreen buffer first, before being transferred to the screen. Swing组件也可以是双缓冲的,这意味着绘画要先转移到屏幕外的缓冲区中,然后再转移到屏幕上。 This gives a smoother appearance and avoids flickering that can happen when painting directly to the screen, and is particularly important for smooth animation. 这样可以使外观更平滑,并避免了直接在屏幕上绘画时可能发生的闪烁,这对于平滑动画尤为重要。

You can specifically draw to an offscreen buffer ("bitmap") that you can use afterwards, eg to draw an image for saving as a file later: 您可以专门绘制到屏幕外的缓冲区(“位图”)上,以供以后使用,例如绘制图像以便以后另存为文件:

   BufferedImage offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
   Grapics2D g2 = offImg.createGraphics();

    // .. optionally set attributes ..
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

The original Java user interface classes are called AWT. 原始的Java用户界面类称为AWT。 These were "heavyweight" components, that sometimes acted differently on different systems (Windows, Mac, Unix). 这些是“重量级”组件,有时在不同系统(Windows,Mac,Unix)上的行为不同。 These components were difficult to use to make a GUI. 这些组件很难用于制作GUI。

Sun developed Swing, which is a set of "lightweight" components that, to the maximum degree possible, work the same on different systems. Sun开发了Swing,它是一组“轻量级”组件,它们在最大程度上可以在不同的系统上运行。 These components made GUI development somewhat easier. 这些组件使GUI开发更加容易。

In order to have a canvas for graphics, you start with a javax.swing.JFrame. 为了拥有用于图形的画布,请从javax.swing.JFrame开始。 You add a child javax.swing.JPanel to the JFrame. 您将一个子javax.swing.JPanel添加到JFrame。 You draw on the JPanel by overriding the paint method. 您可以通过重写paint方法在JPanel上进行绘制。

The JPanel paint method takes a java.awt.Graphics as input. JPanel绘制方法将java.awt.Graphics用作输入。 You can cast Graphics to java.awt.Graphics2D. 您可以将Graphics转换为java.awt.Graphics2D。 The methods of Graphics2D allow you to draw rectangles, images, text, lines, and arbitrary polygons. Graphics2D的方法允许您绘制矩形,图像,文本,线和任意多边形。

You can find out more about Swing by reading Sun's Creating a GUI with JFC/Swing tutorial. 通过阅读Sun的“ 使用JFC / Swing创建GUI”教程,可以找到有关Swing的更多信息。 You can find out more about 2D Graphics by reading Sun's 2D Graphics tutorial. 通过阅读Sun的2D图形教程,可以找到有关2D图形的更多信息。 More details on the Java classes I've mentioned can be found in the Javadoc . 我所提到的Java类的更多详细信息可以在Javadoc中找到。

My recent question about horizontal scrolling in Java includes a tiny little graphics example source code that you could use as a base to work from. 我最近关于Java中水平滚动的问题包括一个很小的图形示例源代码,您可以将其用作工作的基础。 There are both AWT and Swing implementations. 有AWT和Swing实现。 The AWT doesn't support horizontal scrolling, so I'll be using swing. AWT不支持水平滚动,因此我将使用swing。

Not recommending these as best practice or anything, they were a quick demonstration of my particular issue, but it might be enough to get you started. 不建议您将它们作为最佳实践或任何建议,它们只是对我特定问题的快速演示,但可能足以让您入门。

Link is How to use my trackpad for horizontal mousewheel scrolling in a Java AWT ScrollPane 链接是如何使用触控板在Java AWT ScrollPane中进行水平鼠标滚轮滚动

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

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