JAVA Canvas画布不画的图形会自动消失,是什么原因,怎样让它不消失?
请各位高手指教.本来是想先画个棋盘,再下棋的.可运行时棋盘消失了.
源代码如下:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QiPan_r extends Frame implements MouseListener{
Canvas c;
public static void main(String args[]){
Frame f=new Frame("Canvas");
QiPan_r mc=new QiPan_r();
mc.c=new Canvas();
f.add("Center",mc.c);
f.setSize(500, 500);
mc.c.addMouseListener(mc);
f.setVisible(true);
//用来通过X按钮来关闭窗口
f.addWindowListener(new WindowLis());
}
public void paint(Graphics g){
c.setForeground(Color.BLUE);
//画围棋棋盘
for(int y=50;y<=450;y=y+20)
g.drawLine(50, y, 450, y);
for(int x=50;x<=450;x=x+20)
g.drawLine(x,50, x, 450);
//画三颗棋子
c.getGraphics().fillOval(100, 100, 50, 50);
c.getGraphics().fillOval(200, 200, 50, 50);
c.getGraphics().fillOval(300, 300, 50, 50);
}
public void mouseClicked(MouseEvent ev){
System.out.println("mouseClicked");
c.requestFocus();
c.setForeground(Color.red);
//鼠标点击后下棋
c.getGraphics().fillOval(ev.getX(), ev.getY(), 30, 30);
}
public void mouseEntered(MouseEvent ev){
}
public void mouseExited(MouseEvent ev){
}
public void mousePressed(MouseEvent ev){
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent ev){
System.out.println("mouseRelease");
}
public void update(Graphics g){
paint(g);
}
}
class WindowLis extends WindowAdapter
{
public void windowClosing(WindowEvent e){
//f.dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e){
}}
请各位高手指教.本来是想先画个棋盘,再下棋的.可运行时棋盘消失了.
源代码如下:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QiPan_r extends Frame implements MouseListener{
Canvas c;
public static void main(String args[]){
Frame f=new Frame("Canvas");
QiPan_r mc=new QiPan_r();
mc.c=new Canvas();
f.add("Center",mc.c);
f.setSize(500, 500);
mc.c.addMouseListener(mc);
f.setVisible(true);
//用来通过X按钮来关闭窗口
f.addWindowListener(new WindowLis());
}
public void paint(Graphics g){
c.setForeground(Color.BLUE);
//画围棋棋盘
for(int y=50;y<=450;y=y+20)
g.drawLine(50, y, 450, y);
for(int x=50;x<=450;x=x+20)
g.drawLine(x,50, x, 450);
//画三颗棋子
c.getGraphics().fillOval(100, 100, 50, 50);
c.getGraphics().fillOval(200, 200, 50, 50);
c.getGraphics().fillOval(300, 300, 50, 50);
}
public void mouseClicked(MouseEvent ev){
System.out.println("mouseClicked");
c.requestFocus();
c.setForeground(Color.red);
//鼠标点击后下棋
c.getGraphics().fillOval(ev.getX(), ev.getY(), 30, 30);
}
public void mouseEntered(MouseEvent ev){
}
public void mouseExited(MouseEvent ev){
}
public void mousePressed(MouseEvent ev){
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent ev){
System.out.println("mouseRelease");
}
public void update(Graphics g){
paint(g);
}
}
class WindowLis extends WindowAdapter
{
public void windowClosing(WindowEvent e){
//f.dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e){
}}
repaint()用在哪里呢?我的这个程序,用鼠标可以下棋,就是在paint()画过的棋盘看不到
....
private List<IShape> shapes = new ArrayList<IShape>();
...重写 JPANEL 中的
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//.....
for(IShape sp:shapes){
sp.draw();
}
//.....
}
每次刷新都要重画你现有的图形 包括棋盘线...还有一种方法是,你的panel杯具直接用一张棋谱图像做背景
你只要关注棋子就好了,这样效率还高,因为每次画整个棋盘,对CPU要求也很高的