- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的绘制方法似乎无法绘制我的 20x20 单元格。我有一个用于单元格控制其状态的 boolean 数组,如果为 true,则调用单元格绘制方法,单元格将被绘制,但是我有两个问题;
一次只绘制一个,这很奇怪,因为我应该有一个 40x40 的 boolean 数组,这意味着我有 40x40 的单元格
它们实际上并没有准确地绘制我点击的位置。我不知道情况如何,因为当我获得点击的坐标时,我立即将这些坐标作为我的绘制方法中的 x 和 y 值。
主要
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
public class mainApplication extends JFrame implements Runnable, MouseListener {
private static final Dimension windowsize = new Dimension(80, 600);
private BufferStrategy strategy;
private Graphics offscreenGraphics;
private static boolean isGraphicsInitialised = false;
private static int rows = 40;
private static int columns = 40;
private static int height = windowsize.height;
private static int width = windowsize.width;
private static Cells cells = new Cells();
private int xArrayElement,yArrayElement, xPosition, yPosition;
private static boolean gameState[][] = new boolean[rows][columns];
public mainApplication() {
System.out.println(System.getProperty("user.dir"));
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width / 2 - windowsize.width / 2;
int y = screensize.height / 2 - windowsize.height / 2;
setBounds(x, y, screensize.width, screensize.height);
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
offscreenGraphics = strategy.getDrawGraphics();
isGraphicsInitialised = true;
// MouseEvent mouseEvent = new MouseEvent();
addMouseListener(this);
// addMouseMotionListener(MouseEvent);
Thread t = new Thread(this);
t.start();
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 1){
xPosition = e.getX();
yPosition = e.getY();
cells.setPosition(xPosition,yPosition);
xArrayElement = (xPosition/20);
yArrayElement = (yPosition/20);
if(gameState[xArrayElement][yArrayElement]){
gameState[xArrayElement][yArrayElement] = false;
}
else if (!gameState[xArrayElement][yArrayElement]) {
gameState[xArrayElement][yArrayElement] = true;
}
else(gameState[xArrayElement][yArrayElement]) = true;
}
}
@Override
public void run() {
while (true) {
try { //threads entry point
Thread.sleep(20); //forces us to catch exception
}
catch (InterruptedException e) {
}
this.repaint();
}
}
public void paint(Graphics g) {
if (isGraphicsInitialised) {
g = strategy.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 800);
if (gameState[xArrayElement][yArrayElement]) {
g.setColor(Color.WHITE);
cells.paint(g);
System.out.println(xPosition);
}
else if (!gameState[xArrayElement][yArrayElement]) {
g.setColor(Color.BLACK);
g.fillRect(xPosition, yPosition, 20, 20);
}
strategy.show();
}
}
public static void main(String[]args){
mainApplication test = new mainApplication();
}
}
细胞类别
import java.awt.*;
public class Cells {
int x;
int y;
public Cells(){
}
public void setPosition(int xi, int xj){
x = xi;
y = xi;
}
public boolean cellState(boolean visible){
return visible;
}
public void paint(Graphics g){
g.drawRect(x, y, 20,20);
}
}
最佳答案
你做错了很多事情。我的第一个建议是忘记屏幕外图形并确保您正在做您想做的事情。您始终可以稍后创建图像。以下是一些基本准则:
JFrame
。使用实例。JPanel
或创建一个扩展 JPanel
的类并添加到框架实例paintComponent(g)
并使用该图形上下文进行绘制。这是一个早期的答案,可能会有所帮助 Can't add Graphics into JPanel in Java
更多信息可以在 Java Tutorials 中找到。关于绘画。
已更新。我花了几分钟才找到这个。
public void setPosition(int xi, int xj){
x = xi;
y = xi; // <--- should be xj
}
关于上面的(1)。每次输入 paintComponent
时都必须重新绘制每个单元格。这意味着您需要遍历列表并将它们绘制在正确的位置。现在您只需在每个条目上绘制一个。
还有一些建议。为什么不在 mouseClicked()
方法中调用 repaint
,而不是扰乱线程并在循环中每 20 毫秒调用一次 repaint 。
如果您最终需要每 20 毫秒绘制一次。我建议使用 swing Timer
如下:(检查 JavaDoc 以确保语法正确!!)
Timer timer = new Timer(0, (ev)-> frame.repaint());
timer.setDelay(20);
timer.start();
您还可以创建自己的 mouseListener
类并扩展 MouseAdapter
。这些适配器类的目的是保持困惑,这样您就不必使用空方法来满足接口(interface)要求。将类放在主类中,以便它可以访问适当的数据结构。然后只需将其实例添加到目标组件的鼠标监听器即可。
关于java - Paint方法不画Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60567740/
我想以 headless 模式(屏幕上根本没有 GUI)将 JPanel 绘制到 BufferedImage 中。 final JPanel panel = createPanel(); panel.
我是 Canvas 的新手,正在尝试创建看起来逼真的 float 粒子动画。 目前,我正在创建 400 个随机散布在 400x400 Canvas 上的粒子。 然后,在每个 requestAnimat
有没有办法在悬停时停止悬 float 画? :hover 这是一个显示动画的链接: https://codepen.io/youbiteme/pen/RprPrN 最佳答案 只需为您的 svg 悬停添
我想在谷歌地图上绘制覆盖图,其中除了特定点周围 1.5 公里半径以外的所有内容都被遮蔽了。为此,我尝试使用带有大量边框的圆圈,所以我会在边框中放置透明中心和覆盖颜色来实现这一点,但它无法渲染。
我正在尝试通过扩展类 UIView 来创建自定义 View ,该类可以在自定义 View 的中心显示一个圆圈。为了添加自定义绘图,我重写了 draw(_ rect: CGRect) 方法,如下所示。
我是一名优秀的程序员,十分优秀!