Java,如何绘制不断变化的图形 [英] Java, how to draw constantly changing graphics

查看:109
本文介绍了Java,如何绘制不断变化的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前没有这样做过,所以很显然我吮吸它。这里,当前鼠标位置周围的64个像素在窗体上绘制得稍大一点。问题是,它有点慢,我不知道该从哪里开始修复。

除此之外,我做了一个线程,它不断调用更新图形当它完成后,像文本一样的fps,以显示真正绘制的速度。



图像示例:(图像来自Eclipse中的字母'a')

p>



代码示例:

  @SuppressWarnings(serial)
公共静态类AwtZoom extends Frame {
私有BufferedImage图像;
private long timeRef = new Date()。getTime();
机器人机器人= null;

public AwtZoom(){
super(Image zoom);
setLocation(new Point(640,0));
setSize(400,400);
setVisible(true);
final Ticker t =新Ticker();

this.image =(BufferedImage)(this.createImage(320,330));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
t.done();
dispose();
}
});
尝试{
robot = new Robot();
} catch(AWTException e){
e.printStackTrace();
}
t.start();
}

private class Ticker extends Thread {
public boolean update = true;
public void done(){
update = false;
}
public void run(){
try {
$ b $ while(update == true){
update(getGraphics());
//尝试{
// Thread.sleep(200);
//} catch(InterruptedException e){
// e.printStackTrace();
// return;
//
}
} catch(Exception e){

update = false;



$ b $ public void update(Graphics g){
paint(g);
}

boolean isdone = true;
public void paint(Graphics g){
if(isdone){
isdone = false;
int step = 40;
Point p = MouseInfo.getPointerInfo()。getLocation();

Graphics2D gc = this.image.createGraphics();对于(int y = 0; y< 8; y ++),为(int x = 0; x <8; x ++){
尝试{b
$ b try {

) ){
gc.setColor(robot.getPixelColor(px - 4 + x,py
- 4 + y));
gc.fillOval(x * step,y * step,step-3,step-3);
gc.setColor(Color.GRAY);
gc.drawOval(x * step,y * step,step - 3,step - 3);
}
}

} catch(Exception e){
e.printStackTrace();
}
gc.dispose();
isdone = true;
iter ++;
}
g.drawImage(image,40,45,this);
g.setColor(Color.black);
StringBuilder sb = new StringBuilder();
sb.append(iter)
.append(frames in)
.append((double)(new Date()。getTime() - this.timeRef)/ 1000)
.append(s。);
g.drawString(sb.toString(),50,375);
}

int iter = 0;
}

所做的更改:

* addedgc.dispose ();

*增加了isdone,所以重画不能被调用得更快,那么它应该是。
* added 此链接到thrashgod源码重写

*添加此链接到thrashgod源代码重写2

解决方案

这是我的主要重写,其中包含以下值得注意的更改: b
$ b


  • 我将检测像素颜色的任务从绘图任务中分离出来
  • 我替换了robot.getPixelColor (...)与robot.createScreenCapture(...)一次获取全部64个像素,而不是每次获取一个

  • 我已经引入了智能裁剪 - 只需要被重画是重绘。


  • 我已经修复了线程,因此对模型和视图的所有更新都发生在事件派发线程上。 b

    股票行情不断。当它检测到像素颜色变化(或者由于鼠标移动到不同的区域或者鼠标在变化的像素下),它会检测到更改的内容,更新模型,然后请求重新绘制视图。这种方法立即更新人眼。 289次屏幕更新累计耗时1秒。



    这是一个安静的周六晚上的愉快挑战。

      import javax.swing。*; 
    import java.awt。*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.geom.Ellipse2D;
    import java.awt.image.BufferedImage;

    public class ZoomPanel extends JPanel {

    private static final int STEP = 40;
    private int iter = 0;
    private private cumulativeTimeTaken = 0;


    public static void main(String [] args){
    final JFrame frame = new JFrame(Image zoom);

    final ZoomPanel zoomPanel = new ZoomPanel();
    frame.getContentPane()。add(zoomPanel);
    最终代码t =新代码(zoomPanel);

    frame.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent we){
    t.done();
    frame.dispose();
    }
    });
    t.start();

    frame.setLocation(new Point(640,0));
    frame.pack();
    frame.setVisible(true);
    }

    private final Color [] [] model = new Color [8] [8];

    public ZoomPanel(){
    setSize(new Dimension(400,400));
    setMinimumSize(new Dimension(400,400));
    setPreferredSize(new Dimension(400,400));
    setOpaque(true);
    }

    private void setColorAt(int x,int y,Color pixelColor){
    model [x] [y] = pixelColor;
    repaint(40 + x * STEP,45 + y * STEP,40 +(x * STEP)-3,45 +(y * STEP)-3);
    }

    private color getColorAt(int x,int y){
    return model [x] [y];
    }

    public void paintComponent(Graphics g){
    long start = System.currentTimeMillis();
    if(!SwingUtilities.isEventDispatchThread()){
    throw new RuntimeException(Repaint attempt not on event dispatch thread);
    }
    final Graphics2D g2 =(Graphics2D)g;
    g2.setColor(getBackground()); (int y = 0; y< 8; y ++){$ b($ x
    )为(int x = 0; x <8; x ++){
    尝试{

    $ b g2.setColor(model [x] [y]);
    Ellipse2D e =新的Ellipse2D.Double(40 + x *步骤,45 + y *步骤,步骤-3,步骤-3);
    g2.fill(e);
    g2.setColor(Color.GRAY);
    g2.draw(e);
    }
    }

    } catch(Exception e){
    e.printStackTrace();
    }
    iter ++;
    g2.setColor(Color.black);
    long stop = System.currentTimeMillis();
    cumulativeTimeTaken + =停止 - 开始;
    StringBuilder sb = new StringBuilder();
    sb.append(iter)
    .append(frames in)
    .append((double)(cumulativeTimeTaken)/ 1000)
    .append(s。) ;

    System.out.println(sb);
    }

    私有静态类Ticker extends Thread {

    private final Robot Robot;

    public boolean update = true;
    private final ZoomPanel视图;

    公共代码(ZoomPanel zoomPanel){
    view = zoomPanel;
    尝试{
    robot = new Robot();
    catch(AWTException e){
    抛出新的RuntimeException(e);
    }
    }

    public void done(){
    update = false;
    }

    public void run(){
    int runCount = 0;
    while(update){
    runCount ++;
    if(runCount%100 == 0){
    System.out.println(Ran ticker+ runCount +times);
    }
    final Point p = MouseInfo.getPointerInfo()。getLocation();

    Rectangle rect = new Rectangle(p.x - 4,p.y - 4,8,8);
    final BufferedImage capture = robot.createScreenCapture(rect); (int y = 0; y< 8; y ++){
    final colorColorColor =新颜色(capture.getRGB(x,y));

    if(!pixelColor.equals(view.getColorAt(x,y))){
    final int finalX = x;
    final int finalY = y;
    SwingUtilities.invokeLater(new Runnable(){
    public void run(){
    view.setColorAt(finalX,finalY,pixelColor);
    }
    });
    }
    }
    }

    }
    }

    }

    }


    Have not done this before, so obviously I suck at it. Here 64 pixels around current mouse position get drawn little bigger on a form. Problem is, that it's 'kind of' to slow, and I have no idea where to start fixing.

    Besides that, I made a thread, that constantly calls update graphics when it's finished and a little fps like text, to show really how fast things are drawn.

    Image example: (Image is from letter 'a' in Eclipse)

    Code example :

    @SuppressWarnings("serial")
    public static class AwtZoom extends Frame {
        private BufferedImage image;
        private long timeRef = new Date().getTime();
        Robot robot = null;
    
        public AwtZoom() {
            super("Image zoom");
            setLocation(new Point(640, 0));
            setSize(400, 400);
            setVisible(true);
            final Ticker t = new Ticker();
    
            this.image = (BufferedImage) (this.createImage(320, 330));
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    t.done();
                    dispose();
                }
            });
            try {
                robot = new Robot();
            } catch (AWTException e) {
                e.printStackTrace();
            }
            t.start();
        }
    
        private class Ticker extends Thread {
            public boolean update = true;
            public void done() {
                update = false;
            }
            public void run() {
                try {
    
                    while (update == true) {
                        update(getGraphics());
                        // try {
                        // Thread.sleep(200);
                        // } catch (InterruptedException e) {
                        // e.printStackTrace();
                        // return;
                        // }
                    }
                } catch (Exception e) {
    
                    update=false;
                }
            }
        }
    
        public void update(Graphics g) {
            paint(g);
        }
    
        boolean isdone = true;
        public void paint(Graphics g) {
            if (isdone) {
                isdone=false;
                int step = 40;
                Point p = MouseInfo.getPointerInfo().getLocation();
    
                Graphics2D gc = this.image.createGraphics();
    
                try {
    
                    for (int x = 0; x < 8; x++) {
                        for (int y = 0; y < 8; y++) {
                            gc.setColor(robot.getPixelColor(p.x - 4 + x, p.y
                                    - 4 + y));
                            gc.fillOval(x * step, y * step, step - 3, step - 3);
                            gc.setColor(Color.GRAY);
                            gc.drawOval(x * step, y * step, step - 3, step - 3);
                        }
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
                gc.dispose();
                isdone = true;
                iter++;
            }
            g.drawImage(image, 40, 45, this);
            g.setColor(Color.black);
            StringBuilder sb = new StringBuilder();
            sb.append(iter)
                    .append(" frames in ")
                    .append((double) (new Date().getTime() - this.timeRef) / 1000)
                    .append("s.");
            g.drawString(sb.toString(), 50, 375);
        }
    
        int iter = 0;
    }
    

    Changes made:
    * added "gc.dispose();"
    * added "isdone", so redraw could not be called faster, then it should.
    * added this link to thrashgod source rewrite
    * added this link to thrashgod source rewrite 2

    解决方案

    Here's my major rewrite with the following noteworthy changes:

    • I've separated the task of detecting pixel colours from the task of drawing
    • I've replaced robot.getPixelColor(...) with robot.createScreenCapture(...) to fetch all 64 pixels at once, rather than one at a time
    • I've introduced smart clipping - only what needs to be redrawn is redrawn.
    • I've fixed up threading so all updates to the model and view happen on the Event Dispatch Thread

    The ticker runs constantly. When it detects a change in pixel colour (either due to the mouse moving to a different region or the pixels under the mouse changing) it detects exactly what changed, updates the model, then requests the view to repaint. This approach updates instantly to the human eye. 289 screen updates took cumulatively 1 second.

    It was an enjoyable challenge for a quiet Saturday evening.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.geom.Ellipse2D;
    import java.awt.image.BufferedImage;
    
    public class ZoomPanel extends JPanel {
    
        private static final int STEP = 40;
        private int iter = 0;
        private long cumulativeTimeTaken = 0;
    
    
        public static void main(String[] args) {
            final JFrame frame = new JFrame("Image zoom");
    
            final ZoomPanel zoomPanel = new ZoomPanel();
            frame.getContentPane().add(zoomPanel);
            final Ticker t = new Ticker(zoomPanel);
    
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    t.done();
                    frame.dispose();
                }
            });
            t.start();
    
            frame.setLocation(new Point(640, 0));
            frame.pack();
            frame.setVisible(true);
        }
    
        private final Color[][] model = new Color[8][8];
    
        public ZoomPanel() {
            setSize(new Dimension(400, 400));
            setMinimumSize(new Dimension(400, 400));
            setPreferredSize(new Dimension(400, 400));
            setOpaque(true);
        }
    
        private void setColorAt(int x, int y, Color pixelColor) {
            model[x][y] = pixelColor;
            repaint(40 + x * STEP, 45 + y * STEP, 40 + (x * STEP) - 3, 45 + (y * STEP) - 3);
        }
    
        private Color getColorAt(int x, int y) {
            return model[x][y];
        }
    
        public void paintComponent(Graphics g) {
            long start = System.currentTimeMillis();
            if (!SwingUtilities.isEventDispatchThread()) {
                throw new RuntimeException("Repaint attempt is not on event dispatch thread");
            }
            final Graphics2D g2 = (Graphics2D) g;
            g2.setColor(getBackground());
            try {
    
                for (int x = 0; x < 8; x++) {
                    for (int y = 0; y < 8; y++) {
                        g2.setColor(model[x][y]);
                        Ellipse2D e = new Ellipse2D.Double(40 + x * STEP, 45 + y * STEP, STEP - 3, STEP - 3);
                        g2.fill(e);
                        g2.setColor(Color.GRAY);
                        g2.draw(e);
                    }
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            iter++;
            g2.setColor(Color.black);
            long stop = System.currentTimeMillis();
            cumulativeTimeTaken += stop - start;
            StringBuilder sb = new StringBuilder();
            sb.append(iter)
                    .append(" frames in ")
                    .append((double) (cumulativeTimeTaken) / 1000)
                    .append("s.");
    
            System.out.println(sb);
        }
    
        private static class Ticker extends Thread {
    
            private final Robot robot;
    
            public boolean update = true;
            private final ZoomPanel view;
    
            public Ticker(ZoomPanel zoomPanel) {
                view = zoomPanel;
                try {
                    robot = new Robot();
                } catch (AWTException e) {
                    throw new RuntimeException(e);
                }
            }
    
            public void done() {
                update = false;
            }
    
            public void run() {
                int runCount = 0;
                while (update) {
                    runCount++;
                    if (runCount % 100 == 0) {
                        System.out.println("Ran ticker " + runCount + " times");
                    }
                    final Point p = MouseInfo.getPointerInfo().getLocation();
    
                    Rectangle rect = new Rectangle(p.x - 4, p.y - 4, 8, 8);
                    final BufferedImage capture = robot.createScreenCapture(rect);
    
                    for (int x = 0; x < 8; x++) {
                        for (int y = 0; y < 8; y++) {
                            final Color pixelColor = new Color(capture.getRGB(x, y));
    
                            if (!pixelColor.equals(view.getColorAt(x, y))) {
                                final int finalX = x;
                                final int finalY = y;
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        view.setColorAt(finalX, finalY, pixelColor);
                                    }
                                });
                            }
                        }
                    }
    
                }
            }
    
        }
    
    }
    

    这篇关于Java,如何绘制不断变化的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆