我想画简单的椭圆形。在以下代码中,如果我在Jpanel上添加一个椭圆形,则它不起作用,但是如果我在框架上绘制,则可以正常工作。以下是有效的代码。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Controller {
public Controller() {
View view = new View();
Model model = new Model(100, 100, 100, 100);
view.paintModel(model);
}
public static void main(String[] args) {
Controller c = new Controller();
}
public class View extends JFrame {
private JPanel jpanel;
public View() {
this.setBounds(0, 0, 500, 500);
this.setLayout(new BorderLayout());
jpanel = new JPanel();
jpanel.setBackground(Color.WHITE);
jpanel.setLayout(null);
this.add(jpanel, BorderLayout.CENTER);
this.setVisible(true);
this.validate();
}
public void paintModel(Model model) {
jpanel.add(new ModelView(model));
jpanel.validate();
jpanel.repaint();
}
}
public class Model {
public int xPos;
public int yPos;
public int height;
public int width;
public Model(int xPos, int yPos, int height, int width) {
super();
this.xPos = xPos;
this.yPos = yPos;
this.height = height;
this.width = width;
}
}
public class ModelView extends JComponent {
private Model model;
public ModelView(Model model) {
super();
setBorder(new LineBorder(Color.RED));
this.model = model;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(model.xPos + model.width, model.yPos
+ model.height);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillOval(model.xPos, model.yPos, model.width, model.height);
}
}
}
最佳答案
首先,您没有在super.paintComponent
类中调用ModelView
,这非常重要,尤其是在处理诸如JComponent
之类的透明组件时
其次,您没有提供ModelView
的布局信息,也就是说,该组件没有首选,最小或最大大小提示,因此许多布局管理器会认为它的大小为0x0
,即这里发生了什么。JPanel
默认情况下使用FlowLayout
,它将使用组件的首选大小进行布局。
更新ModelView
以提供一些大小调整提示(在大多数情况下,覆盖getPreferredSize
应该就足够了),或者在要添加JPanel
的ModelView
上使用其他布局管理器,而不必在意这些事情(例如BorderLayout
)
更新
此外,您“可能”正在绘画超出组件的可视范围,例如,使用Model model = new Model(100, 100, 100, 100)
创建模型,然后使用g.fillOval(model.xPos, model.yPos, model.width, model.height)
渲染该模型,但是组件的preferredSize
仅为,实际上是在组件的右下边缘(外部)绘制椭圆形
除非打算编写自己的布局管理器来进行管理,否则在计算组件的首选大小时应考虑x / y偏移,例如new Dimension(model.width, model.height)
例如...抱歉,我为您的代码添加了一些代码以填充模型...
红线只是new Dimension(model.xPos + model.width, model.yPos + model.height)
,它显示LineBorder
组件的边界
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class View extends JFrame {
private JPanel jpanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
new View();
}
});
}
public View() {
this.setBounds(0, 0, 500, 500);
this.setLayout(new BorderLayout());
jpanel = new JPanel();
jpanel.setBackground(Color.WHITE);
this.add(jpanel, BorderLayout.CENTER);
Model model = new Model(100, 100, 100, 100);
paintModel(model);
this.setVisible(true);
}
public void paintModel(Model model) {
jpanel.add(new ModelView(model));
jpanel.validate();
jpanel.repaint();
}
public class ModelView extends JComponent {
private Model model;
public ModelView(Model model) {
super();
setBorder(new LineBorder(Color.RED));
this.model = model;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(model.xPos + model.width, model.yPos + model.height);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillOval(model.xPos, model.yPos, model.width, model.height);
}
}
public class Model {
public int xPos;
public int yPos;
public int height;
public int width;
public Model(int xPos, int yPos, int height, int width) {
super();
this.xPos = xPos;
this.yPos = yPos;
this.height = height;
this.width = width;
}
}
}