Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Graphics2D.draw(形状s)是否能够在JComponent上绘制?_Java_Swing - Fatal编程技术网

Java Graphics2D.draw(形状s)是否能够在JComponent上绘制?

Java Graphics2D.draw(形状s)是否能够在JComponent上绘制?,java,swing,Java,Swing,我使用Java1.7SDK在JPanel中绘制矩形、圆弧和椭圆 以下代码似乎不起作用: Graphics2D g = (Graphics2D) g1; Rectangle area = getBounds(); Arc2D.Double shape = new Arc2D.Double(area.x,area.y, area.width, area.height, 0, 3.12, Arc2D.PIE); g.draw(shape); 我检查了g.draw(shape)

我使用Java1.7SDK在JPanel中绘制矩形、圆弧和椭圆

以下代码似乎不起作用:

Graphics2D g = (Graphics2D) g1; 

Rectangle area = getBounds(); 
Arc2D.Double shape = new Arc2D.Double(area.x,area.y, area.width,
        area.height, 0, 3.12, Arc2D.PIE); 
g.draw(shape);
我检查了g.draw(shape)的文档,它是作为
抽象方法实现的。此外,我还看到g.fillArc也是作为抽象方法实现的。在JDK1.7中,这些方法有效吗

请参见以下示例:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class GraphicsTest {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GraphicsTest window = new GraphicsTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public GraphicsTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));

        GraphicsPanel panel = new GraphicsPanel();
        frame.getContentPane().add(panel);

        //obtain value of triggering from somewhere code here! 
        String eValue = JOptionPane.showInputDialog(this, "Enter the value"); 
        panel.triggerGraphing(new Integer(eValue).intValue()); 
    }

    public class GraphicsPanel extends JPanel
    {
        int graphType; 

        public GraphicsPanel()
        {
            graphType = 0; 
        }


        void triggerGraphing(int g)
        {
            graphType = g; 
            repaint(); 
        }

        public void paintComponent(Graphics g1)
        {
            super.paintComponent(g1);
            Dimension d = this.getSize();
            Graphics2D g = (Graphics2D) g1;

            switch(graphType){
            case 1:
                drawRect(g, d.width, d.height); 
                break; 
            case 2:
                drawPie(g, d.width, d.height); 
                break;
            }

        }

        void drawRect(Graphics2D g, int w, int h) {
            g.setColor(java.awt.Color.RED); 
            g.fillRect(0,0,w,h); 

        }

        void drawPie(Graphics2D g, int w, int h) {
            g.setColor(java.awt.Color.BLUE); 

            //Arc2D.Double arc = new Arc2D.Double(0,0,w,h,1,30,Arc2D.PIE); 
            g.fillArc(0,0,w,h,1,30);
            //g.draw(arc); 
        }
    }

}

在社区的帮助下,上面的代码终于起作用了。getGraphics()调用似乎非常微妙,需要仔细调用。此外,在原始代码中,
area.x
area.y
是可疑元素,在使用时会扰乱输出。目前还不清楚为什么会发生这种情况

这些方法确实有效

我怀疑您的问题是
getBounds()
调用。如果返回组件的边界,则应忽略
x
y
,因为组件图形上下文的坐标系已经转换,因此(0,0)是组件的左上角。因此,使用0表示圆弧的x和y:

Arc2D.Double shape = new Arc2D.Double(0, 0, area.width, area.height,
    0, 3.12, Arc2D.PIE);

还要确保图形对象设置了可见的颜色和笔划。

尝试时发生了什么?没有错误!只是普通的空白面板!这意味着你可能做错了什么。您完全可以在JComponent上使用这些方法。抽象方法必须有一个实现,否则您将无法使用该类。使用“g”变量引用的对象是实现这些函数的Graphics2D的子类。在相同的方法中,(我将很快粘贴工作示例),我使用的是条形图。杆的绘制正确!如果我的语言正确,您的回答意味着fillarc方法尚未实现?这与我所说的相反。