JPanel内部的java graphics2D
我试图在JPanel上画一些简单的形状,但遇到了一些困难。如果这个问题以前似乎已经回答过,但其他答案似乎没有帮助,则表示歉意 我遵循了一个简单的教程,并成功地在JFrame上绘制了一些基本形状,但当我将代码移动到一个扩展了JPanel的新类中时,屏幕上什么也没有显示JPanel内部的java graphics2D,java,swing,graphics,jpanel,awt,Java,Swing,Graphics,Jpanel,Awt,我试图在JPanel上画一些简单的形状,但遇到了一些困难。如果这个问题以前似乎已经回答过,但其他答案似乎没有帮助,则表示歉意 我遵循了一个简单的教程,并成功地在JFrame上绘制了一些基本形状,但当我将代码移动到一个扩展了JPanel的新类中时,屏幕上什么也没有显示 public class TestingGraphics extends JFrame{ /** * @param args the command line arguments */ public static void m
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。您忘记设置适当的布局管理器
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。@SanketMakani从主新TestingGraphics()级联而来,在该构造函数中有.add(new TestingPanelGraphics()),然后在TestingPanelGraphics构造函数中有.add(new DrawStuff)@SanketMakani从主要的new TestingGraphics()中级联而来,在该构造函数中有.add(new TestingPanelGraphics()),然后在TestingPanelGraphics构造函数中有.add(new DrawStuff)我很抱歉!我重读了你的答案,并把它在JPanel这一次,它是工作!谢谢你没有必要:)我第一次误读了你的回答,急忙回答。你的解决方案解决了这个问题。感谢您澄清默认布局我很抱歉!我重读了你的答案,并把它在JPanel这一次,它是工作!谢谢你没有必要:)我第一次误读了你的回答,急忙回答。你的解决方案解决了这个问题。感谢您澄清默认布局