Java图形没有出现

时间:2015-05-30 17:13:13

标签: java swing class

基于我以前的question我已经了解到扩展JFrame和JPanel是不好的。所以我决定抛弃这个想法并学会如何编写一个框架及其组件(我还在学习顺便说一下)。但是,虽然我已经完成了按钮,但我似乎无法显示我的图形,现在我的智慧结束了。

下面是我的第一堂课,其中所有小组都已建立并宣布(如果学期不正确,请原谅我)

import java.awt.Color;
import javax.swing.*;

public class OurUI {
   static Color kaler;
   private JPanel mainpanel = new JPanel();
   private JButton colorbutton1 = new JButton("Green");
   private JButton colorbutton2 = new JButton("Red");
   private JButton colorbutton3 = new JButton("Blue");
   private JPanel graphpanel = new JPanel();
   private JComponent[] GUIcomponents = { colorbutton1, colorbutton2,
         colorbutton3, };

   public OurUI() {
      for (JComponent comp : GUIcomponents) {
         mainpanel.add(comp);
      }
   }

   public JComponent getmaincomponent() {
      return mainpanel;
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            OurGUI();
         }
      });
   }

   private static void OurGUI() {
      OurUI ourUI = new OurUI();
      JFrame guiframe = new JFrame("FUNCTIONAL DEPENDENCY");
      guiframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      guiframe.getContentPane().add(ourUI.getmaincomponent());
      guiframe.pack();// this code is for resizing the components, automatically
                      // so that we don't have to set the size
      guiframe.setLocationByPlatform(true);
      guiframe.setSize(500, 450);
      guiframe.setVisible(true);
   }
}

以下这些行是我的另一个类,“DrawGraph”。我不是很擅长编码,但我确定我没有正确地打电话给班级,只是不知道怎么做

 class DrawGraph
{
            DrawGraph drawgraph = new DrawGraph();
            static boolean lukis = true;   





             int oldY = 0;
             double degToRad (int deg){
            return ((2*Math.PI)/360.0) * deg;
            }

            int scale (int i, int width){
                return (int) ((i/(double)width)*720.0);}


            public void paintGraph(Graphics g){



                g.drawLine(0, 100, 400, 100);//X-axis
                g.drawLine(200, 0, 200, 200);//Y-axis
                g.drawLine(400, 100, 390, 90);//arrow pointing rightwards
                g.drawLine(400, 100, 390, 110);//arrow pointing rightwards
                g.drawLine(200, 0, 190, 10);//arrow pointing upwards
                g.drawLine(200, 0, 210, 10);//arrow pointing upwards

                g.drawString("X", 400, 80);//draw 'X' label
                g.drawString("Y", 220, 10);//draw 'Y' label

                g.drawString("\u03C0/2", 240, 120);//draw ' 1/2 Pi' label
                g.drawString("\u03C0", 290, 120);//draw 'Pi' label
                g.drawString("2\u03C0", 400, 120);//draw ' 2 Pi' label
                g.drawString("3\u03C0/2", 340, 120);//draw ' 3/2 Pi' label


                g.drawString("-\u03C0/2", 140, 120);//draw 'negative 1/2 Pi' label
                g.drawString("-\u03C0", 87, 120);//draw 'negative Pi' label
                g.drawString("-2\u03C0", 0, 120);//draw 'negative 2 Pi' label
                g.drawString("-3\u03C0/2", 30, 120);//draw ' 3/2 Pi' label



                g.drawString("0", 205, 110);//draw '0' label
                g.drawString("0.77", 175, 50);//draw '0.77' label for half Pi
                g.drawString("1", 175, 10);//draw '1' label for a Pi
                g.drawString("-0.77", 170, 150);//draw '0.77' label for half Pi
                g.drawString("-1", 185, 200);//draw '1' label for a Pi


                int width = 400;
                int height = 200;

                for (int i=0; i<width; i++){

                    int y = (int) Math.round((-Math.sin(degToRad(scale(i,width)))+1)*height/2.0);
                    if (lukis && i>0){

                        g.drawLine(i-1, oldY, i, y);
                           } 


                    else {
                        g.drawLine(i, y, i, y);

                          }


                oldY = y;
                }
  }
}

编辑:我认为我已经清理了足够的代码缩进,如果我错了,请纠正我。是的,我犯了一些错误,这些都是不专业的,所以请原谅我。

1 个答案:

答案 0 :(得分:1)

  

我了解到扩展JFrame和JPanel是不好的

但是如果你需要在GUI中绘图,你经常需要扩展JPanel,而这正是你应该在这里做的。例如,使用Graphics对象绘制的代码应该在JPanel的protected void paintComponent(Graphics g)方法中。所以你的DrawGraph类应该扩展JPanel,它应该覆盖paintComponent,在顶部有一个@Override注释,并且应该调用super方法:

public class DrawGraph extends JPanel {
    // .... variables, constructor, methods here...

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // your painting code here
    }

关于,

guiframe.getContentPane().add(DrawGraph.paintComponent());

我不知道你在这里做了什么,但是如你所知,这是错误的,删除那条线,甚至不会在未来接近类似的东西。它几乎就像你试图直接调用组件的paintComponent方法,这是你几乎不想做的事情。

关于:

  

以下这些行是我的另一个类,&#34; DrawGraph&#34;。我不太擅长编码,但我确定我没有正确地打电话给班级,只是不知道如何......

但这正是教程的用途。如果您还没有通过它们,请立即:

相关问题