Java 如何使用Graphics2D类绘制字符串?
问候,我正在开发一个简单的应用程序,从无人机获取视频反馈。这就是我的问题。我不知道为什么我不能向我的面板显示字符串“无视频连接”。知道这里发生了什么吗?当我运行这个框架时,它只显示一个没有任何内容的纯空框架。谢谢 代码:Java 如何使用Graphics2D类绘制字符串?,java,swing,paintcomponent,Java,Swing,Paintcomponent,问候,我正在开发一个简单的应用程序,从无人机获取视频反馈。这就是我的问题。我不知道为什么我不能向我的面板显示字符串“无视频连接”。知道这里发生了什么吗?当我运行这个框架时,它只显示一个没有任何内容的纯空框架。谢谢 代码: package dronetest; import com.codeminders.ardrone.ARDrone; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import j
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
}
包装测试;
导入com.codeminders.ardrone.ardrone;
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.RenderingHints;
导入java.awt.image.buffereImage;
导入java.util.concurrent.AtomicBoolean;
导入java.util.concurrent.AtomicReference;
导入javax.swing.*;
公共类testStringVideo扩展了javax.swing.JFrame{
私有AtomicReference image=新的AtomicReference();
private AtomicBoolean PreserveApect=新的AtomicBoolean(true);
private BuffereImage noConnection=新的BuffereImage(500500,BuffereImage.TYPE_INT_RGB);
/**
*创建新表单testStringVideo
*/
公共testStringVideo(){
初始化组件();
Graphics2D g2d=(Graphics2D)noConnection.getGraphics();
Font f=g2d.getFont().deriveFont(24.0f);
系统错误println(f);
g2d.抽绳(“无视频连接”,40100);
image.set(无连接);
系统错误打印项次(图像);
}
公共空间无人驾驶飞机(ARDrone无人驾驶飞机){
drone.addImageListener(这个);
}
公共void setPreserveApect(布尔保留){
preserveSpect.set(preserve);
}
已接收公共无效帧(BuffereImage im){
图像集(im);
重新油漆();
}
公共组件(图形g){
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(renderingHits.KEY\u TEXT\u ANTIALIAS,renderingHits.VALUE\u TEXT\u ANTIALIAS\u ON);
g2d.setRenderingHint(renderingHits.KEY\u抗锯齿,renderingHits.VALUE\u抗锯齿\u开启);
int width=getWidth();
int height=getHeight();
绘图图像(g2d、宽度、高度);
}
私有图像(Graphics2D g2d、整型宽度、整型高度){
BuffereImage im=image.get();
if(im==null){
返回;
}
int xPos=0;
int-yPos=0;
if(preserveApect.get()){
g2d.setColor(Color.BLACK);
g2d.fill3DRect(0,0,宽度,高度,false);
浮动宽度单位=((浮动)宽度/4.0f);
浮动高度方面=(浮动)高度/宽度单位;
浮动高度单位=((浮动)高度/3.0f);
浮动宽度纵横比=(浮动)宽度/高度单位;
如果(宽度方向>4){
xPos=(int)(宽度-(高度单位*4))/2;
宽度=(整数)(高度单位*4);
}否则如果(高度方面>3){
yPos=(int)(高度-(宽度单位*3))/2;
高度=(整数)(宽度单位*3);
}
}
如果(im!=null){
g2d.drawImage(im、xPos、yPos、宽度、高度、空);
}
}
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(testStringVideo.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(testStringVideo.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(testStringVideo.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(testStringVideo.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新建testStringVideo().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JPanel jPanel1;
//变量结束声明
}
我不熟悉Java 2D,但另一种方法是创建JTextArea
并将其添加到testStringVideo
。如果您对如何操作感兴趣,以下是一些一般说明:
JTextArea connectionMessage代码>
connectionMessage=newjtextarea(“无视频连接”)代码>
connectionMessage
添加到testStringVideo
让我知道这是否有效,或者你是否仍然需要帮助 我不熟悉Java 2D,但另一种方法是创建
JTextArea
并将其添加到testStringVideo
。如果您对如何操作感兴趣,以下是一些一般说明:
JTextArea connectionMessage代码>
connectionMessage=newjtextarea(“无视频连接”)代码>
connectionMessage
添加到testStringVideo
Graphics2D g2d = (Graphics2D) noConnection.getGraphics();
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw Text
g.drawString("This is my custom Panel!",10,20);
}