英文:
JPanel graphics flickering when is used the method getGraphics()
问题
我尝试创建一个类,类似于著名的Processing,但很快我发现了一个问题,因为在Processing中有setup()和draw()函数,在这些函数中可以执行特定的功能,比如square(10,10,50)。我试图通过创建一个类来模仿它,在这个类中有一个名为"gfx"的Graphics对象,将各种图形添加到其中。如果你读到这个纸张... 谢谢你,下面是PFrame类(模仿Processing的类)和BohClass类(测试类)。
package PFrame.com;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PFrame {
// ...(这里省略了你提供的类的内容,包括各种方法和变量定义)
// ...(你提供的类中的内容)
// ...(你提供的类中的内容)
}
import PFrame.com.*;
public class BohClass extends PFrame {
// ...(这里省略了你提供的BohClass类的内容,包括setup()和loop()方法的定义)
public static void main(String[] args) {
new BohClass();
}
}
注意:由于你要求只返回翻译好的内容,我已经删除了代码中的大部分内容,只保留了类和方法的定义。如果你需要详细的代码内容,请随时提问。
英文:
I try to create a class that was similar to the famous Processing, but soon I found a problem, since in Processing there are the setup () and draw () functions, in to which it is possible to perform certain functions, such as square (10,10,50), I tried to imitate it by creating a class, in which there is a Graphics object called "gfx" to which the various figures are added. If you read this papyrus ... thank you and I put below the PFrame class (that is the one that imitates Processing) and the BohClass class which will be the test class.
package PFrame.com;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PFrame {
private JFrame frame = new JFrame();
private DPane pane = new DPane(); //DPane is a class that extends JPanel
private Graphics2D gfx;
private Color selColor = new Color(255,255,255); //selected color
private boolean fill = true; //the shapes are filled?
public int width = 200, height = 200; //starting frame dimensins
public PFrame() {
//SKY
pane.setPreferredSize(new Dimension(width,height));
//HEAD
title(); //this functions give to the frame the name of the class (in this case "BohClass")
frame.setResizable(false);
//BODY
frame.add(pane);
pane.addComponentListener(new ResizeListener());
//TAIL
size(width,height); //function that setting size
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gfx = (Graphics2D) pane.getGraphics();
pane.paint(); //DPane function
}
//--|METHODS|--//
//FRAME CONFIG
public final void size(int x, int y) {
width = x;
height = y;
pane.setPreferredSize(new Dimension(width,height));
frame.pack();
frame.setLocationRelativeTo(null);
gfx = (Graphics2D) pane.getGraphics();
}
public final void exit() {
System.exit(0);
}
public final void hide() {
frame.setVisible(false);
}
public final void show() {
frame.setVisible(true);
}
public final void location(int XY) {
frame.setLocation(XY,XY);
}
public final void location(int X, int Y) {
frame.setLocation(X,Y);
}
public final void title() {
frame.setTitle((getClass() + "").split(" ")[1]);
}
public final void title(Object title) {
frame.setTitle(title.toString());
}
public final void resizable() {
frame.setResizable(!frame.isResizable());
}
public final void resizable(boolean resizable) {
frame.setResizable(resizable);
}
//2D CONFIG
public final void fill(boolean fill) {
this.fill = fill;
}
public final void color(int RGB) {
selColor = new Color(RGB,RGB,RGB);
}
public final void color(int R, int G, int B) {
selColor = new Color(R,G,B);
}
//2D
public final void square(int X, int Y, int L) {
rect(X,Y,L,L);
}
public final void square(int X, int Y, int L, int A) {
rect(X,Y,L,L,A);
}
public final void square(int X, int Y, int L, int Ax, int Ay) {
rect(X,Y,L,L,Ax,Ay);
}
public final void rect(int X, int Y, int W, int H) {
gfx.setColor(selColor);
if(fill) gfx.fillRect(X, Y, W, H);
else gfx.drawRect(X, Y, W, H);
}
public final void rect(int X, int Y, int W, int H,int A) {
gfx.setColor(selColor);
if(fill) gfx.fillRoundRect(X, Y, W, H, A, A);
else gfx.drawRoundRect(X, Y, W, H, A, A);
}
public final void rect(int X, int Y, int W, int H,int Ax, int Ay) {
gfx.setColor(selColor);
if(fill) gfx.fillRoundRect(X, Y, W, H, Ax, Ay);
else gfx.drawRoundRect(X, Y, W, H, Ax, Ay);
}
public final void circle(int X, int Y, int d) {
ellipse(X,Y,d,d);
}
public final void ellipse(int X, int Y, int W, int H) {
gfx.setColor(selColor);
if(fill) gfx.fillOval(X, Y, W, H);
else gfx.drawOval(X, Y, W, H);
}
public final void triangle(int Ax, int Ay, int Bx, int By, int Cx, int Cy) {
gfx.setColor(selColor);
if(fill) gfx.fillPolygon(new Polygon(new int[] {Ax,Bx,Cx}, new int[] {Ay,By,Cy}, 3));
else gfx.drawPolygon(new Polygon(new int[] {Ax,Bx,Cx}, new int[] {Ay,By,Cy}, 3));
}
//PANEL CONFIG
public final void background(int RGB) {
pane.setBackground(new Color(RGB,RGB,RGB));
}
public final void background(int R, int G, int B) {
pane.setBackground(new Color(R,G,B));
}
public final void clear() {
gfx.clearRect(0, 0, width, height);
}
//PUBLIC METHODS
public void setup() {}
public void loop() {}
//PRIVATE CLASS PANEL
private class DPane extends JPanel {
private static final long serialVersionUID = 57423L;
public void paint() {
setup();
while(true) {
loop();
}
}
}
//LISTENERS
class ResizeListener implements ComponentListener {
public void componentResized(ComponentEvent e) {
width = pane.getWidth();
height = pane.getHeight();
}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
}
}
and...
import PFrame.com.*;
public class BohClass extends PFrame {
public void setup() {
size(800,600);
background(100);
}
public void loop() {
color(72,28,47);
triangle(60,10,10,60,110,60);
color(255);
square(12,12,123);
}
public static void main(String[] args) {
new BohClass();
}
}
答案1
得分: 2
像其他人说的一样,你不应该在你的绘图函数中使用 while(true)
循环。
无耻的自我推广:在这里 有一个关于Swing中自定义绘图的教程,来自Processing背景。基本上你需要创建一个触发重绘的 Timer
。
英文:
Like others have said, you should not have a while(true)
loop inside your painting function.
Shameless self-promotion: here is a tutorial on custom painting in Swing, coming from a Processing background. Basically you need to create a Timer
that triggers a repaint.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论