Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java图形图像刷新率_Java_Swing_Timer - Fatal编程技术网

Java图形图像刷新率

Java图形图像刷新率,java,swing,timer,Java,Swing,Timer,我正在制作一个JFrame,其中包含几个使用repaint()刷新的图像。然而,问题是,我添加的图像越多(或者渲染的区域越大),帧速率就急剧下降 我最初试图减少Swing不可预知的计时器所经历的延迟,但没有成功。这次,我试着打电话 repaint() 基于系统时间,每10毫秒一次。(要绕过计时器,将调用desktop.screenPaint.drawWindow();而不是repaint()) while(true) { long starttime=System.currentTimeMil

我正在制作一个JFrame,其中包含几个使用repaint()刷新的图像。然而,问题是,我添加的图像越多(或者渲染的区域越大),帧速率就急剧下降

我最初试图减少Swing不可预知的计时器所经历的延迟,但没有成功。这次,我试着打电话

repaint()
基于系统时间,每10毫秒一次。(要绕过计时器,将调用desktop.screenPaint.drawWindow();而不是repaint())

while(true)
{
long starttime=System.currentTimeMillis();
而(System.currentTimeMillis()-starttime<10)
{
}
desktop.screenPaint.drawWindow();
desktop2.screenPaint.drawWindow();
}
如果运行下面的代码,每个“桌面”只是您看到的可拖动窗口之一。我注意到,当“框架”中只添加“桌面”时,并没有闪烁。但是,当同时添加“desktop2”时,闪烁不会消失,直到每次刷新设置为20毫秒或更长时间。这是非常奇怪的,因为当我还在使用计时器时,即使等待时间设置为10毫秒,帧速率也会降低到每20毫秒一帧!我做了一点测试,发现了一个趋势:你会一直闪烁,直到等待时间达到10毫秒计时器滞后的时间。我的问题是:为什么会发生这种情况?除了回到计时器,还有什么方法可以防止闪烁吗

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.SwingUtilities.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.io.File;
import java.io.IOException;
public class Display extends JPanel
{
   public static int width, height;
   public static JFrame frame = new JFrame("");
   private static int rh = 10;
   public static Display desktop = new Display();
   public static Display desktop2 = new Display();
   public static void main(String[] args)
   {
      Dimension screen = new Dimension();
      screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
      width = (int)screen.getWidth();
      height = (int)screen.getHeight();
      frame.setLayout(null);
      frame.setLocation(0, 0);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(null);
      desktop.setBounds(0, 0, 620, 500+rh);
      desktop2.setBounds(1000, 200, 620, 500+rh);
      frame.add(desktop);
      frame.add(desktop2);
      frame.setUndecorated(true);
      frame.setSize(width, height);
      frame.setVisible(true);
      while(true)
      {
         long starttime = System.currentTimeMillis();
         while(System.currentTimeMillis()-starttime < 10)
         {
         }
         desktop.screenPaint.drawWindow();
         //desktop2.screenPaint.drawWindow();
      }
   }
   private BufferedImage image;
   private Graphics2D g;
   public Listener screenPaint = new Listener();
   private Display identify;
   public Display()
   {
      identify = this;
      image = new BufferedImage(620, 500+rh, BufferedImage.TYPE_INT_RGB);
      g = (Graphics2D)image.getGraphics();
      Timer timer = new Timer(1, screenPaint);
      timer.start();
      addMouseListener(new mMouse());
      addMouseWheelListener(new wWheel());
      setFocusable(true);
   }
   private BufferedImage toCompatibleImage(BufferedImage image)
   {
      GraphicsConfiguration gfx_config = GraphicsEnvironment.
         getLocalGraphicsEnvironment().getDefaultScreenDevice().
         getDefaultConfiguration();
      if (image.getColorModel().equals(gfx_config.getColorModel()))
         return image;
      BufferedImage new_image = gfx_config.createCompatibleImage(
         image.getWidth(), image.getHeight(), image.getTransparency());
   }
   public void paint(Graphics view)
   {
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      view.drawImage(toCompatibleImage(image), 0, 0, 620, 500+rh, null);
      view.dispose();
   }
   private class Listener implements ActionListener
   {
      int y = 0;
      int wy = 0;
      int b = 500;
      int c = 1500;
      int iy = (int)(wy/(c/(double)b));
      int a = -1;
      int i = -1;
      int j = -1;
      boolean d = false;
      boolean u = false;
      int f = 0;
      public void moveY(int sy)
      {
         f += sy;
      }
      public void actionPerformed(ActionEvent e)
      {
         //drawWindow();
      }
      public void drawWindow()
      {
         int lx = (int)(identify.getLocation().getX());
         int ly = (int)(identify.getLocation().getY());
         g.setColor(Color.white);
         g.fillRect(0, 0, 620, 500+rh);
         g.drawImage(new ImageIcon("image.png").getImage(), 0, wy+rh, 610, c, null);
         if(c > b)
         {
            if(d || Mouse.withinRect(610+lx, (int)(-wy/(c/(double)b))+rh+ly, 10, (int)Math.ceil(Math.pow(b, 2)/(double)c)))
            {
               if(Mouse.pressLeft())
               {
                  if(!d)d = true;
                  if(a == -1)a = Mouse.y()-(int)(-wy/(c/(double)b));
                  y = (int)((Mouse.y()-a)*(c/(double)b));
                  f = y;
                  g.setColor(Color.black);
               }
               else
               {  
                  if(d)d = false;
                  if(a != -1)a = -1;
                  g.setColor(new Color(60, 60, 60));
               }
            }
            else
            {
               g.setColor(Color.gray);
               if(a != -1)a = -1;
            }
            if(y == f){}
            else if(y < f)
            {
               y += (int)((f-y)*0.1);
               if(y < f)y++;
            }
            else
            {
               y -= (int)((y-f)*0.1);
               if(y > f)y--;
            }
            if(y < 0)
            {
               y = 0;
               f = 0;
            }
            else if(y > c-b)
            {
               y = c-b;
               f = y;
            }
            wy = -y;
            if(u || Mouse.withinRect(lx, ly, 620, 10))
            {
               if(Mouse.pressLeft())
               {
                  if(!u)u = true;
                  if(i == -1)i = Mouse.x()-lx;
                  if(j == -1)j = Mouse.y()-ly;
                  identify.setLocation(Mouse.x()-i, Mouse.y()-j);
               }
               else
               {  
                  if(u)u = false;
                  if(i != -1)i = -1;
                  if(j != -1)j = -1;
               }
            }
            else
            {
               if(u)u = false;
               if(i != -1)i = -1;
               if(j != -1)j = -1;
            }
            int scrollBarLength = (int)Math.ceil(Math.pow(b, 2)/(double)c);
            g.fillRect(610, (int)(-wy/(c/(double)b))+rh, 10, scrollBarLength);
         }
         else
         {
            g.setColor(new Color(200, 200, 200));
            g.fillRect(610, rh, 10, b);
         }
         g.setColor(new Color(50, 50, 50));
         g.fillRect(0, 0, 620, rh);
         if(identify == desktop)
            repaint();
         else if(false)
         {}
         }
      }
   }
   public static boolean LMPress, LMRelease = false;
   public static boolean LMRight, LMLeft = false;
   private class mMouse extends MouseAdapter
   {
      public void mousePressed(MouseEvent e)
      {
         LMPress = true; LMRelease = false;
         if(SwingUtilities.isRightMouseButton(e))
         {
            LMRight = true;
            LMLeft = false;
         }
         else if(SwingUtilities.isLeftMouseButton(e))
         {
            LMLeft = true;
            LMRight = false;
         }
      }
      public void mouseReleased(MouseEvent e)
      {
         LMRelease = true; LMPress = false;
         if(SwingUtilities.isRightMouseButton(e))
         {
            LMRight = true;
            LMLeft = false;
         }
         else if(SwingUtilities.isLeftMouseButton(e))
         {
            LMLeft = true;
            LMRight = false;
         }
      }
   }
   private class wWheel implements MouseWheelListener
   {
      public void mouseWheelMoved(MouseWheelEvent e)
      {
         int notches = e.getWheelRotation();
         if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {}
         e.getScrollAmount();
         screenPaint.moveY(e.getUnitsToScroll()*60); //desired pixel jump: 120
      }
   }
}

import javax.swing.JFrame;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.image.*;
导入javax.swing.SwingUtilities.*;
导入静态java.awt.GraphicsDevice.Window半透明。*;
导入java.io.File;
导入java.io.IOException;
公共类显示扩展了JPanel
{
公共静态int宽度、高度;
公共静态JFrame=新JFrame(“”);
专用静态int rh=10;
公共静态显示桌面=新建显示();
公共静态显示桌面2=新显示();
公共静态void main(字符串[]args)
{
维度屏幕=新维度();
screen=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
宽度=(int)screen.getWidth();
高度=(int)screen.getHeight();
frame.setLayout(空);
帧设置位置(0,0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(空);
桌面.立根(0,0,620,500+rh);
桌面2.立根(1000200620500+rh);
添加框架(桌面);
frame.add(桌面2);
框架。设置未装饰(真实);
框架尺寸(宽度、高度);
frame.setVisible(true);
while(true)
{
long starttime=System.currentTimeMillis();
而(System.currentTimeMillis()-starttime<10)
{
}
desktop.screenPaint.drawWindow();
//desktop2.screenPaint.drawWindow();
}
}
私有缓冲图像;
私有图形2dg;
public Listener screenPaint=new Listener();
私人显示识别;
公开展览()
{
识别=这个;
图像=新的BuffereImage(620500+rh,BuffereImage.TYPE_INT_RGB);
g=(Graphics2D)image.getGraphics();
定时器=新定时器(1,屏幕绘制);
timer.start();
addMouseListener(新的mMouse());
addMouseWheelListener(新wWheel());
设置聚焦(真);
}
专用缓冲区映像到CompatibleImage(缓冲区映像)
{
图形配置gfx_config=GraphicsEnvironment。
getLocalGraphicsEnvironment().getDefaultScreenDevice()。
getDefaultConfiguration();
if(image.getColorModel().equals(gfx_config.getColorModel()))
返回图像;
BuffereImage new_image=gfx_config.createCompatibleImage(
image.getWidth()、image.getHeight()、image.getTransparency();
}
公共空间绘制(图形视图)
{
g、 setRenderingHint(RenderingHits.KEY\u ANTIALIASING,RenderingHits.VALUE\u ANTIALIAS\u ON);
view.drawImage(到CompatibleImage(图像),0,0,620,500+rh,空);
view.dispose();
}
私有类侦听器实现ActionListener
{
int y=0;
int-wy=0;
int b=500;
int c=1500;
intiy=(int)(wy/(c/(double)b));
int a=-1;
int i=-1;
int j=-1;
布尔d=假;
布尔u=假;
int f=0;
公共无效移动(int sy)
{
f+=sy;
}
已执行的公共无效操作(操作事件e)
{
//拉窗();
}
公共空窗口()
{
intlx=(int)(identifite.getLocation().getX());
int ly=(int)(identifite.getLocation().getY());
g、 setColor(Color.white);
g、 fillRect(0,0,620,500+rh);
g、 drawImage(新的ImageIcon(“image.png”).getImage(),0,wy+rh,610,c,null);
如果(c>b)
{
如果(d | |鼠标在(610+lx,(int)(-wy/(c/(double)b))+rh+ly,10,(int)Math.ceil(Math.pow(b,2)/(double)c)))
{
如果(鼠标左键())
{
如果(!d)d=true;
如果(a==-1)a=Mouse.y()-(int)(-wy/(c/(double)b));
y=(int)((Mouse.y()-a)*(c/(double)b));
f=y;
g、 设置颜色(颜色为黑色);
}
其他的
{  
如果(d)d=假;
如果(a!=-1)a=-1;
g、 setColor(新颜色(60,60,60));
}
}
其他的
{
g、 setColor(颜色为灰色);
如果(a!=-1)a=-1;
}
如果(y==f){}
else if(yf)y--;
}
if(y<0)
{
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.SwingUtilities.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.io.File;
import java.io.IOException;
public class Display extends JPanel
{
   public static int width, height;
   public static JFrame frame = new JFrame("");
   private static int rh = 10;
   public static Display desktop = new Display();
   public static Display desktop2 = new Display();
   public static void main(String[] args)
   {
      Dimension screen = new Dimension();
      screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
      width = (int)screen.getWidth();
      height = (int)screen.getHeight();
      frame.setLayout(null);
      frame.setLocation(0, 0);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(null);
      desktop.setBounds(0, 0, 620, 500+rh);
      desktop2.setBounds(1000, 200, 620, 500+rh);
      frame.add(desktop);
      frame.add(desktop2);
      frame.setUndecorated(true);
      frame.setSize(width, height);
      frame.setVisible(true);
      while(true)
      {
         long starttime = System.currentTimeMillis();
         while(System.currentTimeMillis()-starttime < 10)
         {
         }
         desktop.screenPaint.drawWindow();
         //desktop2.screenPaint.drawWindow();
      }
   }
   private BufferedImage image;
   private Graphics2D g;
   public Listener screenPaint = new Listener();
   private Display identify;
   public Display()
   {
      identify = this;
      image = new BufferedImage(620, 500+rh, BufferedImage.TYPE_INT_RGB);
      g = (Graphics2D)image.getGraphics();
      Timer timer = new Timer(1, screenPaint);
      timer.start();
      addMouseListener(new mMouse());
      addMouseWheelListener(new wWheel());
      setFocusable(true);
   }
   private BufferedImage toCompatibleImage(BufferedImage image)
   {
      GraphicsConfiguration gfx_config = GraphicsEnvironment.
         getLocalGraphicsEnvironment().getDefaultScreenDevice().
         getDefaultConfiguration();
      if (image.getColorModel().equals(gfx_config.getColorModel()))
         return image;
      BufferedImage new_image = gfx_config.createCompatibleImage(
         image.getWidth(), image.getHeight(), image.getTransparency());
   }
   public void paint(Graphics view)
   {
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      view.drawImage(toCompatibleImage(image), 0, 0, 620, 500+rh, null);
      view.dispose();
   }
   private class Listener implements ActionListener
   {
      int y = 0;
      int wy = 0;
      int b = 500;
      int c = 1500;
      int iy = (int)(wy/(c/(double)b));
      int a = -1;
      int i = -1;
      int j = -1;
      boolean d = false;
      boolean u = false;
      int f = 0;
      public void moveY(int sy)
      {
         f += sy;
      }
      public void actionPerformed(ActionEvent e)
      {
         //drawWindow();
      }
      public void drawWindow()
      {
         int lx = (int)(identify.getLocation().getX());
         int ly = (int)(identify.getLocation().getY());
         g.setColor(Color.white);
         g.fillRect(0, 0, 620, 500+rh);
         g.drawImage(new ImageIcon("image.png").getImage(), 0, wy+rh, 610, c, null);
         if(c > b)
         {
            if(d || Mouse.withinRect(610+lx, (int)(-wy/(c/(double)b))+rh+ly, 10, (int)Math.ceil(Math.pow(b, 2)/(double)c)))
            {
               if(Mouse.pressLeft())
               {
                  if(!d)d = true;
                  if(a == -1)a = Mouse.y()-(int)(-wy/(c/(double)b));
                  y = (int)((Mouse.y()-a)*(c/(double)b));
                  f = y;
                  g.setColor(Color.black);
               }
               else
               {  
                  if(d)d = false;
                  if(a != -1)a = -1;
                  g.setColor(new Color(60, 60, 60));
               }
            }
            else
            {
               g.setColor(Color.gray);
               if(a != -1)a = -1;
            }
            if(y == f){}
            else if(y < f)
            {
               y += (int)((f-y)*0.1);
               if(y < f)y++;
            }
            else
            {
               y -= (int)((y-f)*0.1);
               if(y > f)y--;
            }
            if(y < 0)
            {
               y = 0;
               f = 0;
            }
            else if(y > c-b)
            {
               y = c-b;
               f = y;
            }
            wy = -y;
            if(u || Mouse.withinRect(lx, ly, 620, 10))
            {
               if(Mouse.pressLeft())
               {
                  if(!u)u = true;
                  if(i == -1)i = Mouse.x()-lx;
                  if(j == -1)j = Mouse.y()-ly;
                  identify.setLocation(Mouse.x()-i, Mouse.y()-j);
               }
               else
               {  
                  if(u)u = false;
                  if(i != -1)i = -1;
                  if(j != -1)j = -1;
               }
            }
            else
            {
               if(u)u = false;
               if(i != -1)i = -1;
               if(j != -1)j = -1;
            }
            int scrollBarLength = (int)Math.ceil(Math.pow(b, 2)/(double)c);
            g.fillRect(610, (int)(-wy/(c/(double)b))+rh, 10, scrollBarLength);
         }
         else
         {
            g.setColor(new Color(200, 200, 200));
            g.fillRect(610, rh, 10, b);
         }
         g.setColor(new Color(50, 50, 50));
         g.fillRect(0, 0, 620, rh);
         if(identify == desktop)
            repaint();
         else if(false)
         {}
         }
      }
   }
   public static boolean LMPress, LMRelease = false;
   public static boolean LMRight, LMLeft = false;
   private class mMouse extends MouseAdapter
   {
      public void mousePressed(MouseEvent e)
      {
         LMPress = true; LMRelease = false;
         if(SwingUtilities.isRightMouseButton(e))
         {
            LMRight = true;
            LMLeft = false;
         }
         else if(SwingUtilities.isLeftMouseButton(e))
         {
            LMLeft = true;
            LMRight = false;
         }
      }
      public void mouseReleased(MouseEvent e)
      {
         LMRelease = true; LMPress = false;
         if(SwingUtilities.isRightMouseButton(e))
         {
            LMRight = true;
            LMLeft = false;
         }
         else if(SwingUtilities.isLeftMouseButton(e))
         {
            LMLeft = true;
            LMRight = false;
         }
      }
   }
   private class wWheel implements MouseWheelListener
   {
      public void mouseWheelMoved(MouseWheelEvent e)
      {
         int notches = e.getWheelRotation();
         if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {}
         e.getScrollAmount();
         screenPaint.moveY(e.getUnitsToScroll()*60); //desired pixel jump: 120
      }
   }
}
import java.awt.*;
import java.awt.event.*;

public class Mouse {
    public static int x() {
        PointerInfo a = MouseInfo.getPointerInfo();
        Point b = a.getLocation();
        return (int) b.getX();
    }

    public static int y() {
        PointerInfo a = MouseInfo.getPointerInfo();
        Point b = a.getLocation();
        return (int) b.getY();
    }

    public static boolean withinRect(int x, int y, int w, int h) {
        if (x() >= x && x() < x + w && y() >= y && y() < y + h) return true;
        else return false;
    }

    public static boolean press() {
        return Display.LMPress;
    }

    public static boolean release() {
        return Display.LMRelease;
    }

    public static boolean pressLeft() {
        if (Display.LMPress && Display.LMLeft)
            return true;
        else return false;
    }

    public static boolean releaseLeft() {
        if (Display.LMRelease && Display.LMLeft)
            return true;
        else return false;
    }

    public static boolean pressRight() {
        if (Display.LMPress && Display.LMRight)
            return true;
        else return false;
    }

    public static boolean releaseRight() {
        if (Display.LMRelease && Display.LMRight)
            return true;
        else return false;
    }
}