Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaApplet extra circles printing on screen

Tags:

java

applet

I am new in this. I am trying to draw a circle in a javaApplet but somehow in the output it shows 3 circles. Any idea?

enter image description here

import javax.swing.JApplet;
import java.util.*;
import java.awt.*;

public class Shapes extends JApplet
{
    public void paint (Graphics page)
    {
        resize(400,300);
        Random rand = new Random();

        // Declare size constants
        final int circleMax = 160,circleMin = 40; // circle max and min diameter
        final int locMaxX = 360, locMaxY = 260;
        int radiusSize = 0, locationx = 0,locationy = 0 ;

        // Declare variables
        radiusSize = (rand.nextInt(circleMax)+ circleMin); 
        locationx =20 ;//rand.nextInt(locMaxX)+ 20;
        locationy =20 ;// rand.nextInt(locMaxY) + 20;

        // Draw the circle 1
        page.drawOval(locationx, locationy, radiusSize,radiusSize);
    }
}
like image 531
Meeeeee Avatar asked Apr 10 '26 04:04

Meeeeee


2 Answers

Your main problems are that you're calling resize(...) within a painting method and are not calling the super's painting method. Having said that, my recommendations are:

  • Never draw within a top level window's (such as a JApplet or JFrame's) paint method.
  • Instead draw within the paintComponent method of a JPanel that is displayed within the top-level window.
  • Call the super's method inside of your painting method, usually first.

For example

import javax.swing.JApplet;
import java.util.*;
import java.awt.*;

public class Shapes extends JApplet {

    @Override
    public void init() {
        add(new ShapesPanel());
    }

}    

class ShapesPanel extends JPanel {
    private Random rand = new Random();
    // Declare size constants
    final int circleMax = 160,circleMin = 40; // circle max and min diameter
    final int locMaxX = 360, locMaxY = 260;
    int radiusSize = 0, locationx = 0,locationy = 0 ;

    public ShapesPanel() {
        radiusSize = (rand.nextInt(circleMax)+ circleMin); 
        locationx =20 ;//rand.nextInt(locMaxX)+ 20;
        locationy =20 ;// rand.nextInt(locMaxY) + 20;
    }

    @Override
    protected void paintComponent (Graphics page)   {
        super.paintComponent(page);
        // Draw the circle 1
        page.drawOval(locationx, locationy, radiusSize,radiusSize);
    }
}
like image 65
Hovercraft Full Of Eels Avatar answered Apr 12 '26 17:04

Hovercraft Full Of Eels


The Graphics context which is passed to your component is a shared resource, this means that it will contain what ever was previously painted to it.

By failing to call super.paint before you do any custom painting, you've prevented the applet from performing many of critical tasks it's designed for (one of which is to fill the Graphics context with the background color).

Take a look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing and AWT.

Now, you could simply call super.paint, but it's generally discouraged to override the paint method of a top level container like JApplet, it actually contains a JRootPane, which contains a contentPane which can cause issues in the painting process.

A better solution is to start with a JPanel, override it's paintComponent method (calling super.paintComponent first) and perform your custom painting there. Then you can add this to what ever container you want

You should also avoid calling any method which may result in a repaint request been made from with a paint method, this will cause a never ending loop of repaints, which will consume your CPU cycles and bring your system to it's knees

For example...

Applets

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import javax.swing.JApplet;
import javax.swing.JPanel;
import test.Shapes.TestPane;

public class Shapes extends JApplet {

    @Override
    public void init() {
        super.init();
        add(new TestPane());
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            Random rand = new Random();

            // Declare size constants
            final int circleMax = 160, circleMin = 40; // circle max and min diameter
            final int locMaxX = 360, locMaxY = 260;
            int radiusSize = 0, locationx = 0, locationy = 0;

            // Declare variables
            radiusSize = (rand.nextInt(circleMax) + circleMin);
            locationx = 20;//rand.nextInt(locMaxX)+ 20;
            locationy = 20;// rand.nextInt(locMaxY) + 20;

            // Draw the circle 1
            g2d.drawOval(locationx, locationy, radiusSize, radiusSize);
            g2d.dispose();
        }

    }
}

I'd also question then use of JApplet at all now days, considering the almost all browsers are actively disabling them and the complexities that the bring in their development

like image 23
MadProgrammer Avatar answered Apr 12 '26 17:04

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!