java双缓冲问题??已标记如下:
/**Tochangethistemplate,chooseTools|Templates*andopenthetemplateintheeditor.*/package...
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lj;
/**
*
* @author john
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ImitateDoubleBuffer extends JFrame implements ActionListener{
Image buffer;
boolean bool=true;JButton b1,b2;
JPanel p=new JPanel();
Dimension dim=getSize();
int w,h;
public static void main(String args[]){
new ImitateDoubleBuffer();
}
public ImitateDoubleBuffer(){
setSize(300,300);
b1=new JButton("双缓冲");
b2=new JButton("普通模式");
setLayout(new BorderLayout());
p.add(b1);p.add(b2);
add(p,"North");b1.addActionListener(this);
b2.addActionListener(this);
setVisible(true);
w=dim.width;h=dim.height;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
bool=true;
repaint();
}
if(e.getSource()==b2){
bool=false;
repaint();
}
}
public void update(Graphics g){
paint(g);
}
@Override
public void paint(Graphics g){
Graphics screen=null;
if(bool){
screen=g;
g=buffer.getGraphics();//为何显示此处空指向异常?
}
g.setColor(Color.blue);
for(int i=0;i<=w;i++){
g.drawLine(0,i,w,i);
}
for(int i=0;i<=h;i++){
g.setColor(Color.white);
g.drawLine(i,0,i,h);
}
screen.drawImage(buffer, 0, 0, this);
}
}
其实这个程序是不合理的,比如无法重复调用绘图,还有的swing线程调用也会出问题… 展开
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lj;
/**
*
* @author john
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ImitateDoubleBuffer extends JFrame implements ActionListener{
Image buffer;
boolean bool=true;JButton b1,b2;
JPanel p=new JPanel();
Dimension dim=getSize();
int w,h;
public static void main(String args[]){
new ImitateDoubleBuffer();
}
public ImitateDoubleBuffer(){
setSize(300,300);
b1=new JButton("双缓冲");
b2=new JButton("普通模式");
setLayout(new BorderLayout());
p.add(b1);p.add(b2);
add(p,"North");b1.addActionListener(this);
b2.addActionListener(this);
setVisible(true);
w=dim.width;h=dim.height;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
bool=true;
repaint();
}
if(e.getSource()==b2){
bool=false;
repaint();
}
}
public void update(Graphics g){
paint(g);
}
@Override
public void paint(Graphics g){
Graphics screen=null;
if(bool){
screen=g;
g=buffer.getGraphics();//为何显示此处空指向异常?
}
g.setColor(Color.blue);
for(int i=0;i<=w;i++){
g.drawLine(0,i,w,i);
}
for(int i=0;i<=h;i++){
g.setColor(Color.white);
g.drawLine(i,0,i,h);
}
screen.drawImage(buffer, 0, 0, this);
}
}
其实这个程序是不合理的,比如无法重复调用绘图,还有的swing线程调用也会出问题… 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询