Hi guys, I'm really new to graphics and im trying to pick it up but struggling with a few points. Can someone provide some code on how i can set where a graphics shape should be created/spawn? eg like (32, 11) 32 pixels on the horizontal axis and 11 pixels down. How do i set a circle shape to be created there?

Dani AI

Generated

Note for : in Swing you draw into a component (usually a JPanel) by overriding paintComponent. The coordinate system is pixels with (0,0) at the top-left of the component and Y growing downward. Methods like fillOval and drawOval take the top-left corner of the bounding rectangle, not the circle center — to place a circle whose center is at (32,11) subtract the radius from those coordinates.

As suggested, Java2D covers this in more detail. The example below shows a minimal, correct pattern that paints a filled circle centered at (32,11). It runs on the Event Dispatch Thread and uses anti-aliasing for a smoother circle.

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

public class CircleAtPoint {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame("Circle spawn");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new CirclePanel());
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }

    static class CirclePanel extends JPanel {
        private final int cx = 32, cy = 11, r = 8;
        CirclePanel() { setPreferredSize(new Dimension(200, 100)); setBackground(Color.WHITE); }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.RED);
            g2.fillOval(cx - r, cy - r, r * 2, r * 2);
            g2.dispose();
        }
    }
}

Troubleshooting notes: if the circle does not appear, check that the panel is large enough (use setPreferredSize and pack), that painting happens on the EDT, and that coordinates lie within the component bounds. To spawn shapes dynamically, store their coordinates in fields, update them (for example from a mouse listener or Timer), and call repaint() to refresh.

Recommended Answers

All 3 Replies

You could start with the 2D Graphics tutorial.

Hi mate, yeah i've seen that, im still having a bit of a problem with it. I remember it bein pretty good, will have another look.

You could also take a look at this post that demonstrates the absolute basics of drawing a couple of lines on a JPanel. If you check the Graphics (and Graphics2D) API, you'll find other methods for drawing other shapes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.