import java.awt.*;import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.image.MemoryImageSource;class H1 extends JFrame implements ActionListener
{
private int a;
private static final long serialVersionUID = -7318514688915584864L;
public H1()
{
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(200,50,700,600);
Container frameContainer = getContentPane();
setLayout(null);
setVisible(true);
}
public void setA(int a)
{
this.a = a;
}
public int getA()
{
return this.a;
} public void actionPerformed(ActionEvent e)
{
//输入响应ActionEvent操作
}
public void paint(Graphics g)
{
// super.paint(g);
int w = 300;
int h = 300;
int pix[] = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
int red = (y * 255) / (h - 1);
for (int x = 0; x < w; x++) {
int blue = (x * 255) / (w - 1);
pix[index++] = (255 << 24) | (red << 16) | blue;
}
}
Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));
System.out.println(img.getClass()); //为什么多次输出。按理说应该执行一次。
g.drawImage(img, w, h, this);
}
public static void main(String args[])
{
H1 de = new H1();
}
}
import java.io.*;
import java.awt.event.*;
import java.awt.image.MemoryImageSource;class H1 extends JFrame implements ActionListener
{
private int a;
private static final long serialVersionUID = -7318514688915584864L;
public H1()
{
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(200,50,700,600);
Container frameContainer = getContentPane();
setLayout(null);
setVisible(true);
}
public void setA(int a)
{
this.a = a;
}
public int getA()
{
return this.a;
} public void actionPerformed(ActionEvent e)
{
//输入响应ActionEvent操作
}
public void paint(Graphics g)
{
// super.paint(g);
int w = 300;
int h = 300;
int pix[] = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
int red = (y * 255) / (h - 1);
for (int x = 0; x < w; x++) {
int blue = (x * 255) / (w - 1);
pix[index++] = (255 << 24) | (red << 16) | blue;
}
}
Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));
System.out.println(img.getClass()); //为什么多次输出。按理说应该执行一次。
g.drawImage(img, w, h, this);
}
public static void main(String args[])
{
H1 de = new H1();
}
}
解决方案 »
- 如何改变一个jar文件里面的某个文件的内容
- 大家能举一个形象的例子,说一下collection接口下的list,set,iterator作用?
- 在Frame中添加了一个TextField,把它设置成了不可编辑模式,但是把鼠标点上去还是会有插入光标在那删,插入光标如何隐藏呢?
- 怎么在别的CLASS拿到MAIN CLASS数据
- 请教logger.info与System.out.print的区别?它们都分别应用在什么情况下?
- 高分谢国庆答疑
- Java
- 为什么不能在JTextPane中实现水平滚动条,滚动文字.
- 为什么程序运行到一定程度总是没响应
- 怎样在页面刷新后保持表单中的文本框、选择框的内容同刷新前一样?
- try-catch-finally
- 图形界面的java 两个Label显示,如何上下换行表示
答:paint(Graphics g)方法的执行,是由JAVA的GUI系统内部调用的,而且这个调用是:JAVA内部系统认为需要时,可自动调用多次(如:窗口大小改变,窗口切换,以及其它许多种JAVA内部系统认为需要调用的情况....)