定义一个多线程JPanel类,在paint(Graphics g)方法里,
如果直接g.drawXXX()进行绘图没问题,
但是如果this.getGraphics().drawXXX()就会闪动
这是怎么回事丫。我想在此类中定义绘图方法绘制图形的各个部分,但每次都要传入“Graphics g”,才能正确绘图,而这样的方法无法被此类之外的方法调用,因为要传入“Graphics g”,而别的类并没有这个Graphics。想请问各位DX,能够使用this.getGraphics()来绘图,并且屏幕不闪
还有,paint里的Graphic g对象是哪里的,谢谢!代码如下:import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.applet.*;
import javax.swing.*;
import java.sql.*;
import java.awt.geom.*;public class Applet1 extends Applet {
private boolean isStandalone = false;
ClassLineGraphPanel LineGraphPanel = new ClassLineGraphPanel();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public Applet1() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(null);
this.add(LineGraphPanel);
LineGraphPanel.setBounds(new Rectangle(0, 0, 100, 100));
LineGraphPanel.start();
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}}class ClassLineGraphPanel extends JPanel implements Runnable
{
int i = 0; //counter
int sleepinterval = 100;//sleep interval. NOTE! keep it equal to sleepinterval@class ClassLineGraph
boolean runthread = true; //flag that control the running of the thread
Thread panelthread=null;
public void start()
{
if (panelthread==null)
{
panelthread=new Thread (this);
panelthread.start();
}
} public void stop()
{
panelthread.stop();
panelthread=null;
} public void run()
{
while(runthread)
{
repaint(); //clear the canvas and repaint
try{Thread.sleep(sleepinterval);} //sleep
catch(InterruptedException e){}
}
}
public void repaint()
{
super.repaint();//40, 0, 400, 300+45); //important! clear the canvas
}
public void paint(Graphics g)
{
super.paint(g);
i++;
this.getGraphics().drawLine(0,0,50,50);
//g.drawLine(0,0,50,50); }
}
如果直接g.drawXXX()进行绘图没问题,
但是如果this.getGraphics().drawXXX()就会闪动
这是怎么回事丫。我想在此类中定义绘图方法绘制图形的各个部分,但每次都要传入“Graphics g”,才能正确绘图,而这样的方法无法被此类之外的方法调用,因为要传入“Graphics g”,而别的类并没有这个Graphics。想请问各位DX,能够使用this.getGraphics()来绘图,并且屏幕不闪
还有,paint里的Graphic g对象是哪里的,谢谢!代码如下:import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.applet.*;
import javax.swing.*;
import java.sql.*;
import java.awt.geom.*;public class Applet1 extends Applet {
private boolean isStandalone = false;
ClassLineGraphPanel LineGraphPanel = new ClassLineGraphPanel();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public Applet1() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(null);
this.add(LineGraphPanel);
LineGraphPanel.setBounds(new Rectangle(0, 0, 100, 100));
LineGraphPanel.start();
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}}class ClassLineGraphPanel extends JPanel implements Runnable
{
int i = 0; //counter
int sleepinterval = 100;//sleep interval. NOTE! keep it equal to sleepinterval@class ClassLineGraph
boolean runthread = true; //flag that control the running of the thread
Thread panelthread=null;
public void start()
{
if (panelthread==null)
{
panelthread=new Thread (this);
panelthread.start();
}
} public void stop()
{
panelthread.stop();
panelthread=null;
} public void run()
{
while(runthread)
{
repaint(); //clear the canvas and repaint
try{Thread.sleep(sleepinterval);} //sleep
catch(InterruptedException e){}
}
}
public void repaint()
{
super.repaint();//40, 0, 400, 300+45); //important! clear the canvas
}
public void paint(Graphics g)
{
super.paint(g);
i++;
this.getGraphics().drawLine(0,0,50,50);
//g.drawLine(0,0,50,50); }
}
不闪丫,只是把g换成this.getGraphics().就闪了。
但你画的东西一样要用双缓冲才可以