Simply Java Button to Display a Circle


I am currently learning java, I understand the concepts except Graphics which as a programmer is totally new to me. Quite frankly it is driving my around the bend. My example should in theory make a circle appear at the press of a Button.

Using print methods to debug I keep finding that the Button correctly called all the methods and creates a new circle c object but in the newNode().drawCircle() repaint() is never called and hence the new object not drawn. WHY IS THIS and can someone help me get this darn circle to appear!! Some may notice I resorted to using this example to try and help resolve the problem <a href="http://leepoint.net/notes-java/examples/graphics/circles/circles.html" rel="nofollow">http://leepoint.net/notes-java/examples/graphics/circles/circles.html</a> .

This was meant to be the start of a Network graphing program which i PRESUMED would be easy...except for displaying the nodes when created...i.e the circle!

This code now works so i hope it can help people with a similar problem, as i know this is a common java assignment :)

import java.awt.*; import java.awt.geom.*; import javax.swing.*; import java.awt.event.*; import java.util.*; /////////////////////////////////////////////////////////////////////// public class NetGrapher { public static void main(String[] args){ final JFrame frame = new JFrame ("NetGrapher"); frame.getContentPane().add(new NewNode()); /////delete line final NewNode newNode = new NewNode(); ///// Revision after answer, add, frame.getContentPane().add(newNode); (erase the above frame.getContent) JPanel buttonPanel = new JPanel(); JButton button = new JButton ("New Node"); button.addActionListener(new ActionListener( ){ public void actionPerformed( ActionEvent e) { System.out.println( "Button Pressed"); newNode.drawCircle(); } }); buttonPanel.add(button); frame.add(buttonPanel, BorderLayout.SOUTH); frame.setSize(600,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } ////////////////////////////////////////////////////////////////////// class NewNode extends JComponent { public ArrayList<Circle> _circles = new ArrayList<Circle>(); public void paintComponent(Graphics g){ g.setColor(Color.WHITE); g.fillRect(0, 0, 600, 600); System.out.println( "RePaint"); for ( Circle c : _circles){ System.out.println( "Each C"); g.setColor(Color.BLACK); c.draw(g); } } public void drawCircle(){ System.out.println( "drawCircle Implemented"); Circle c = new Circle(100, 100, 100, 100); _circles.add(c); repaint(); } } ///////////////////////////////////////////////////////////////////// class Circle { int x, y, z, a; Circle (int _x, int _y, int _z, int _a){ this.x = _x; this.y = _y; this.z = _z; this.a = _a; } public void draw(Graphics g){ System.out.println( "Called In Draw Method"); g.setColor(Color.BLACK); g.fillOval(x, y, z, a); } }


You are using two different instances of NewNode

frame.getContentPane().add(new NewNode()); final NewNode newNode = new NewNode();

In your action listener you're calling newNode.drawCircle() on newNode which was not added to a content pane.

BTW have you noticed that you've got two Circle classes, where the first does some strange things (like adding a new circle into _circles which it cannot access)?


  • TF26198 error when doing git tfs rcheckin
  • Add a 'prefix' controller to url in CodeIgniter
  • C++/SDL2 — Rendering A Circle
  • Calculate intersection area of two paths
  • QT QGraphicsScene Drawing Arc
  • Regex: Find a character anywhere in a document but only on lines that begin with a specific word
  • How to set up a clipping rectangle or area
  • How to reference selected bokeh data points
  • ball collision detection
  • How do I get narrow width and a large height on a RadioButtons widget, and still have round radio bu
  • Chart js - Draw center of each bubbles of a bubble chart
  • A Question about the .NET garbage collector when cyclic references exist
  • Get attributes of existing SVG elements and bind as data with d3.js
  • https in htaccess and order of rules (using Expression Engine)
  • Vue props data not updating in child component
  • Neo4j: Legacy Indexes and auto index vs new label bases schema indexes
  • How to add learning rate to summaries?
  • CloseOptionsMenu doesn't work?
  • d3.js selection conditional rendering
  • Clear activity stack before launching another activity
  • Jquery popup on mouse over of calendar control
  • Differences in dis-assembled C code of GCC and Borland?
  • Array with custom indexes in Ionic2
  • Textfile Structure (tables)
  • How do I get HTML corresponding to current DOM tree?
  • Refering to the class itself from within a class mehod in Objective C
  • How to create a file in java without a extension
  • Avoid links criss cross / overlap in d3.js using force layout
  • Algorithm for a smudge tool?
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Incrementing object id automatically JS constructor (static method and variable)
  • ILMerge & Keep Assembly Name
  • Large data - storage and query
  • WOWZA + RTMP + HTML5 Playback?
  • Free memory of cv::Mat loaded using FileStorage API
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • Checking variable from a different class in C#
  • java string with new operator and a literal