*我正在尝试打印一些东西,但背景为粉红色和白色文字。但文本颜色确实发生了变化,但屏幕总是呈黑色。 *
import java.awt.*;
import javax.swing.JFrame;
public class Screen {
private GraphicsDevice vc;
public Screen(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
vc.setFullScreenWindow(window);
if(dm!=null && vc.isDisplayChangeSupported()){
try{
vc.setDisplayMode(dm);
}
catch(Exception ex){
}
}
}
public void restoreScreen(){
Window w=vc.getFullScreenWindow();
if(w!=null ){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
现在的主要方法:
import javax.swing.JFrame;
import java.awt.*;
public class myGuiMain extends JFrame {
public static void main(String arg[]){
// Screen.java
DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
myGuiMain m = new myGuiMain();
m.run(dm);
}
public void run(DisplayMode dm){
setForeground(Color.WHITE);
setBackground(Color.PINK);
setFont(new Font("Arial",Font.PLAIN,25));
Screen s = new Screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}
catch(Exception ex){
}
}finally{
s.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("This is gonna be awesome",200,200);
}
}
结果:
我一直在互联网上搜索但最终没有任何结果。如果有人可以解决此问题,请提前致谢。
答案 0 :(得分:2)
问题#1:从JFrame
延伸。通常这是不鼓励的,因为它将你锁定到一个用例(你不能真正将框架添加到其他任何东西),此外,你并没有真正向该类添加任何新功能。最好只在需要时创建一个实例
问题#2:覆盖顶级容器paint
(JFrame
)。出于多种原因,通常不鼓励这样做:打破油漆链(你已经完成)很容易;它不是双缓冲的(对闪烁的更新问好);可以绘制子组件,而不需要重新绘制父容器(你好奇怪的油漆更新和artificates)。
最好创建一个自定义类,从JPanel
扩展并覆盖它的paintComponent
方法并在那里执行自定义绘制(并在进行任何自定义绘制之前调用super.paintComponent
)< / p>
有关详细信息,请参阅Painting in AWT and Swing和Performing Custom Painting
像...一样的东西。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class myGuiMain {
public static void main(String arg[]) {
new myGuiMain();
}
public myGuiMain() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
try {
Screen.setFullScreen(dm, frame);
try {
Thread.sleep(5000);
} catch (Exception ex) {
}
} finally {
Screen.restoreScreen();
}
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setForeground(Color.WHITE);
setBackground(Color.PINK);
setFont(new Font("Arial", Font.PLAIN, 25));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(getForeground());
g2d.drawString("This is gonna be awesome", 200, 200);
g2d.dispose();
}
}
public static class Screen {
public static void setFullScreen(DisplayMode dm, JFrame window) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice vc = env.getDefaultScreenDevice();
window.setUndecorated(true);
window.setResizable(false);
vc.setFullScreenWindow(window);
if (dm != null && vc.isDisplayChangeSupported()) {
try {
vc.setDisplayMode(dm);
} catch (Exception ex) {
}
}
}
public static void restoreScreen() {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice vc = env.getDefaultScreenDevice();
Window w = vc.getFullScreenWindow();
if (w != null) {
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
}
您可能还想查看2D Graphics