gpt4 book ai didi

Java JApplet 渲染问题

转载 作者:行者123 更新时间:2023-12-01 12:56:28 26 4
gpt4 key购买 nike

我在使用 JApplet 时遇到问题。代码工作得很好,但是当我将其从 JFrame 转换为 JApplet 时,渲染部分停止正常工作。基本上我想做的是简单化的绘图应用程序。当启动小程序时,有一半的时间 repaint() 不起作用(没有灰色背景;您必须将鼠标放在按钮上才能更新其颜色等),而且像素渲染部分根本不显示。代码如下:

框架类(JApplet)

  package painter;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Frame extends JApplet {

public JPanel panel;
private JButton plus, minus, buttonColor;
private int scaleSize;
private JLabel labelScale;
private final Timer updateTimer;
private static boolean painting = false;
public static Color currentColor;
public static int mode = 0;
// 0 = draw; 1 = setcolor; 2 = erase

private ArrayList<Pixel> pixelArray;

public Frame() {

pixelArray = new ArrayList<>();
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
pixelArray.add(new Pixel(i, j));
}
}

setLayout(new BorderLayout());
panel = new JPanel() {

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//g.fillRect(10, 10, 100, 100); <- test if fillRect works at all. Yus it does.
for (int i = 0; i < pixelArray.size(); i++) {
pixelArray.get(i).render(g);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}

};

//panel.setBounds(0, 0, 800, 800);
//add(panel);
getContentPane().add(panel);
//panel.setLayout(null);
//panel.setOpaque(true);

//panel.setDoubleBuffered(true);
currentColor = Color.yellow;

buttonColor = new JButton("Choose color");
buttonColor.setBounds(10, 10, 128, 64);
buttonColor.setBackground(currentColor);
buttonColor.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
currentColor = JColorChooser.showDialog(null, "JColorChooser Sample", Color.gray);
}

});

updateTimer = new Timer(20, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
buttonColor.setBackground(currentColor);
repaint();
}

});
updateTimer.start();
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
painting = true;
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
painting = false;
}
});

panel.add(buttonColor);

repaint();
}

public static boolean getPaint() {
return painting;
}

public static void main(String[] args) {
new Frame();
}
}

这是 Pixel 类:

package painter;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;

public class Pixel {

private Color color;
private int size;
private int x, y, relativex, relativey;

public Pixel(int relx, int rely) {
color = new Color(0x999999, false);
size = 32;
x = relx * size + 64;
y = rely * size + 64;

}

public boolean mouseOver() {
Point pos, mousepos;
pos = new Point(x, y);
mousepos = MouseInfo.getPointerInfo().getLocation();
if ((mousepos.x > pos.x)
&& (mousepos.x < pos.x + size)
&& (mousepos.y > pos.y)
&& (mousepos.y < pos.y + size)) {
return true;
} else {
return false;
}
}

public void render(Graphics g) {
g.setColor(color);
if (mouseOver() && Frame.getPaint()) {

if (Frame.mode == 0) {
color = Frame.currentColor;
}
if (Frame.mode == 1) {
Frame.currentColor = color;
}
if (Frame.mode == 2) {
color = new Color(0xffffffff, true);
}
}

g.fillRect(x, y, size, size);
if (mouseOver()) {
g.setColor(Color.black);
g.drawRect(x, y, size - 1, size - 1);
g.setColor(Color.yellow);
g.drawRect(x + 1, y + 1, size - 3, size - 3);
}
//g.fillRect(10, 10, 250, 250);
}

}

最佳答案

作为一种摸索,不要在您未明确创建的 Graphics 上下文上调用 Graphics#dipose

除了Graphics上下文是一个共享资源,由可能需要在给定绘制周期内绘制的所有组件使用这一事实之外,它还可以阻止向其绘制的内容将在部分平台展示

在 15 年的专业开发生涯中,我从来没有理由调用 Toolkit.getDefaultToolkit().sync();。我怀疑这会产生那么大的影响,我只是说

关于Java JApplet 渲染问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23843071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com