gpt4 book ai didi

JPanel 中的 java graphics2D

转载 作者:行者123 更新时间:2023-11-29 08:36:24 25 4
gpt4 key购买 nike

我试图在 JPanel 上绘制一些简单的形状,但遇到了一些困难。如果这个问题之前似乎已经回答过,但其他答案似乎没有帮助,我们深表歉意。

我遵循了一个简单的教程并成功地将一些基本形状绘制到 JFrame 上,但是当我将代码移动到一个扩展 JPanel 的新类中时,屏幕上没有任何显示。

public class TestingGraphics extends JFrame{

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

new TestingGraphics();

}

public TestingGraphics(){

this.setSize(1000,1000);
this.setPreferredSize(new Dimension(1000,1000));
this.setTitle("Drawing tings");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new TestingPanelGraphics(), BorderLayout.CENTER);
this.setVisible(true);

}

public class TestingPanelGraphics extends JPanel {

public TestingPanelGraphics(){

this.setSize(1000,1000);
this.setPreferredSize(new Dimension(1000,1000));
this.add(new DrawStuff(), BorderLayout.CENTER);
revalidate();
repaint();
this.setVisible(true); //probably not necessary

}

private class DrawStuff extends JComponent{

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);

Graphics2D graph2 = (Graphics2D) g;

graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);



Shape rootRect = new Rectangle2D.Float(100, 100, 1000, 500);

graph2.setColor(Color.BLACK);
graph2.draw(rootRect);
}

我试过设置首选大小,并重新验证和重新绘制。我已经添加了对 super.paintComponent 的调用,尽管当我直接在 JFrame 上绘图时这两个都不是必需的。我确保我覆盖了 paintComponent 并将其从公共(public)更改为 protected 。所有以下来自类似线程的建议,但似乎没有任何效果。我已经在调试器模式下逐步完成并确保它进入正确的方法,甚至看着它进入 paintManager,但窗口上仍然没有任何显示。

最佳答案

您忘记设置适当的布局管理器。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestingGraphics extends JFrame{

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

new TestingGraphics();

}

public TestingGraphics(){

this.setSize(1000,1000);
this.setPreferredSize(new Dimension(1000,1000));
this.setTitle("Drawing tings");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new TestingPanelGraphics(), BorderLayout.CENTER);
this.setVisible(true);

}

public class TestingPanelGraphics extends JPanel {

public TestingPanelGraphics(){
setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(1000,1000));
this.add(new DrawStuff(), BorderLayout.CENTER);
revalidate();
repaint();
this.setVisible(true); //probably not necessary

}

private class DrawStuff extends JComponent{

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);

Graphics2D graph2 = (Graphics2D) g;

graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);



Shape rootRect = new Rectangle2D.Float(100, 100, 1000, 500);

graph2.setColor(Color.BLACK);
graph2.draw(rootRect);
}
}
}
}

当您扩展 JFrame 时,您的默认布局是 BorderLayout,但当您扩展 JPanel 时,您的默认布局是 FlowLayout。

关于JPanel 中的 java graphics2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43843811/

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