Java 使用方法getGraphics()时,JPanel graphics闪烁
我试图创建一个类似于著名处理的类,但很快我发现了一个问题,因为在处理过程中有setup()和draw()函数,可以在其中执行某些函数,例如square(10,10,50),我试图通过创建一个类来模拟它,其中有一个名为“gfx”的图形对象其中添加了各种数字。如果你读这张纸莎草。。。谢谢你,我把PFrame类(这是一个模仿处理的类)和BohClass类放在下面,后者将是测试类Java 使用方法getGraphics()时,JPanel graphics闪烁,java,swing,processing,Java,Swing,Processing,我试图创建一个类似于著名处理的类,但很快我发现了一个问题,因为在处理过程中有setup()和draw()函数,可以在其中执行某些函数,例如square(10,10,50),我试图通过创建一个类来模拟它,其中有一个名为“gfx”的图形对象其中添加了各种数字。如果你读这张纸莎草。。。谢谢你,我把PFrame类(这是一个模仿处理的类)和BohClass类放在下面,后者将是测试类 package PFrame.com; import javax.swing.*; import java.awt.*;
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) {}
}
}
而且
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();
}
}
正如其他人所说,在绘制函数中不应该有
while(true)
循环
无耻的自我提升:是一个关于Swing中定制绘画的教程,来自一个处理背景。基本上,您需要创建一个触发重新绘制的
计时器。就像其他人所说的那样,在绘制函数中不应该有while(true)
循环
无耻的自我提升:是一个关于Swing中定制绘画的教程,来自一个处理背景。基本上,您需要创建一个触发重新绘制的计时器。在绘制方法中使用循环是错误的。如果未正确使用图形和绘制,则会阻止UI渲染线程。不要调用getGraphics()
,也不要直接调用paint()
。请参阅以了解正确的方法。我认为在绘制方法中使用循环是错误的。如果未正确使用图形和绘制,则会阻止UI渲染线程。不要调用getGraphics()
,也不要直接调用paint()
。请参阅以了解正确的方法。