1

I made a post earlier that was not clear! Here is the code. It is a sample code I am testing to see if it works on my ubuntu!

// using drawLine to connect the corners of s panel

import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawPanel extends JPanel
{// draws an x from the corners of the panel
    public void paintComponent( Graphics g )
    {
    // calls paintComponent to ensure the panel displys correctly
    super.paintComponent( g );
    int width = getWidth();
    int height = getHeight();
    // draw line from the upper-left to lower-right corners
    g.drawLine( 0, 0, width, height );
    // draw line from then upper-right to the lower-left corners
    g.drawLine( width, 0, 0, height);
    System.out.println( "paintComponent Called");
    } // end method paintComponent
} // end class DrawPanel

And the driver code:

// application to display a DrawPanel
import javax.swing.JFrame;

public class DrawPanelTest
{
    public static void main(String[] args)
    {
    //create a panel that contains our drawing
    DrawPanel panel = new DrawPanel();
    // create a new frame to hold the panel
    JFrame application = new JFrame("Drawing Diagonals");
    // set the frame to exit when closed
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.add(panel); // adds panel to the frame
    application.setSize(300, 300); // set the size 
    }
}

When I compile it and run it, nothing shows! It is supposed to pop up a window and display the graphics !!

It works on other systems but not on my ubuntu! What could be wrong?

Thanks

6
  • 1
    I think this question is more suited for StackOverflow
    – AlexGreg
    Sep 15, 2015 at 15:15
  • 1
    @AlexGreg, it seems ok, as it works in other OS but not in Ubuntu. So it's a problem which is related to Ubuntu.
    – user.dz
    Sep 15, 2015 at 15:26
  • Kwasi, Please don't replicate posts if they got closed or no answer. You can edit them. You have 3 now.
    – user.dz
    Sep 15, 2015 at 15:30
  • @AlexGreg, I forget to mention, I got that from his other replicated posts. You could make a look on his profile.
    – user.dz
    Sep 15, 2015 at 15:54
  • 2
    I made a post earlier that was not clear! @Kwasi, next time edit your post instead of adding a new one ;-)
    – AlexGreg
    Sep 16, 2015 at 7:12

1 Answer 1

2

Add application.setVisible(true); in your class DrawPanelTest

public class DrawPanelTest {
    public static void main(String[] args) {
        // create a panel that contains our drawing
        DrawPanel panel = new DrawPanel();
        // create a new frame to hold the panel
        JFrame application = new JFrame("Drawing Diagonals");
        // set the frame to exit when closed
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel); // adds panel to the frame
        application.setSize(300, 300); // set the size
        application.setVisible(true);
    }
}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .