java 在 Jframe 中画一个圆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31337454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing a circle into a Jframe
提问by Dragongeek
I'm trying to draw a small circle into a gray square JFrame as an indicator of stick positions for an RC remote.
我正在尝试在灰色方形 JFrame 中绘制一个小圆圈,作为 RC 遥控器摇杆位置的指示器。
I've got two classes:
我有两个班级:
public class GUI2 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI2 frame = new GUI2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GUI2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
StickWidget Left = new StickWidget();
Left.setName("Left Stick");
Left.drawCircle(10, 10);
contentPane.add(Left.draw(), BorderLayout.EAST);
StickWidget Right = new StickWidget();
Right.setName("Right Stick");
contentPane.add(Right.draw(), BorderLayout.WEST);
}
}
And I've got a second class:
我有第二堂课:
public class StickWidget extends JPanel {
private javax.swing.JPanel panel_1;
private String NameOfStick;
private int xpos;
private int ypos;
public void initcomponents()
{
panel_1 = new javax.swing.JPanel();
}
public StickWidget() {
//do nothing?
}
public Component draw()
{
initcomponents();
JPanel main = new JPanel();
main.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
Dimension D1 = new Dimension(120, 150);
main.setMinimumSize(D1);
main.setMaximumSize(D1);
main.setPreferredSize(D1);
main.setSize(D1);
main.setLayout(new MigLayout("", "[grow]", "[grow][grow]"));
JPanel panel = new JPanel();
main.add(panel, "cell 0 0,grow");
JLabel StickName = new JLabel(NameOfStick);
panel.add(StickName);
panel_1.setBackground(Color.LIGHT_GRAY);
Dimension d = new Dimension(100, 100);
panel_1.setMinimumSize(d);
panel_1.setMaximumSize(d);
main.add(panel_1, "cell 0 1,grow");
return main;
}
public void drawCircle(int x, int y)
{
Graphics2D g2d = (Graphics2D)panel_1.getGraphics();
g2d.fillOval(x, y, 10, 10);
}
public void setName(String s)
{
NameOfStick = s;
}
}
}
The current program draws the GUI just fine however when I attempt to run the drawCircle() it falls apart and gives me errors. I'm not sure what I've done wrong, I don't have a lot of experience with 2d drawing.
当前程序可以很好地绘制 GUI,但是当我尝试运行 drawCircle() 时它会崩溃并给我错误。我不确定我做错了什么,我对 2d 绘图没有很多经验。
So my questions:
所以我的问题:
-Is this the right approach to this? -How can I get the drawCircle to draw circles within the gray Jframes?
- 这是正确的方法吗?- 如何让 drawCircle 在灰色 Jframes 中绘制圆圈?
Error Message:
错误信息:
java.lang.NullPointerException
at experiments.StickWidget.drawCircle(StickWidget.java:59)
at experiments.GUI2.<init>(GUI2.java:44)
at experiments.GUI2.run(GUI2.java:23)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.awt.EventQueue.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
采纳答案by ControlAltDel
Draw a circle by overridding paint / paintComponent (if using swing)
通过覆盖paint/paintComponent(如果使用swing)绘制一个圆圈
public void paintComponent(Graphics g) {
g.drawOval(0,0,50,50);
}
To draw custom circles, you can use a (Buffered)Image to draw on first
要绘制自定义圆圈,您可以先使用 (Buffered)Image 进行绘制
public class DrawCircles extends JPanel {
Image im = new BufferedImage(...);
public void drawCircle(int x, int y) {
im.getGraphics().drawOval(x,y,50,50); //TODO: should really dispose the Graphics object after drawing the oval
repaint();
}
public void paintComponent(Graphics g) {
g.drawImage(im,0,0,null);
}
}
回答by maskacovnik
To debug the problem try to put code:
要调试问题,请尝试放置代码:
public void drawCircle(int x, int y)
{
if(panel_1==null){
System.err.println("Panel is null");
return;
}
Graphics2D g2d = (Graphics2D)panel_1.getGraphics();
if(g2d==null){
System.err.println("Graphics is null");
return;
}
g2d.fillOval(x, y, 10, 10);
}
I would use Canvas instead of JPanel to paint with Graphics on it.
我会使用 Canvas 而不是 JPanel 在其上绘制图形。