0

I am working on a funtion plotter and am looking towards painting 2 lines which shall represent the x and y axis. Would you be so kind and review my code and possibly tell me what is wrong! Thank you! :) This thing funktionsschar is irrelevant to my question.

public class AUSGABE_GUI extends JFrame {
  // Anfang Attribute
  public static final int WIDTH = 1024;
  public static final int HEIGHT = 768;
  Funktionsschar funktionsschar;
  
  private JPanel jPanel1 = new JPanel(null);
  // Ende Attribute
  
  public AUSGABE_GUI(Funktionsschar pFunktionsschar) { 
    super ("Plotter");
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(WIDTH, HEIGHT);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    Container cp = getContentPane();
    cp.setLayout(null);
    
    jPanel1.setBounds(0, 0, WIDTH, HEIGHT);
    cp.add(jPanel1);
    
    setVisible(true);
  } 
  public void paintComponents(Graphics g) {
    super.paintComponents(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.blue);
    g2d.drawLine(0, HEIGHT / 2, WIDTH, HEIGHT / 2);
    g2d.drawLine(WIDTH / 2, 0, WIDTH / 2, HEIGHT);
  }

Also i am really sorry if my code looks terrbile i am NEW to coding!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    You're not in fact drawing within a paintComponent method override, or drawing within a JPanel (which is where you should be drawing). You instead will want to draw in the paintComponent method of a JPanel not the paintComponents method of a JFrame (or any component). Avoid guessing and instead read the tutorials: [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html). – Hovercraft Full Of Eels Dec 19 '20 at 17:36
  • Also, this is not good: `cp.setLayout(null);` Instead learn and use the layout managers. – Hovercraft Full Of Eels Dec 19 '20 at 17:38
  • 3
    *...i am NEW to coding!* If you are new to coding you should not be trying to do graphics. Doing graphics well depends on understanding the basic aspects of Java. These would include classes, generics, lists, sets,maps, arrays, types (int, long, etc) and their wrapper classes (Integer, Long, etc), and many, many other parts of the language. – WJS Dec 19 '20 at 18:00
  • 1
    You're welcome. BTW, don't get discouraged by my comment. It's excellent that you're learning Java. Just keep at it and try learning the basics of the language first. It will make your eventual graphics development easier and more enjoyable. – WJS Dec 19 '20 at 18:59

0 Answers0