图形程序适用于Windows,但不适用于Mac



我用swing用java编写了这个程序。每次调用repaint()时,当我在Mac上运行此程序时,会生成50个新的蓝色点,并擦除旧的蓝色点。我一直在做很多研究,试图解决这个问题,但我运气不好。今天在我的计算机科学课上,我发现这个程序在教室里的windows电脑上运行。我的问题是,为什么会出现这种情况,我如何解决这个问题,使程序在我的Mac上运行?顺便说一句,我对在java中使用swing还比较陌生,所以我想知道我是否正确地组织了一切,是否可以做一些不同的事情?

这是我在JPanel中绘制所有内容的类。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Canvas;
import java.lang.Math;
import java.awt.event.*;
import javax.swing.*;
public class BarnsleyFern extends JPanel 
{
private double newX,x=0;
private double newY,y=0;
public BarnsleyFern()
{  
ActionListener action = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
repaint();
}
};
Timer timer = new Timer(100,action);
//timer.start();
MouseListener mouse = new MouseListener()
{
public void mouseClicked(MouseEvent event)
{
//repaint();
}
public void mousePressed(MouseEvent event)
{
}
public void mouseReleased(MouseEvent event)
{
}
public void mouseEntered(MouseEvent event)
{
}
public void mouseExited(MouseEvent event)
{
} 
};
MouseMotionListener mouseMotion = new MouseMotionListener()
{
public void mouseDragged(MouseEvent event)   
{
repaint();
}
public void mouseMoved(MouseEvent event)
{
repaint();
}
};   
addMouseListener(mouse);
addMouseMotionListener(mouseMotion);
}
public void paintComponent(Graphics window)
{
Graphics2D g2d = (Graphics2D)window;
g2d.translate(360,800);
fern(window);
}
public void fern(Graphics window)
{
Color newColor = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
for(int i=0;i<50;i++)
{
window.setColor(Color.BLUE);
int rand = (int)(Math.random()*100);
if(rand<1)
{
newX=0;
newY=0.16*y;
}
else if(rand<86)
{
newX=0.85*x + 0.04*y;
newY=0.85*y - 0.04*x + 1.6;
}  
else if(rand<93)
{
newX=0.20*x - 0.26*y;
newY=0.23*x + 0.22*y + 1.6;
}
else
{
newX=0.28*y - 0.15*x;
newY=0.26*x + 0.24*y + 0.44;
}
window.fillOval((int)(newX*165.364),-(int)(newY*80.014),2,2);   
x=newX;
y=newY;
}
}
}

这是设置JFrame并添加JPanel的类。

import javax.swing.*;
import java.awt.*; 
public class BarnsleyFernRunner 
{
public BarnsleyFernRunner()
{
JFrame frame = new JFrame();
frame.setTitle("Barnsley Fern");
frame.setSize(800,800);
frame.setLocation(300,0);
frame.setResizable(false);
frame.setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
BarnsleyFern panel= new BarnsleyFern();
panel.setSize(800,800);
panel.setOpaque(true);
panel.setBackground(Color.BLACK);
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args)
{  
BarnsleyFernRunner runner = new BarnsleyFernRunner();
}
}

"主要"问题是,您正在打破油漆链要求。。。

public void paintComponent(Graphics window)
{
Graphics2D g2d = (Graphics2D)window;
g2d.translate(360,800);
fern(window);
}

paintComponentsuper实现会做一些事情,一些重要的事情,除非你准备承担这个责任,否则你应该确保你首先调用super.paintComponent

你的问题没有帮助…

frame.setSize(800,800);
frame.setLocation(300,0);
frame.setResizable(false);
frame.setLayout(null);
panel.setSize(800,800);
panel.setOpaque(true);

你应该保留默认的BorderLayout,它会让你的生活简单很多。

只需更新BarnsleyFern以覆盖getPreferredSize,您就可以获得更灵活的解决方案。这意味着pack窗口和可用的内容大小将是内容的首选大小,与窗口的大小减去框架装饰相当。

public class BarnsleyFern extends JPanel {
//...
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}

基于。。。

ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent event) {
repaint();
}
};
Timer timer = new Timer(100, action);
//timer.start();

我怀疑你希望得到一种复合涂料,其中x/y特性在每个涂料循环中都会更新。

好吧,这是行不通的,原因有很多。你找到了一个。Graphics是一个共享资源,在给定的绘制周期中绘制的每个组件都将使用相同的Graphics实例,这意味着你最终可能会使用脏的绘制(作为副作用,这是你似乎在寻找的结果),但正如你所发现的,它并不总是有效的。

绘画也可能由于多种原因而发生,其中许多原因都是在你无法控制或不知情的情况下发生的。

绘画应该绘制当前状态,而不应该修改它。这就是Timer应该做的。

您需要设计一个模型,允许每次Timer滴答作响时进行增量更改,然后组件将使用该模型简单地绘制当前状态

我建议阅读:

  • 执行自定义绘画
  • AWT和摆动涂装
  • 在容器中布置零部件

了解更多详细信息

最新更新