Java,形状不是绘图



我的程序试图使用Java JFrame和FPanel绘制东西时遇到了问题,我还查看了模拟fashio(不是用Java绘制的形状)中标记的另一个问题,但从这个问题中我无法确定我的程序出了什么问题。所以现在,即使这在某种意义上是一份副本,我也在寻求帮助,指出我的错误。我也在使用netbeans作为媒介。

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Font;

public class BullsEye extends JPanel{
@Override
   public void printComponent(Graphics g)
   {
   super.paintComponent(g);
   //for(int x =0; x>10;x++)
   //{
   int x=10;
       int y =(100-10*(x-1));
       //if((x%2)==0)
       //{
           g.setColor(Color.RED);//setting color
           g.drawRect(100, 10, 10, 15);//drawing
            g.drawOval(0, 0, 100, 100);//drawing
       //}
      // else
       //{
           g.setColor(Color.GREEN);//setting color
            g.fillOval(10, 10, 50, 50);//drawing
       //}
   //}

   }
    public static void main(String[] args) 
    {
        BullsEye b =  new BullsEye();//creating b varaible for drawings
     JFrame jf =  new JFrame();//frame varaible for the frame
     jf.setTitle("BullsEye");//setting title
     jf.setSize(500,400);//setting size
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//opertion close
     jf.add(b);//adding to the frame
     jf.setVisible(true);//setting it to visible
    }
}

您覆盖了错误的方法。覆盖paintComponent而不是printComponent,因为它仅用于打印。了解更多信息的好教程:https://docs.oracle.com/javase/tutorial/uiswing/painting/

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Font;

public class BullsEye extends JPanel{
@Override
   public void paintComponent(Graphics g)
   {
   super.paintComponent(g);
   //for(int x =0; x>10;x++)
   //{
   int x=10;
       int y =(100-10*(x-1));
       //if((x%2)==0)
       //{
           g.setColor(Color.RED);//setting color
           g.drawRect(100, 10, 10, 15);//drawing
            g.drawOval(0, 0, 100, 100);//drawing
       //}
      // else
       //{
           g.setColor(Color.GREEN);//setting color
            g.fillOval(10, 10, 50, 50);//drawing
       //}
   //}

   }
    public static void main(String[] args) 
    {
        BullsEye b =  new BullsEye();//creating b varaible for drawings
     JFrame jf =  new JFrame();//frame varaible for the frame
     jf.setTitle("BullsEye");//setting title
     jf.setSize(500,400);//setting size
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//opertion close
     jf.add(b);//adding to the frame
     jf.setVisible(true);//setting it to visible
    }
}

相关内容

  • 没有找到相关文章

最新更新