我创建了一个时钟小程序,可以在用户拉伸窗口时调整文本大小。在Netbeans中它工作正常但在Web服务器上运行jar它没有检测到任何大小的变化。我最初在paint函数中使用getWidth()和getHeight()来获取当前窗口大小。当我意识到它没有检测到Netbeans之外的变化时,我切换/添加了一个componentResized()监听器,但是也没有检测到Netbeans之外的任何大小调整。
在web服务器index.html页面上,我使用win = window.open(url,name,the_size)打开一个带有时钟applet的窗口。当我伸展窗户时,为什么听众没有看到任何调整大小?是因为window.open()窗口不同于Netbean弹出窗口?
@Override
public void init() {
if (getParameter("verbose") != null)
{
try {
m_verboseLevel = Integer.parseInt(getParameter("verbose"));
} catch (NumberFormatException e) {
System.out.println(Thread.currentThread().getName() + " verbose: " + getParameter("verbose") + " is not 0->3");
m_verboseLevel = 0;
}
}
m_size = getParameter("size");
m_clock_color = getParameter("color");
// Add listener to update width and height as user changes it
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
m_w = getWidth();
m_h = getHeight();
System.out.println(m_w + " " + m_h);
}
});
try {
在绘画中,m_w和m_h最初为0,因为没有任何移动。我调用getWidth()和getHeight(),输出匹配html applet宽度和高度值600和800.但是在Netbeans m_h / m_w之外永远不会改变,所以缩放逻辑不会改变任何东西。
@Override
public void paint(Graphics g) {
// first time?
if ((m_w == 0) && (m_h == 0)) {
m_w = getWidth(); // return the width of the applet
m_h = getHeight(); // return the height of the applet
}
// Use buffering for smooth update
BufferedImage bi = new BufferedImage(m_w, m_h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
super.paint(g2d);
String[] lines = new String[2];
lines[0] = "Acquisition of Signal";
lines[1] = "Loss of Signal";
// first time?
if (m_clockLine == 0) {
FontRenderContext frc = g2d.getFontRenderContext();
LineMetrics metrics = m_clock_font.getLineMetrics(lines[1], frc);
// Omitt the descent from the height variable.
String time = "00:00:00";
g2d.setFont(m_clock_font);
float height1 = metrics.getAscent() + metrics.getDescent();
m_width = m_clock_font.getStringBounds(time, frc).getWidth();
m_clockLine = (int) (metrics.getAscent() - 1);
metrics = m_label_font.getLineMetrics(lines[0], frc);
float height2 = metrics.getAscent() + metrics.getDescent();
m_labelLine = (int) (metrics.getAscent() - 1);
metrics = m_tk_font.getLineMetrics(lines[1], frc);
float height3 = metrics.getAscent() + metrics.getDescent();
m_titleLine = (int) (metrics.getAscent() - 1);
m_width += 22*2;
m_height = height1 + height2 + height3 + height1 + height2 + (m_padding*4);
}
System.out.println(m_w + " " + m_width + " " + m_h + " " + m_height);
g2d.scale(m_w/m_width, m_h/m_height);
/* Make background black */
g2d.setColor(Color.black);
g2d.fillRect(0, 0, (int)m_width, (int)m_height);
/* Draw individual clock labels */
g2d.setFont(m_label_font);
g2d.setColor(m_main_color);
g2d.drawString(lines[0], 145, m_clockLine+m_labelLine+m_padding);
g2d.drawString(lines[1], 220, m_clockLine+m_labelLine+m_titleLine+m_clockLine+m_labelLine+(m_padding*4));
// Write check clock timer lines
if (m_c != null) {
m_c.print(g2d, m_labelLine, m_clockLine, m_titleLine, m_padding, m_width, m_height);
}
g2d.dispose();
g.drawImage(bi, 0, 0, this);
} // end paint
答案 0 :(得分:0)
好的,经过大量的谷歌搜索,我发现了问题所在。当applet使用addComponentListener componentResized或getWidth / getHeight调用来检查它的尺寸时,它正在读取applet / object的html宽度和高度值。使用Netbeans Applet Viewer时,这些值会随着查看器的拉伸而改变。但是在html页面中,随着浏览器调整大小,宽度和高度不会改变。这就是拉伸浏览器时applet大小不会改变的原因。因此,真实环境中的解决方案是通过javascript重置宽度和高度值,以便applet可以在onLoad上看到新的维度。注意:onResize似乎永远不会消失,但无论如何我都离开了它。
所以我删除了componentResized逻辑,在paint函数中只需为applet维度调用getWidth / getHeight。这是最终的绘画功能。
@Override
public void paint(Graphics g) {
// This makes the applet resizable
int w = getWidth(); // return the width of the applet
int h = getHeight(); // return the height of the applet
// Use buffering for smooth update
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
super.paint(g2d);
String[] lines = new String[2];
lines[0] = "Acquisition of Signal";
lines[1] = "Loss of Signal";
// first time?
if (m_clockLine == 0) {
FontRenderContext frc = g2d.getFontRenderContext();
LineMetrics metrics = m_clock_font.getLineMetrics(lines[1], frc);
// Omitt the descent from the height variable.
String time = "00:00:00";
g2d.setFont(m_clock_font);
float height1 = metrics.getAscent() + metrics.getDescent();
m_width = m_clock_font.getStringBounds(time, frc).getWidth();
m_clockLine = (int) (metrics.getAscent() - 1);
metrics = m_label_font.getLineMetrics(lines[0], frc);
float height2 = metrics.getAscent() + metrics.getDescent();
m_labelLine = (int) (metrics.getAscent() - 1);
metrics = m_tk_font.getLineMetrics(lines[1], frc);
float height3 = metrics.getAscent() + metrics.getDescent();
m_titleLine = (int) (metrics.getAscent() - 1);
m_width += 22*2;
m_height = height1 + height2 + height3 + height1 + height2 + (m_padding*4);
}
g2d.scale(w/m_width, h/m_height);
/* Make background black */
g2d.setColor(Color.black);
g2d.fillRect(0, 0, (int)m_width, (int)m_height);
/* Draw individual clock labels */
g2d.setFont(m_label_font);
g2d.setColor(m_main_color);
g2d.drawString(lines[0], 145, m_clockLine+m_labelLine+m_padding);
g2d.drawString(lines[1], 220, m_clockLine+m_labelLine+m_titleLine+m_clockLine+m_labelLine+(m_padding*4));
// Write check clock timer line
if (m_c != null) {
m_c.print(g2d, m_labelLine, m_clockLine, m_titleLine, m_padding, m_width, m_height);
}
g2d.dispose();
g.drawImage(bi, 0, 0, this);
} // end paint
在html脚本中,我添加了以下逻辑:
<html>
<head>
<TITLE>Terra Small Clock</TITLE>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<meta content="IE=5.0000" http-equiv="X-UA-Compatible">
<meta name="GENERATOR" content="MSHTML 11.00.9600.17842">
</head>
<SCRIPT LANGUAGE="JavaScript">
/*
Description:
The re-size function job is to reset the applet width and height to the new dimensions as the
user stretches the browser window. The clock applet will then use the "current" width and
height to scale the clock.
*/
function resize()
{
if (navigator.appName.indexOf("Microsoft") != -1)
{
// 100% works fine on object tag with IE
return;
}
var w_newWidth,w_newHeight;
var netscapeScrollWidth=0;
w_newWidth = window.innerWidth-netscapeScrollWidth;
w_newHeight = window.innerHeight-netscapeScrollWidth;
var appletDiv = document.getElementById('clockApp');
if (appletDiv != null) {
appletDiv.style.width = w_newWidth + "px";
appletDiv.style.height = w_newHeight + "px";
}
}
window.onResize = resize;
window.onLoad = resize;
</SCRIPT>
<body bgcolor="000000" leftmargin="0" rightmargin="0" topmargin="0" marginheight="0" marginwidth="0" onResize="resize()" onLoad="resize()">
<p style="text-align:center">
<object type="application/x-java-applet" id="clockApp" width="240" height="230">
<param name="codebase" value="https://<host stuff>.nasa.gov/java_clocks/classes/" />
<param name="code" value="aosClock.class" />
<param name="archive" value="clock.jar" />
<param name="verbose" value="0" />
<param name="color" value="green" />
<param name="input_file" value="https://<host stuff>.nasa.gov/java_clocks/terra/terra_aos_times" />
</object>
</p>
</body>
</html>
因此,当我停止拉伸浏览器窗口时,onLoad会关闭,并重置applet /对象的宽度和高度。然后小程序缩放新维度的时钟。