Results 1 to 5 of 5
  1. #1
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

    Default java graphics within main method

    I'm building an application (NOT an applet), and I was wondering if it is possible to draw graphics to the screen (like a line or a rectangle) within the main method (public static void main(String [] args)). Is this possible?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If this is a Swing application, yes, it can be done, but within a JComponent's (such as a JPanel's) paintComponent method.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For example:

    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import javax.swing.*;
    
    public class DrawInMain {
    
      public static final int SIDE = 300;
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            JPanel panel = new JPanel() {
    
              protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                int w = getWidth();
                int h = getHeight();
                if (w > 10 && h > 10) {
                  g.drawLine(10, 10, w - 10, h - 10);
                }
              }
            };
            panel.setPreferredSize(new Dimension(SIDE, SIDE));
            JFrame frame = new JFrame("DrawInMain Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
          }
        });
    
      }
    }
    But I'm not sure why you'd want to do this as it's not very OOPS-like.

  4. #4
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

    Default

    I'm working on a part of an application that takes user input, and outputs a rectangle based on the input that the user gives it

  5. #5
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Help with main method
    By eliCanzee in forum JDBC
    Replies: 4
    Last Post: 01-06-2010, 09:12 AM
  2. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  3. Where do I put main method?
    By jerryrice80 in forum New To Java
    Replies: 4
    Last Post: 09-16-2009, 08:23 PM
  4. main method
    By eva in forum New To Java
    Replies: 5
    Last Post: 12-19-2007, 09:25 AM
  5. The main method in java...
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •