Results 1 to 7 of 7
  1. #1
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default Minor AWT drawing issue

    I was trying to get the basics of drawing in a JFrame down before I moved on to creating a small bouncing ball program. I kinda gave up halfway through and copied (although I understand the code too) most of the following code from the website at the top, which had the same issue in their code when I ran their program:


    Java Code:
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.geom.*;
    import javax.swing.*;
    
    //mostly borrowed from http://www.java2s.com/Code/Java/2D-Graphics-GUI/Drawline.htm
    
    public class Draw extends JApplet {
    
        public void init() {
            setBackground(Color.GRAY);
            setForeground(Color.GRAY);
        }
        int x = 25;
        int y = 25;
    
        @Override
        public void paint(Graphics g1) {
            Graphics2D g = (Graphics2D) g1;
            //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            //RenderingHints.VALUE_ANTIALIAS_ON);
            g.setPaint(Color.BLACK);
            g.drawString("Line2D", x, 250);
            g.draw(new Line2D.Double(x, y, 200, 200));
            Ellipse2D e = new Ellipse2D.Double(30, 70, 100, 100);
            g.draw(e);
            g.fill(e);
            g.drawString("HELLO!!!", 100, 100);
    
        }
    
        public static void main(String[] args) {
            JFrame win = new JFrame("Bouncing Ball Demo");
            /**
            win.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
            System.exit(0);
            }
            });
             **/
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
            JApplet applet = new Draw();
            win.getContentPane().add("Center", applet);
            applet.init();
            win.pack();     //helpful to give user a workable/smallish sized window
            win.setSize(600, 400);
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //win.add(pan);
            win.setVisible(true);
        }
    }

    I am having an issue with the JFrame appearing transparent temporarily, and the background of the line, circle, and text is whatever is directly behind the JFrame when it first runs. I assume this may have to do with default opacity, but I'm not very familiar with graphics in Java. Is there a way to give the JFrame a plain white/gray background? And are there any comments on whether I will be able to use Ellipse2D and a timer (and perhaps the repaint method) to create a JFrame with a moving ball/circle?

    I'm still new here in figuring out my way, so comments and suggestions are always appreciated!

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Applets and frames are both top level containers and I don't like that design style of trying to add a JApplet to a JFrame.

    Instead custom painting should be done on a JPanel (or JComponent). Then you add the panel to the frame or the applet. To learn custom painting I suggest you start with the Swing tutorial:

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default

    Interesting problem.
    When I run it with: D:\Java\jdk1.6.0_02\bin\java.exe -Xmx512M -classpath D:\JavaDevelopment;. DrawTesting
    The background is picked up in the frame's background.
    When I run it with: "C:\Program Files\Java\j2re1.4.2_08\bin\java.exe" -cp D:\JavaDevelopment;.;acm.jar DrawTesting
    The background is gray.


    If I change the code:

    public class DrawTesting extends JPanel { //JApplet {
    and

    DrawTesting applet = new DrawTesting();

    I get a gray background with D:\Java\jdk1.6.0_02\bin\java.exe -Xmx512M -classpath D:\JavaDevelopment;. DrawTesting

    On closer observation, I added: super.paint(g1); // clear background before drawing
    to the paint method and it works.
    Last edited by Norm; 06-01-2011 at 11:32 PM.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Then general rule is never override the paint() method, which is why the Swing tutorial should be read.

    The second general rule is that if you do override paint() then you should always invoke super.paint() or if you override paintComponent() then you should always invoke super.paiintComponent() so you don't lose the default background painting functionality.

    However, this is just a simple fix and the proper solution is to do the custom painting on a panel as I first suggested.
    Last edited by camickr; 06-01-2011 at 11:58 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default

    Bit tricky here as the OP has extended JApplet which does not have a paintComponent method.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Bit tricky here as the OP has extended JApplet which does not have a paintComponent method.
    Hope my update is easer to understand.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default

    yes, you were recommending the right way. Extending JApplet is non-sense.

Similar Threads

  1. Some minor doubts about JAVA
    By naveenpaul1987 in forum New To Java
    Replies: 9
    Last Post: 03-16-2011, 11:53 AM
  2. Minor of a matrix (LinkedListArray)
    By sehudson in forum New To Java
    Replies: 1
    Last Post: 03-08-2011, 07:27 AM
  3. Minor of a matrix
    By sehudson in forum New To Java
    Replies: 3
    Last Post: 02-21-2011, 08:12 PM
  4. Drawing an arc
    By berkeleybross in forum Java 2D
    Replies: 10
    Last Post: 12-09-2010, 01:32 AM
  5. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 02:23 AM

Posting Permissions

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