gpt4 book ai didi

java - 如何使用 Graphics2D 类绘制字符串?

转载 作者:行者123 更新时间:2023-12-01 22:14:57 25 4
gpt4 key购买 nike

您好,我正在开发一个简单的应用程序,用于从无人机获取视频反馈。所以这是我的问题。我不知道为什么我无法在面板上显示“无视频连接”字符串。知道这里发生了什么吗?当我运行这个框架时,它只向我显示一个没有任何内容的普通空框架。谢谢。

代码:

package dronetest;

import com.codeminders.ardrone.ARDrone;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import javax.swing.*;

public class testStringVideo extends javax.swing.JFrame {

private AtomicReference<BufferedImage> image = new AtomicReference<BufferedImage>();
private AtomicBoolean preserveAspect = new AtomicBoolean(true);
private BufferedImage noConnection = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
/**
* Creates new form testStringVideo
*/
public testStringVideo() {
initComponents();

Graphics2D g2d = (Graphics2D) noConnection.getGraphics();
Font f = g2d.getFont().deriveFont(24.0f);
System.err.println(f);

g2d.drawString("No video connection", 40, 100);
image.set(noConnection);
System.err.println(image);
}

public void setDrone(ARDrone drone) {
drone.addImageListener(this);
}

public void setPreserveAspect(boolean preserve) {
preserveAspect.set(preserve);
}

public void frameReceived(BufferedImage im) {
image.set(im);
repaint();
}

public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int width = getWidth();
int height = getHeight();
drawDroneImage(g2d, width, height);
}

private void drawDroneImage(Graphics2D g2d, int width, int height) {
BufferedImage im = image.get();
if (im == null) {
return;
}
int xPos = 0;
int yPos = 0;
if (preserveAspect.get()) {
g2d.setColor(Color.BLACK);
g2d.fill3DRect(0, 0, width, height, false);
float widthUnit = ((float) width / 4.0f);
float heightAspect = (float) height / widthUnit;
float heightUnit = ((float) height / 3.0f);
float widthAspect = (float) width / heightUnit;

if (widthAspect > 4) {
xPos = (int) (width - (heightUnit * 4)) / 2;
width = (int) (heightUnit * 4);
} else if (heightAspect > 3) {
yPos = (int) (height - (widthUnit * 3)) / 2;
height = (int) (widthUnit * 3);
}
}
if (im != null) {
g2d.drawImage(im, xPos, yPos, width, height, null);
}
}

public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(testStringVideo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(testStringVideo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(testStringVideo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(testStringVideo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new testStringVideo().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
}

最佳答案

一般来说,您不会直接调用getGraphics():

Graphics2D g2d = (Graphics2D) noConnection.getGraphics();

这为您提供了一个指向 Graphics 对象的指针,但不是在绘制窗口时提供的。

相反,您应该重写内部 JLabelpaintComponent() 方法,例如 this tutorial演示(注意其他人建议的 super.paintComponent() 调用)。

public void paintComponent(Graphics g) {
super.paintComponent(g);

// Draw Text
g.drawString("This is my custom Panel!",10,20);
}

paintComponent() 方法由 Swing 调用 Event Dispatch Thread当 Swing 准备好绘制该组件时,传递一个 Graphics 实例。

Oracle 网站(以及其他地方)上有许多有用的教程,其中包含更多详细信息:

请务必阅读这些内容; Swing 在幕后为您做了很多工作。它可以节省你很多时间,但如果你不明白它在做什么,你就会与它作斗争,而不是让它为你工作。

关于java - 如何使用 Graphics2D 类绘制字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31251286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com