拖动鼠标快速移动时,绘图程序滞后 [英] Paint program lags when mouse dragged is moved fast
本文介绍了拖动鼠标快速移动时,绘图程序滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿伙计,所以我试图重新创建一个版本的mspaintm,我有几个问题。首先,当我在画布上快速绘画时,很明显并非每个位置都被绘制,是否可以使用多线程解决问题?另外我如何保存屏幕的部分并将其保存为文件,谢谢你看看
import javax.swing。*;
公共类CanvasInitializer {
public static void main(String [] args){
CanvasPanel canvasPanel = new CanvasPanel();
canvasPanel.setSize(500,500);
canvasPanel.setTitle(Canvas);
canvasPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvasPanel.setVisible(true);
}
}
import javax.swing。*;
import java.awt。*;
import java.awt.event。*;
import java.util。*;
公共类CanvasPanel扩展JFrame {
private Brush brush;
private LinkedList< Brush> brushList;
私有JButton清除,擦除;
private Map< String,Color>地图;
私人帆布帆布;
private JComboBox< String> jComboBox,brushCombo;
private String [] brushTips = {Small,Medium,Large};
private Color [] colors = {Color.BLACK,Color.BLUE,Color.CYAN,Color.GRAY,Color.DARK_GRAY,Color.LIGHT_GRAY,Color.GREEN,
Color.MAGENTA,Color.ORANGE ,Color.PINK,Color.RED,Color.WHITE,Color.YELLOW};
private String [] colorArray = {Black,Blue,Cyan,Grey,DarkGray,LightGray,
Green,Magneta,Orange , 红粉, 红, 白, 黄};
private Color colorSelected = Color.black;
private int xGrid,yGrid,width,height;
public CanvasPanel(){
clear = new JButton(Clear);
erase = new JButton(Erase);
map = new HashMap< String,Color>();
brushList = new LinkedList< Brush>();
jComboBox = new JComboBox< String>(colorArray);
brushCombo = new JComboBox< String>(brushTips);
canvas = new Canvas();
canvas.requestFocus();
for(int i = 0; i< colorArray.length; ++ i){
map.put(colorArray [i],colors [i]);
}
JPanel topPanel = new JPanel();
topPanel.add(jComboBox);
topPanel.add(brushCombo);
topPanel.add(clear);
topPanel.add(擦除);
clear.addActionListener(new ButtonListener());
erase.addActionListener(new ButtonListener());
jComboBox.addActionListener(new ComboBox());
brushCombo.addActionListener(new BrushTip());
canvas.addMouseMotionListener(new PointListener());
JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT,topPanel,canvas);
setLayout(new BorderLayout());
add(sp);
}
公共类Canvas扩展JPanel实现Runnable {
public Canvas(){
setBackground(Color。白色);
}
protected void paintComponent(Graphics page){
super.paintComponent(page);
if(!brushList.isEmpty())
for(int i = 0; i< brushList.size(); ++ i){
brushList.get(i).drawImage(页);
}
}
公共类画笔{
private int x,y,width,height;
私人颜色;
public Brush(int x,int y,int width,int height,Color color){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
protected void drawImage(图形图形){
graphics.setColor(color);
graphics.fillOval(x,y,width,height);
}
}
解决方案
多线程可能会也可能不会帮到你。如果你使用多线程,你必须犯下的一个主要罪恶是拥有2个或更多线程来执行UI功能。如果您能够将非UI处理分离到另一个线程,您可能会(强调:可能)看到一些改进。这可能会或可能不会起作用的原因是两个或多个线程之间的接口将导致额外的处理,这可能会使性能变差。
多线程技术之一/可能对你有帮助的模式是使用Actor模型。 此处 [ ^ ]并且有一个CP示例(C ++)这里 [ ^ ]。
Hey guys so Im trying to recreate a version of mspaintm, and I have a couple of questions. First when i rapidly paint in the canvas it becomes apparent that not every location is painted, would the problem be solved using mutli threading? Also how would i save the portion of the screen and save it as a file, thanks for looking
import javax.swing.*; public class CanvasInitializer { public static void main(String [] args){ CanvasPanel canvasPanel = new CanvasPanel(); canvasPanel.setSize(500,500); canvasPanel.setTitle("Canvas"); canvasPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canvasPanel.setVisible(true); } }
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class CanvasPanel extends JFrame { private Brush brush; private LinkedList<Brush> brushList; private JButton clear, erase; private Map<String,Color> map ; private Canvas canvas; private JComboBox<String> jComboBox , brushCombo; private String [] brushTips = {"Small", "Medium","Large"}; private Color [] colors = {Color.BLACK,Color.BLUE, Color.CYAN, Color.GRAY,Color.DARK_GRAY, Color.LIGHT_GRAY, Color.GREEN, Color.MAGENTA,Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW}; private String [] colorArray = {"Black","Blue","Cyan","Gray","DarkGray","LightGray", "Green","Magneta","Orange","Pink","Red","White","Yellow"}; private Color colorSelected = Color.black; private int xGrid,yGrid, width, height; public CanvasPanel(){ clear = new JButton("Clear"); erase = new JButton("Erase"); map = new HashMap<String, Color>(); brushList = new LinkedList<Brush>(); jComboBox = new JComboBox<String>(colorArray); brushCombo = new JComboBox<String>(brushTips); canvas = new Canvas(); canvas.requestFocus(); for(int i = 0; i< colorArray.length; ++i){ map.put(colorArray[i], colors[i]); } JPanel topPanel = new JPanel(); topPanel.add(jComboBox); topPanel.add(brushCombo); topPanel.add(clear); topPanel.add(erase); clear.addActionListener(new ButtonListener()); erase.addActionListener(new ButtonListener()); jComboBox.addActionListener(new ComboBox()); brushCombo.addActionListener(new BrushTip()); canvas.addMouseMotionListener(new PointListener()); JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel, canvas); setLayout(new BorderLayout()); add(sp); } public class Canvas extends JPanel implements Runnable{ public Canvas(){ setBackground(Color.WHITE); } protected void paintComponent(Graphics page){ super.paintComponent(page); if(!brushList.isEmpty()) for(int i =0; i< brushList.size(); ++i){ brushList.get(i).drawImage(page); } }
public class Brush { private int x, y, width, height; private Color color; public Brush(int x, int y,int width, int height, Color color){ this.x = x; this.y = y; this.width = width; this.height = height; this.color = color; } protected void drawImage(Graphics graphics){ graphics.setColor(color); graphics.fillOval(x,y,width,height); } }
解决方案
Multithreading may or may not help you here. If you use multithreading, one cardinal sin you must not commit is to have 2 or more threads performing UI functions. You might (emphasis: "might") see some improvements if you are able to spin off non-UI processing to another thread. The reason that this may or may not work is that interfacing between 2 or more threads will cause extra processing to occur which might make performance worse.
One of the multithreading techniques/patterns that might be helpful for you is use of the "Actor model". This is described here[^] and there is a CP example (C++) here[^].
这篇关于拖动鼠标快速移动时,绘图程序滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文