重新绘制一个面板的一部分

时间:2015-01-13 17:00:53

标签: java jpanel

我正在从书中做Java练习。有人说在刷新时只重绘一个面板的一部分,但没有提供足够的线索如何做到这一点:

(来自演习) 重新绘制面板的整个查看区域时,有时只会更改查看区域的一小部分。您可以通过仅重新绘制受影响的区域来提高性能,但不能 重新绘制面板时调用super.paintComponent(g),因为这将导致整个查看区域被清除。

它应该适用于任何问题,但是例如:它在按下a,w,s,d键时绘制线段(从中心开始),但不保留先前绘制的线

public class PartialRepaint extends JFrame{
    private DrawBoard dr=new DrawBoard();
    private int x,y,dx,dy;
    private boolean boo=true;

    public static void main(String[] args){
        PartialRepaint fr=new PartialRepaint();
        fr.setSize(400, 400);
        fr.setLocation(300, 200);
        fr.setTitle("KeyListening");
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setVisible(true);

    }
    public PartialRepaint(){
        add(dr);
        KListen listen=new KListen();
        dr.addKeyListener(listen);
        dr.setFocusable(true);
    }
    public class DrawBoard extends JPanel{
        protected void paintComponent(Graphics g){

            if(boo){
                //super.paintComponent(g);
                x=dr.getWidth()/2;
                y=dr.getHeight()/2;
            }
            int x1=x+dx;
            int y1=y+dy;
            g.drawLine(x, y, x1, y1);
            x=x1; y=y1;
           } 
    }
    public class KListen implements KeyListener{
        public void keyTyped(KeyEvent e) {
            if(e.getKeyChar()=='a'){
                dx=-10; dy=0;
            }
            if(e.getKeyChar()=='d'){
                dx=10; dy=0;
            }
            if(e.getKeyChar()=='w'){
                dx=0; dy=-10;
            }
            if(e.getKeyChar()=='s'){
                dx=0; dy=10;
            }
            boo=false;
            repaint();
        }
        public void keyPressed(KeyEvent e) {        }
        public void keyReleased(KeyEvent e) {        }

    }
}

1 个答案:

答案 0 :(得分:0)

您的代码根本没有重绘DrawingPanel。它只是根据您的按键来绘制新的线段。

此处将您的代码修改为从event dispatch thread(EDT)开始并格式化。

package com.ggl.testing;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PartialRepaint implements Runnable {
    private DrawBoard dr = new DrawBoard();
    private int x, y, dx, dy;
    private boolean boo = true;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new PartialRepaint());
    }

    @Override
    public void run() {
        KListen listen = new KListen();
        dr.addKeyListener(listen);
        dr.setFocusable(true);

        JFrame frame = new JFrame();
        frame.setTitle("KeyListening");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(dr);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class DrawBoard extends JPanel {

        private static final long serialVersionUID = 6566116168990163107L;

        public DrawBoard() {
            this.setPreferredSize(new Dimension(400, 400));
        }

        @Override
        protected void paintComponent(Graphics g) {
            if (boo) {
                // super.paintComponent(g);
                x = dr.getWidth() / 2;
                y = dr.getHeight() / 2;
            }
            int x1 = x + dx;
            int y1 = y + dy;
            g.drawLine(x, y, x1, y1);
            x = x1;
            y = y1;
        }
    }

    public class KListen implements KeyListener {
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == 'a') {
                dx = -10;
                dy = 0;
            }
            if (e.getKeyChar() == 'd') {
                dx = 10;
                dy = 0;
            }
            if (e.getKeyChar() == 'w') {
                dx = 0;
                dy = -10;
            }
            if (e.getKeyChar() == 's') {
                dx = 0;
                dy = 10;
            }
            boo = false;
            dr.repaint();
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
        }

    }
}