我先把代码帖出来 已经把不必要和这个问题没有关心的地方全部删除了已经是最方便大家看的了 想要解决的问题是 在下一个棋子的时候 屏幕都又重新刷新一遍 怎么样才能避免这样的问题发生?我期望的结果是 在下一个棋子的时候 棋盘和其他棋子不刷新(棋子不刷新已经解决了)只是屏幕上多了一个棋子 研究了2天 没解决 快疯了都还有就是现在棋盘把棋子给覆盖了 代码如下:主frame的代码package src.com.http.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;public class FristMain extends JFrame implements MouseListener{ private Font font = new Font("ËÎÌå", 0, 12);
private Dimension dm ;
private JPanel panelMain =(JPanel)this.getContentPane();
private JPanel panel = new JPanel(new FlowLayout());
private final int Width = 800;
private final int Height = 770;
private final int cellwidth = 46;
private final int cellheigth = 44; private boolean flagExcte = true;
private Borad borad = new Borad(Color.PINK,48 ,26 ,690 ,660 );
public FristMain() {
// TODO Auto-generated constructor stub
this.setTitle("FristMain");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setFont(font);
dm = this.getToolkit().getScreenSize();
this.setBounds((int)((dm.getWidth() - Width)/2), (int)((dm.getHeight() - Height)/2) , Width, Height); panelMain.setLayout( new BorderLayout());
panelMain.setBackground(Color.WHITE);
panel.add(borad);
panel.addMouseListener(this); panelMain.add(panel);
this.setResizable(false);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new FristMain();
}
public void update(Graphics g){
this.paint(g);
} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent arg0) {
int mapx =0;
int mapy =0;
mapx = (arg0.getX()+48)%cellwidth;
if(mapx != 0 && mapx <= (cellwidth /2)){
mapx = (arg0.getX()+48)/cellwidth * cellwidth;
}else if ((cellwidth /2) < mapx && mapx !=cellwidth){
mapx = ((arg0.getX()+48)/cellwidth+1) * cellwidth;
}else{
mapx = (arg0.getX()+48);
}
mapy = (arg0.getY()+26)%cellheigth;
if(mapy != 0 && mapy <= (cellheigth /2)){
mapy = (arg0.getY()+26)/cellheigth * cellheigth;
}else if ((cellheigth /2) < mapy && mapy !=cellheigth){
mapy = ((arg0.getY()+26)/cellheigth+1) * cellheigth;
}else{
mapy = (arg0.getY()+26);
}
Chess chess = new Chess();
if(flagExcte){
chess.setColor(Color.PINK);
flagExcte = false;
}else{
chess.setColor(Color.GRAY);
flagExcte = true;
}
chess.setX(mapx-42);
chess.setY(mapy-18);
chess.setVisible(true);
panel.invalidate();
panel.add(chess);
panel.validate();
this.repaint();
}
public void paintComponent(Graphics g){
super.paintComponents(g);
}
}
棋盘的代码:package src.com.http.swing;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.HashMap;
import javax.swing.*;import java.awt.event.*;
public class Borad extends JPanel{ private Color color ;
private final int cellwidth = 46;
private final int cellheigth = 44;
private int width;
private int height;
private int x;
private int y;
public Borad (Color color , int x ,int y, int width , int height ){
this.color = color;
this.width = width ;
this.height = height ;
this.x = x ;
this.y = y ;
this.setLayout(null);
} public void paintComponent(Graphics g){
this.setBounds(x, y, width+1, height+1);
g.setColor(color);
for(int i = 0 ; i < 15*cellwidth ; ){
for(int j = 0 ; j < 15*cellheigth ; ){
g.drawRect(i,j,cellwidth,cellheigth);
j = j + cellheigth;
}
i = i + cellwidth;
}
}
public void update(Graphics g){
paintComponent(g);
}
} 棋子的代码package src.com.http.swing;import java.awt.*;
import java.util.HashMap;import javax.swing.*;
public class Chess extends JComponent{
private Color color ;
private int x;
private int y;
private boolean chessState ;
public Chess(){
}
public Chess(Color color , int x , int y ){
this.color = color;
this.x = x-20;
this.y = y-20;
}
public Chess( Color color){
this.color = color;
}
public void paintComponent(Graphics g){
this.setBounds(x, y, 40, 40);
g.setColor(color);
g.fillOval(0, 0, 40, 40);
}
public void setStater(boolean chessState){
this.chessState = chessState;
}
public boolean getState(){
return this.chessState;
}
public void setColor(Color color){
this.color = color;
}
public Color getColor(){
return this.color;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public void setX( int x){
this.x= x-20;
}
public void setY( int y){
this.y= y-20;
}
}这些代码复制了以后就可以运行 大家复制了后帮我看看怎么解决。
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;public class FristMain extends JFrame implements MouseListener{ private Font font = new Font("ËÎÌå", 0, 12);
private Dimension dm ;
private JPanel panelMain =(JPanel)this.getContentPane();
private JPanel panel = new JPanel(new FlowLayout());
private final int Width = 800;
private final int Height = 770;
private final int cellwidth = 46;
private final int cellheigth = 44; private boolean flagExcte = true;
private Borad borad = new Borad(Color.PINK,48 ,26 ,690 ,660 );
public FristMain() {
// TODO Auto-generated constructor stub
this.setTitle("FristMain");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setFont(font);
dm = this.getToolkit().getScreenSize();
this.setBounds((int)((dm.getWidth() - Width)/2), (int)((dm.getHeight() - Height)/2) , Width, Height); panelMain.setLayout( new BorderLayout());
panelMain.setBackground(Color.WHITE);
panel.add(borad);
panel.addMouseListener(this); panelMain.add(panel);
this.setResizable(false);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new FristMain();
}
public void update(Graphics g){
this.paint(g);
} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent arg0) {
int mapx =0;
int mapy =0;
mapx = (arg0.getX()+48)%cellwidth;
if(mapx != 0 && mapx <= (cellwidth /2)){
mapx = (arg0.getX()+48)/cellwidth * cellwidth;
}else if ((cellwidth /2) < mapx && mapx !=cellwidth){
mapx = ((arg0.getX()+48)/cellwidth+1) * cellwidth;
}else{
mapx = (arg0.getX()+48);
}
mapy = (arg0.getY()+26)%cellheigth;
if(mapy != 0 && mapy <= (cellheigth /2)){
mapy = (arg0.getY()+26)/cellheigth * cellheigth;
}else if ((cellheigth /2) < mapy && mapy !=cellheigth){
mapy = ((arg0.getY()+26)/cellheigth+1) * cellheigth;
}else{
mapy = (arg0.getY()+26);
}
Chess chess = new Chess();
if(flagExcte){
chess.setColor(Color.PINK);
flagExcte = false;
}else{
chess.setColor(Color.GRAY);
flagExcte = true;
}
chess.setX(mapx-42);
chess.setY(mapy-18);
chess.setVisible(true);
panel.invalidate();
panel.add(chess);
panel.validate();
this.repaint();
}
public void paintComponent(Graphics g){
super.paintComponents(g);
}
}
棋盘的代码:package src.com.http.swing;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.HashMap;
import javax.swing.*;import java.awt.event.*;
public class Borad extends JPanel{ private Color color ;
private final int cellwidth = 46;
private final int cellheigth = 44;
private int width;
private int height;
private int x;
private int y;
public Borad (Color color , int x ,int y, int width , int height ){
this.color = color;
this.width = width ;
this.height = height ;
this.x = x ;
this.y = y ;
this.setLayout(null);
} public void paintComponent(Graphics g){
this.setBounds(x, y, width+1, height+1);
g.setColor(color);
for(int i = 0 ; i < 15*cellwidth ; ){
for(int j = 0 ; j < 15*cellheigth ; ){
g.drawRect(i,j,cellwidth,cellheigth);
j = j + cellheigth;
}
i = i + cellwidth;
}
}
public void update(Graphics g){
paintComponent(g);
}
} 棋子的代码package src.com.http.swing;import java.awt.*;
import java.util.HashMap;import javax.swing.*;
public class Chess extends JComponent{
private Color color ;
private int x;
private int y;
private boolean chessState ;
public Chess(){
}
public Chess(Color color , int x , int y ){
this.color = color;
this.x = x-20;
this.y = y-20;
}
public Chess( Color color){
this.color = color;
}
public void paintComponent(Graphics g){
this.setBounds(x, y, 40, 40);
g.setColor(color);
g.fillOval(0, 0, 40, 40);
}
public void setStater(boolean chessState){
this.chessState = chessState;
}
public boolean getState(){
return this.chessState;
}
public void setColor(Color color){
this.color = color;
}
public Color getColor(){
return this.color;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public void setX( int x){
this.x= x-20;
}
public void setY( int y){
this.y= y-20;
}
}这些代码复制了以后就可以运行 大家复制了后帮我看看怎么解决。
解决方案 »
- 十万火急,怎样实现点击一个按钮隐藏一个面板,象qq登录时的设置按钮一样!!
- 想要啊!但不知道怎么办?
- JAVA程序中把字符串插入SQL SERVER中的Date类型字段出错请教解决方案
- Int转为Integer
- 奇怪的异常: java.net.MalformedURLException: unknown protocol: https
- 关于java与vc之间的socket通讯问题
- 怎么把"134754481"里面都是半角的字符转成全角的字符串"134754481"??
- 如何在jbuilder中运行Applet?
- 怎样将一个10进制串转成真正的字符串?如“彰”代表的是汉字“彰”字。
- 方法中的return()是返回到何处的,如何能获得,请解答
- 两个面试问题求解
- java默认编码是什么?
你没看见我都给棋子里加了一个 chessState 属性
就是为了判断这个是不是要显示的
我一开始建立了一个 chess[16][16]得数组但是也还是出现这个问题你说的是我最先使用的方法 但是还是出现这个问题所以我才改成这样得 没想到还是没解决你在想想还有没有其他什么办法这个问题是由于你向jpanelt 添加组件的时候 他在加载新的组建的时候都重新绘制一遍 自己所以有抖动现象
paintComponent(g)吗?这个方法是写在 主frame里 ?
里面用一个Graphics对象来画棋盘和棋子
如果愿意 还可以用双缓存
里面用一个Graphics对象来画棋盘和棋子
如果愿意 还可以用双缓存
public void paint(Graphics g) {
if (memoryGraphics != null) {
borad.drawBoradImage(memoryGraphics);
if (chess != null) {
chess.drawChessImage(memoryGraphics);
}
}
g.drawImage(memoryImage, 0, 0, this);
}
再在mouseReleased事件方法中repaint