java graphics 画线问题
packagemyframe;importjava.awt.Color;importjava.awt.Graphics;importjavax.swing.JFrame;...
package myframe;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Myframe extends JFrame{
public void MyPaint( ) {
Graphics g=this.getGraphics() ;
g.setColor(Color.blue);
g.drawLine(100, 100, 200, 200);
}
public static void main(String[] args) {
Myframe win=new Myframe();
win.setSize(400,500);
win.setLocation(200,200);
win.setVisible(true);
win.MyPaint() ;
}
} 展开
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Myframe extends JFrame{
public void MyPaint( ) {
Graphics g=this.getGraphics() ;
g.setColor(Color.blue);
g.drawLine(100, 100, 200, 200);
}
public static void main(String[] args) {
Myframe win=new Myframe();
win.setSize(400,500);
win.setLocation(200,200);
win.setVisible(true);
win.MyPaint() ;
}
} 展开
2个回答
展开全部
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Myframe extends JFrame {
public void paint(Graphics g) {
g.setColor(Color.blue);
g.drawLine(100, 100, 200, 200);
}
public static void main(String[] args) {
Myframe win = new Myframe();
win.setSize(400, 500);
win.setLocation(200, 200);
win.setVisible(true);
}
}
import java.awt.Graphics;
import javax.swing.JFrame;
public class Myframe extends JFrame {
public void paint(Graphics g) {
g.setColor(Color.blue);
g.drawLine(100, 100, 200, 200);
}
public static void main(String[] args) {
Myframe win = new Myframe();
win.setSize(400, 500);
win.setLocation(200, 200);
win.setVisible(true);
}
}
追问
我的程序也么有错,但是线闪一下就没有 了
追答
在调用构造方法后,第一次调用构造方法,绘制JFrame,画出直线;接着会自动调用paint(Graphics g)方法,重新绘制JFrame的内置面板,画上的直线被覆盖。出现直线一闪而过的情况。这也是绘图部分一般都写在paint(Graphics g)中的原因。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询