paint是系统回调函数,用户不能主动调用,需要进行重绘时,要使用repaint方法,当用户调用repaint方法后,实际上,系统后台调用了paint方法,所以,把的绘制代码或清除代码要写到paint方法中,然后需要绘制或清除的时候,调用repaint方法,就可以了
哦,那就是加载图片,举个例子:
import java.awt.image.BufferedImage
import java.io.File
import java.io.IOException
import javax.imageio.ImageIO
public class ImageInfo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String imgPath = "D:\\abc.bmp"
File imgFile = new File(imgPath)
BufferedImage bimg = ImageIO.read(imgFile)
int w = bimg.getWidth()
int h = bimg.getHeight()
for(int x=0x<wx++){
for(int y=0y<hy++){
int rgb = bimg.getRGB(x, y)
//...
}
}
}
}
java在JFrame上画东西,主要是使用paint方法,代码如下:
import java.awt.Colorimport java.awt.Graphics
import javax.swing.JFrame
import javax.swing.JPanel
public class Draw extends JFrame{
JPanel jPanel=new JPanel()
public Draw() {
jPanel.setBackground(Color.red)
add(jPanel)
Drawation drawaction=new Drawation()//添加画图,把上面jpanel的设置给覆盖了;要是先添加画图再添加
add(drawaction) //jpanel则把画图覆盖了
}
public static void main(String[] args){
Draw draw=new Draw()
draw.setTitle("abc")
draw.setSize(300,300)
draw.setVisible(true)
draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
}
class Drawation extends JPanel{
public void paintComponent(Graphics g){
super.paintComponents(g)
g.drawString("agagh", 50, 45)
}
}
运行结果如下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)