![](/img/trans.png)
[英]How can I allow multiple methods to run at the same time, while certain other methods are not running?
[英]How do I run/display mutliple graphics methods at the same time?
我正在使用非常基本的 Java 圖形,並試圖同時顯示多種圖形方法。 換句話說,我想重疊它們。
目前,當它運行時,它會在短時間內顯示一個圖形,然后消失並顯示另一個圖形。
有沒有什么辦法解決這一問題?
這是我的圖形的跑步者。
import javax.swing.JFrame;
public class GraphicsRunnerCopy extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public GraphicsRunnerCopy(int x,int y,int z,int n)
{
super("My Frame");
setSize(WIDTH,HEIGHT);
getContentPane().add(new Graphic(x,y,z,n));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
GraphicsRunnerCopy run = new GraphicsRunnerCopy(3,3,1,20); //I want this to display at
GraphicsRunnerCopy run2 = new GraphicsRunnerCopy(0,0,5,4); //the same time as this
}
}
在跑步者顯示的 object 中,我使用如下代碼。
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Canvas;
class Graphic extends Canvas
{
private int x,y,z,n;
public Graphic(int xa, ya, za, na)
{
x = xa;
y = ya;
z = za;
n = na;
setSize(800,600);
setBackground(Color.WHITE);
setVisible(true);
}
public void paint( Graphics window )
{
Color tan = new Color(185, 133, 91);
window.setColor(tan);
window.fillRect((x*n+20/z), (y*n+20/z), (320/z), (320/z)); //base
}
}
任何幫助表示贊賞,謝謝。
您需要從組件代碼中拆分出邏輯代碼和數據:
public void paint(Graphics g)
方法的 class例如,如果我使用 Swing 執行此操作(因為 AWT 比 Swing 更過時),我會執行以下操作:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class GuiFun extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 600;
private static final Color BACKGROUND = Color.WHITE;
private List<Draw> draws = new ArrayList<>();
public GuiFun() {
setPreferredSize(new Dimension(PREF_W, PREF_H));
setBackground(BACKGROUND);
}
public void addDraw(Draw draw) {
draws.add(draw);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Draw draw : draws) {
draw.myPaint(g);
}
}
private static void createAndShowGui() {
GuiFun guiFun = new GuiFun();
guiFun.addDraw(new Draw(3, 3, 1, 20));
guiFun.addDraw(new Draw(0, 0, 5, 4));
JFrame frame = new JFrame("GuiFun");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(guiFun);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
和
import java.awt.Color;
import java.awt.Graphics;
public class Draw {
private static final Color TAN = new Color(185, 133, 91);
private int x;
private int y;
private int z;
private int n;
public Draw(int x, int y, int z, int n) {
this.x = x;
this.y = y;
this.z = z;
this.n = n;
}
public void myPaint(Graphics g) {
g.setColor(TAN);
g.fillRect((x * n + 20 / z), (y * n + 20 / z), (320 / z), (320 / z));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.