Custom painting/ graphics in a panel

Ten Duke Dollars for whoever gives the best high level solution for my problem.
I'm writing a small application that displays music notation on screen and lets the user move the notes vertically to the desired locations on the staves. The user can then do various kinds of analysis on each example. The analysis stuff is done, but I'm having trouble finding an approach that will work for the graphics.
My basic approach so far is to subclass JPanel and use it to handle all the custom painting. I'm planning to add all needed JPEG's to JLabels. Some of the images (e.g. treble and bass clef images) will always be in a fixed location, while others (note images) will move vertically when the user drags them. For the lines on the staves and the measure lines, I'm planning to use g.drawLine.
The following questions occur to me:
1. How will I prevent the note images from hiding the lines? Will I need to make the images transparent GIFs or something? Or can I set some layering property in the JPanel to build up a layered drawing?
2. to detect mouse events, should I attach mouse listeners to my panel, or to each label that contains a note image? (I considered using Ellipse objects for the note heads and lines for the stems, but this will not give me a high enough quality of image.)
3. I will probably need to use absolute positioning in the panel class rather than FlowLayout or whatever, but I'm having trouble getting rid of the layout manager. Can you give me a few lines of code to do this?
4. Is my overall approach correct? Is there a better, easier way to accomplish what I'm trying to do?
thanks,
Eric

>
The following questions occur to me:
1. How will I prevent the note images from hiding the
lines? Will I need to make the images transparent GIFs
or something? Or can I set some layering property in
the JPanel to build up a layered drawing?If you are going to use images (probably BufferedImages), their Transparency will probably be BITMASK or TRANSLUSCENT.
2. to detect mouse events, should I attach mouse
listeners to my panel, or to each label that contains
a note image? (I considered using Ellipse objects for
the note heads and lines for the stems, but this will
not give me a high enough quality of image.)I don't think using JLabel objects is a good idea. Instead of add labels to a panel, I would define a custom JComponent that did its own painting in paintComponent.
>
3. I will probably need to use absolute positioning in
the panel class rather than FlowLayout or whatever,
but I'm having trouble getting rid of the layout
manager. Can you give me a few lines of code to do
this?If you follow my last comment, your component isn't being used as a container, so this is not relevant.
>
4. Is my overall approach correct? Is there a better,
easier way to accomplish what I'm trying to do?
thanks,
EricCheck out forum Java 2D. That's where this topic belongs. You also need to learn the 2D API. Search for some text book references in that forum.
From the Imipolex G keyboard of...
Lazlo Jamf

Similar Messages

  • Scrolling a custom Component (e.g. JPanel) with overridden paint(Graphic g)

    Hi.
    I’m creating an application for modelling advanced electrical systems in a house. Until now I have focused on the custom canvas using Java2D to draw my model. I can move the model components around, draw lines between them, and so on.
    But now I want to implement a JScrollPane to be able to scroll a large model. I’m currently using a custom JPanel with a complete override of the paint(Graphic g) method.
    Screen-shot of what I want to scroll:
    http://pchome.grm.hia.no/~aalbre99/ScreenShot.png
    Just adding my custom JPanel to a JScrollPane will obviously not work, since the paint(Graphic g) method for the JPanel would not be used any more, since the JScrollPane now has to analyze which components inside the container (JPanel) to paint.
    So my question is therefore: How do you scroll a custom Component (e.g. JPanel) where the paint(Graphic g) method is totally overridden.
    I believe the I have to paint on a JViewport instructing the JScrollPane my self, but how? Or is there another solution to the problem?
    Thanks in advance for any suggestions.
    Aleksander.

    I�m currently using a custom JPanel with a complete override of the paint(Graphic g) method. Althought this isn't your problem, you should be overriding the paintComponent(..) method, not the paint(..) method.
    But now I want to implement a JScrollPane to be able to scroll a large model.When you create a custom component to do custom painting then you are responsible for determining the preferredSize of the component. So, you need to override the getPreferredSize(...) method to return the preferredSize of your component. Then scrolling will happen automatically when the component is added to a scrollPane.

  • Custom Painting in Swing Panels.

    I am trying to implement a custom painted panel on a JFrame. The panel performs custom painting in a separate thread. In additon, the frame also adds other swing panels (containing standard components) dynamically as per menu selection. I use box layout on frame to add / remove panels.
    The paint panel doesnt render properly whenever other panels are added or replaced. The paint panel is rendered only when no other are panels added to the frame.
    There is no problem however with adding / replacing components-only panels containing no custom painting.
    Please help me understand what I am missing. Any clues / suggestions most welcome
    sridhar

    Hi,
    You have problem only when adding custom painting JPanel to the same custom painting JPanel??? Are you layering your painting methods??? Like the most upper JPanel paints and then the childs??? What do you get for drawings??? Do you use invoque later method??? What is your priority on the Threads you are using???
    JRG

  • Custom paint() and rare repainting

    I have made custom paint() method like this:
    @Override
              public void paint(Graphics  g){
                   int time;               
                   if(renderer != null)
                        renderer.updateImage();
                   time = Simulation.getCurrentStepTime();          
                   if(time>=0)          labelCurrentTimeVal.setText(""+time);
                   else              labelCurrentTimeVal.setText("-");
                   time = Simulation.getAverageStepTime();          
                   if(time>=0)          labelAvgTimeVal.setText(""+time);
                   else              labelAvgTimeVal.setText("-");
                   super.paint(g);
              }     I have some calculations going on in the background (separate thread). 'renderer' is responsible for repainting the JLabel holding an ImageIcon (which gets changed by the calculations). The case is that my background calculations take a few seconds. After they're done I call the repaint() method, which updates the image and paints the whole thing.
    My problem is that I want to have up-to-date info on how much the iteration is taking so I`ve made the labelCurrentTimeVal JLabel to display it. The label only repaints when I call the repaint() method. However when I keep moving some slider in this panel repaint gets called all the time (I can see the labelCurrentTimeVal value changing constantly as I move the knob).
    So what should I do to make the repaint() method get called often enough (20-30 times /sec to get about 20-30 fps)? I tried creating a different thread that calls repaint all the time (just to test it) and it doesn't solve the case.new Runnable() {
                public void run() {
                     while(true)
                          if(MainWindow.instance != null)
                               MainWindow.instance.repaint();
              };

    I`ve made same code to represent the problem. The calculationThread does the calculations - in this case sets the iteration counter and the repaintThread is supposed to update the GUI and repaint it once in a while. GUI doesn't update at all and I don't get why?
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class Main extends JFrame{     
         private JPanel panel;
         private JLabel label;
         public static Main window;
         private Runnable calcThread,repaintThread;
         private static int it;
         public static void main(String[] args) {
              new Main();
         private void createComponents(){
              window = this;
              panel = new JPanel();
              label = new JLabel("iteration:");
              panel.setPreferredSize(new Dimension(200,200));
              panel.add(label);
              this.add(panel);
              this.pack();
              this.setVisible(true);
         public Main(){
              createComponents();
              calcThread = new Runnable(){
                   public void run(){
                        int i=0;                    
                        while(true){                         
                             Main.it = i;
                             System.out.println("check - iteration: "+i);
                             i++;
              repaintThread = new Runnable(){
                   public void run(){
                        while(true){
                             try{
                                  wait(30);
                                  label.setText("iteration: "+it);     
                                  window.repaint();
                             }catch(InterruptedException e){}
              calcThread.run();
              repaintThread.run();
    }

  • Design advice for custom painting

    Hi,
    Can someone give me some high-level design advice on designing a JPanel subclass for custom painting? My panel class is becoming very complex, with lots of drawing and scaling methods, so I'm wondering if I could abstract away some of these graphical elements by creating new classes to make the design more object-oriented. However, I'm finding that there are also disadvantages in representing some of my graphic components as classes. Specifically,
    1. It will lead to a much higher level of class coupling. My panel will depend on all these new classes to work correctly. In fact the situation is even worse because my panel is an inner class and, to do some of the scaling, needs to use methods from an object stored in the parent class. I would therefore have to also pass this object reference as an argument to many of these new classes.
    2. It will lead to a lot of awkward passing of data between classes. For example, I need to use g2.drawImage(img, x, y, w, h, this), so I will have to pass not only the graphics context but also the panel reference itself.
    Is it common for panel subclasses that do custom painting to be complex?
    thanks,
    Eric

    I wrote the map view for a commercial GIS system. Drawing and scaling on a JPanel is challenging, but it need not be complex.
    1. To eliminate class coupling, you need to create a couple of interfaces: Renderable (what you want drawn) and Renderer (the thing doing the low-level drawing). Renderer will have before and after setup and reset methods (to do things like scaling and rotation), and methods that the renderables can use to draw graphics. The Renderable interface can be as simple as a single method: draw(Renderer).
    Every type of graphic that you draw on the screen would be a different class that implements Renderable, and which knows how to draw itself using whatever lower-level drawing commands you put in the Renderer. If you construct each Renderable in terms of java.awt.Shape, then Renderable.draw() could call a method Renderer.draw(java.awt.Shape, java.awt.Color).
    2. The Panel becomes fairly simple. It has a Renderer and a collection of Renderable objects. Its paint() method calls the Renderer setup method, calls Renderable.draw(Renderer) on each object, and calls the Renderer reset method. Each Renderable in turn calls Renderable.draw(java.awt.Shape, java.awt.Color) one or more times.
    Renderer should get a Graphics2D from the Panel when the setup method is called. That's when the Renderer does all of the scaling, positioning, and rotation on the Graphics2D. The Renderable implementations shouldn't even need to know about it.
    I don't think custom painting code is necessarily complex, merely challenging to write. If you're only drawing a few lines and circles, you probably don't have to be too concerned about design elegance and code maintainability. The map view I designed for our GIS system, on the other hand, has to handle all kinds of map geometry, icons, text, and aerial photos.

  • Painting in only one panel?

    Hello,
    I need to make a joystick style gui part in my applet, but only in one panel of the applet, Is it possible to paint in just one panel, and if so, can you point me in the right direction? Or even better, is there a joystick style swing component i dont know about? it basically just needs to be a slider with x and y components. Also, Would using a custom synth look and feel screw up any painting im trying to do? Thanks beforehand.
    Cheers.

    Andre_Uhres wrote:
    I don't know about any "joystick style swing component" and I couldn't even tell you how to develop such a component.So I searched the web. This article might be helpful: [Virtual joystick GUI|http://www.freepatentsonline.com/6515687.html]. Logical Processing for the Virtual Joystick is summarized in the pseudo-code of TABLE 1.
    Edited by: Andre_Uhres on Jan 30, 2010 11:35 AM
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class SimpleJoystickDemo extends JFrame {
        private int displayWidth = 340;
        private int displayHeight = 550;
        private final Point position;
        public SimpleJoystickDemo() {
            super("SimpleJoystickDemo");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(displayWidth, displayHeight);
            setLocationRelativeTo(null);
            position = new Point();
            SimpleJoystick myJoystick = new SimpleJoystick(150, position, 100);
            add(myJoystick, BorderLayout.PAGE_END);
            Drawing drawing = new Drawing(position);
            add(drawing);
        public static void main(final String[] args) {
            Runnable gui = new Runnable() {
                public void run() {
                    new SimpleJoystickDemo().setVisible(true);
            //GUI must start on EventDispatchThread:
            SwingUtilities.invokeLater(gui);
        class Drawing extends JPanel {
            private final Point position;
            public Drawing(Point position) {
                this.position = position;
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setColor(Color.RED);
                g2.fillOval(160 + position.x, 160 - position.y, 15, 15);
    class SimpleJoystick extends JPanel {
        //Maximum value for full horiz or vert position where centered is 0:
        private int joyOutputRange;
        private float joySize;     //joystick icon size
        private float joyWidth, joyHeight;
        private float joyCenterX, joyCenterY;  //Joystick displayed Center
        //Display positions for text feedback values:
        private int textHorizPos, textVertPos;
        private int fontSpace = 12;
        private float curJoyAngle;    //Current joystick angle
        private float curJoySize;     //Current joystick size
        private boolean isMouseTracking;
        private boolean leftMouseButton;
        private int mouseX, mouseY;
        private Stroke lineStroke = new BasicStroke(10,
                BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        private final Point position;
        public SimpleJoystick(final int joyOutputRange, final Point position,
                final int joySize) {
            this.joyOutputRange = joyOutputRange;
            this.position = position;
            this.joySize = joySize;
            joyWidth = joySize;
            joyHeight = joyWidth;
            setPreferredSize(new Dimension((int) joyWidth + 250,
                    (int) joyHeight + 80));
            joyCenterX = getPreferredSize().width / 2;
            joyCenterY = getPreferredSize().height / 2;
            this.joySize = joyWidth / 2;
            setBackground(new Color(226, 226, 226));
            MouseAdapter mouseAdapter = new MouseAdapter() {
                @Override
                public void mouseMoved(final MouseEvent e) {
                    mouseCheck(e);
                @Override
                public void mousePressed(final MouseEvent e) {
                    leftMouseButton = SwingUtilities.isLeftMouseButton(e);
                    mouseCheck(e);
            addMouseMotionListener(mouseAdapter);
            addMouseListener(mouseAdapter);
        private void mouseCheck(final MouseEvent e) {
            mouseX = e.getX();
            mouseY = e.getY();
            float dx = mouseX - joyCenterX;
            float dy = mouseY - joyCenterY;
            if (leftMouseButton) {
                isMouseTracking = true;
            } else {
                isMouseTracking = false;
            if (isMouseTracking) {
                curJoyAngle = (float) Math.atan2(dy, dx);
                curJoySize = (float) Point.distance(mouseX, mouseY,
                        joyCenterX, joyCenterY);
            } else {
                curJoySize = 0;
            if (curJoySize > joySize) {
                curJoySize = joySize;
            position.x = (int) (joyOutputRange * (Math.cos(curJoyAngle)
                    * curJoySize) / joySize);
            position.y = (int) (joyOutputRange * (-(Math.sin(curJoyAngle)
                    * curJoySize) / joySize));
            SwingUtilities.getRoot(SimpleJoystick.this).repaint();
        @Override
        protected void paintComponent(final Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.LIGHT_GRAY);
            g2.fillOval((int) (joyCenterX - joyWidth / 2),
                    (int) (joyCenterY - joyHeight / 2),
                    (int) joyWidth, (int) joyHeight);
            //rotate and draw joystick line segment:
            Graphics2D g3 = (Graphics2D) g2.create();
            g3.translate(joyCenterX, joyCenterY);
            g3.rotate(curJoyAngle);
            g3.setColor(Color.GRAY);
            g3.setStroke(lineStroke);
            g3.drawLine(0, 0, (int) curJoySize, 0);
            g3.dispose();
            g2.setColor(Color.GRAY);
            g2.fillOval((int) joyCenterX - 10,
                    (int) joyCenterY - 10, 20, 20);
            textHorizPos = 50;
            textVertPos = (int) (joyCenterY - 50);
            g2.drawString("Horiz:", textHorizPos, textVertPos);
            textHorizPos += (4 * fontSpace);
            g2.drawString(String.valueOf((int) position.x), textHorizPos,
                    textVertPos);
            textHorizPos = 50;
            textVertPos += 12;
            g2.drawString("Vert:", textHorizPos, textVertPos);
            textHorizPos += (4 * fontSpace);
            g2.drawString(String.valueOf((int) position.y), textHorizPos,
                    textVertPos);
    }Edited by: Andre_Uhres on Feb 1, 2010 10:23 PM

  • Simultaneous custom painting

    I have created a JPanel which is a long rectangle "setPreferredSize(new Dimension(321, 21));" that performs custom painting . The custom painting is to represent a loop; a colored rectangle moves along the length of the panel when the process loops it repeats the painting. I have to create many instances of this panel and add these instances to the program's main panel. I have managed to get the colored rectangle to move through every instance of the panel but as each one progress they are not in sync, they each need to be at the same point of the panel at the same time. I have used a thread to control the custom painting and I think because each instance of the panel uses its own thread they are being interleaved which is resulting in time lags. Can anyone give me some ideas of how I can imlement this so each colored rectangle is at the same point at the same time. Here are some relevent code snippets:
    * This method is called from within paintComponent
    private void moveRect(Graphics g){
              if(looped==true){
                   Dimension d = getSize();
                   offscreen = createImage(d.width, d.height);
                   offscreensize = d;
                   offgraphics = offscreen.getGraphics();
                   offgraphics.setColor(new Color(115,212,236));
                   offgraphics.fillRect(xpos+1, 1, 19 ,19);
                   g.drawImage(offscreen, 0, 0, this);
    public void run(){
              while(true){
                   for(xpos = 0;xpos<=320;xpos = xpos+20){
                        repaint();
                       try{
                        Thread.sleep(116);
                       }catch(InterruptedException e){}
         

    Thanks for your reply. The Timer seems to work better. I have created a separate class for the Timer and when a new instance of the Bar JPanel is created a Timer is also created. I have made the Time class a JPanel, the problem I am having now is I need to place the Time JPanel on top of the Bar JPanel but below the Graphics on the Bar JPanel. When I try to do it the Time JPanel just sits on top of the Bar JPanel and all the graphics are not seen. The Time JPanel needs to be in the back ground; Is there a method call like setBackground that accepts components, or any ideas on how this can be achieved?

  • (Another) problem with custom painting using JApplet and JPanel

    Hi all,
    I posted regarding this sort of issue yesterday (http://forums.sun.com/message.jspa?messageID=10883107). I fixed the issue I was having, but have run into another issue. I've tried solving this myself to no avail.
    Basically I'm working on creating the GUI for my JApplet and it has a few different JPanels which I will be painting to, hence I'm using custom painting. My problem is that the custom painting works fine on the mainGUI() class, but not on the rightGUI() class. My code is below:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    public class TetrisClone extends JApplet {
         public void init() {
              setSize( 450, 500 );
              Container content = getContentPane();
              content.add( new mainGUI(), BorderLayout.CENTER );
              content.add( new rightGUI() , BorderLayout.LINE_END );
    class mainGUI extends JPanel {
         // Main bit where blocks fall
         public mainGUI() {
              setBackground( new Color(68,75,142) );
              setPreferredSize( new Dimension( 325, 500 ) );
              validate();
         public Dimension getPreferredSize() {
              return new Dimension( 450, 500 );
         public void paintComponent( Graphics g ) {
              super.paintComponent(g);
              // As a test. This shows up fine.
              g.setColor( Color.black );
              g.fillRect(10,10,100,100);
              g.setColor( Color.white );
              g.drawString("Main",45,55);
    class rightGUI extends JPanel {
         BufferedImage img = null;
         int currentLevel = 0;
         int currentScore = 0;
         int currentLines = 0;
         public rightGUI() {
              // The right panel. Has quite a few bits. Starts here..
              FlowLayout flow = new FlowLayout();
              flow.setVgap( 20 );
              setLayout( flow );
              setPreferredSize( new Dimension( 125, 500 ) );
              setBackground( new Color(27,34,97) );
              setBorder( BorderFactory.createMatteBorder(0,2,0,0,Color.black) );
              // Next block bit
              JPanel rightNext = new JPanel();
              rightNext.setPreferredSize( new Dimension( 100, 100 ) );
              //rightNext.setBackground( new Color(130,136,189) );
              rightNext.setOpaque( false );
              rightNext.setBorder( BorderFactory.createEtchedBorder(EtchedBorder.LOWERED) );
              Font rightFont = new Font( "Courier", Font.BOLD, 18 );
              // The player's playing details
              JLabel rightLevel = new JLabel("Level: " + currentLevel, JLabel.LEFT );
              rightLevel.setFont( rightFont );
              rightLevel.setForeground( Color.white );
              JLabel rightScore = new JLabel("Score: " + currentScore, JLabel.LEFT );
              rightScore.setFont( rightFont );
              rightScore.setForeground( Color.white );
              JLabel rightLines = new JLabel("Lines: " + currentLines, JLabel.LEFT );
              rightLines.setFont( rightFont );
              rightLines.setForeground( Color.white );
              JPanel margin = new JPanel();
              margin.setPreferredSize( new Dimension( 100, 50 ) );
              margin.setBackground( new Color(27,34,97) );
              JButton rightPause = new JButton("Pause");
              try {
                  img = ImageIO.read(new File("MadeBy.gif"));
              catch (IOException e) { }
              add( rightNext );
              add( rightLevel );
              add( rightScore );
              add( rightLines );
              add( margin );
              add( rightPause );
              validate();
         public Dimension getPreferredSize() {
                   return new Dimension( 125, 500 );
         public void paintComponent( Graphics g ) {
              super.paintComponent(g);
              g.setColor( Color.black );
              g.drawString( "blah", 425, 475 ); // Doesn't show up
              g.drawImage( img, 400, 400, null ); // Nor this!
              System.out.println( "This bit gets called fine" );
    }Any help would be greatly appreciated. I've read loads of swing and custom painting tutorials and code samples but am still running into problems.
    Thanks,
    Tristan Perry

    Many thanks for reminding me about the error catching - I've added a System.out.println() call now. Anywhoo, the catch block never gets run; the image get call works fine.
    My problem was/is:
    "My problem is that the custom painting works fine on the mainGUI() class, but not on the rightGUI() class. My code is below:"
    I guess I should have expanded on that. Basically whatever I try to output in the public void paintComponent( Graphics g ) method of the rightGUI class doesn't get output.
    So this doesn't output anything:
    g.drawString( "blah", 425, 475 ); // Doesn't show up
    g.drawImage( img, 400, 400, null ); // Nor this!
    I've checked and experimented with setOpaque(false), however this doesn't seem to be caused by any over-lapping JPanels or anything.
    Let me know if I can expand on this :)
    Many thanks,
    Tristan Perry
    Edited by: TristanPerry on Dec 10, 2009 8:40 AM

  • Painting Graphics from an Array

    I have a GUI that displays the Towers of Hanoi solution in a JPanel. Everything actually works except for a slight repainting issue.
    Currently I am overriding the paint() function, I know that for my purpose using paintComponent() would probably be the better route, however that's not my issue...
    The images (disks) in my JPanel are not redrawing properly, and I have no idea why...
    Basically:
    - I call setup() to set the number of disks and the co ordinates they should be drawn. (store them in an array)
    - I call moveDisk() to change the co ordinates of each disk as they are being moved.
    - then drawDisk() is called to go through the array of disks and print them.
    currently paint() calls drawDisk() in order to display the current position of all the disks.
    It seems to work fine for the first 2 moves, then disks start disappearing until there is one (the last disk) left...BUT if I print the co ordinates of the disks they seem to be correct, just not drawing.
    could someone take a look at my code and see what is wrong?
    below is the meat of the code, minus the GUI:
        public class HanoiFrame extends javax.swing.JFrame {
        private static final int STATE_SETUP = 0;
        private static final int STATE_ANIMATE = 1;
        private static final int POLE_AREA_HEIGHT = 356;
        private static final int POLE_AREA_WIDTH = 205;
        private static final int DISK_ARC_HEIGHT = 15;
        private static final int DISK_ARC_WIDTH = 15;
        private static final int DISK_HEIGHT = 23;
        private static final int MAX_DISKS = 10;
        private static final int MAX_ANIMATION_DELAY = 30000;
        private static final int MAX_PHYSICAL_DELAY = 100000;
        private static final int MIN_DISKS = 1;
        private static final int MIN_ANIMATION_DELAY = 1;
        private static final int MIN_PHYSICAL_DELAY = 1;
        private static final int EMPTY_CELL_VALUE = -1;
        private static final int CELL_HEIGHT = 25;
        private static final int COLUMNS = 3;
        private static final int ROW = 11;
        private  static final int MILLISECONDS_PER_SECOND = 1000;
        private final static String newline = "\n";
        private static Disk[] disks;
        private static Disk newDisk;
        private static int[][] diskCells;
        public static  int startX = 2;
        public static  int startY = 340;
        public static int setDiskAmount = 0;
        public static int setAnimationDelay = 0;
        public static int  setPhysicalDelay = 0;
        public static int  moveNumber = 0;
        public static int intMovesLeft = 0;
        private boolean  buttonPressed = false;
        private double movesLeft = 0;
        private int windowState = 0;
        /** Creates new form HanoiFrame */
        public HanoiFrame() {
            initComponents();
            /* initialize the "poles" to hold a certain number of "cells */
            this.diskCells = new int[ROW][COLUMNS];
            for (int row = ROW - 1;row >= 0; row--) {
                for (int col = 0;col < COLUMNS ; col++) {
                    diskCells[row][col] = EMPTY_CELL_VALUE;
        private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            /* reset all the variable to original state */
            setDiskAmount = 0;
            setAnimationDelay = 0;
            setPhysicalDelay = 0;
            moveNumber = 0;
            movesLeft = 0;
            intMovesLeft = 0;
            buttonPressed = false;
            windowState = 0;
            this.startButton.setEnabled(true);
            this.diskAmount.setEditable(true);
            this.animationDelay.setEditable(true);
            this.physicalDelay.setEditable(true);
            /* reset all the cell vales to empty (this could be its own function) */
            for (int row = ROW - 1;row >= 0; row--) {
                for (int col = 0;col < COLUMNS ; col++) {
                    diskCells[row][col] = EMPTY_CELL_VALUE;
            /* reset the content of the TextFields and Area */
            this.diskAmount.setText("");
            this.animationDelay.setText("");
            this.physicalDelay.setText("");
            this.outTextArea.setText("");
            /* repaint the display panel */
            this.hanoiPanel.repaint();
        /* i have no idea why this is here...It was generated by Netbeans when I
         *made the reset button.  As you can see it made 2.  The one above it the same
         *except it contains code.  Since it was automatically generated
         *I cannot delete it.
        private void resetButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
        /* is executed when the start button is pressed.
         *also executes a field check and intializes key variables. Finally,
         *it executes the solution and generates the solve time
        private void startButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
            /* check if the program has already been run (without being reset) */
            if (!buttonPressed){
                /* check the fields to ensure the input is correct and useable */
                if (checkFields()){
                    /* give button a pressed status if all info valid */
                    buttonPressed = true;
                    windowState = 1;
                    /* disable the button */
                    this.startButton.setEnabled(false);
                    /* disable the fields */
                    this.diskAmount.setEditable(false);
                    this.animationDelay.setEditable(false);
                    this.physicalDelay.setEditable(false);
                    /* setup the disks on the starting pole */
                    setup();
                    /* initialize the number of moves required. 2 to the power of n minus 1 */
                    movesLeft = Math.pow(2, setDiskAmount) - 1;
                    /* convert the number to an integer */
                    intMovesLeft = (int)movesLeft;
                    /* set the physical delay */
                    setPhysicalDelay = Integer.parseInt(this.physicalDelay.getText());
                    /* create and start a new thread.  This is EXTREMELY important
                     *as it allows for GUI to be repainted while the soulution
                     *is animated */
                    SolveEngine s = new SolveEngine();
                    s.start();               
         * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new HanoiFrame().setVisible(true);
        /*returns the Animation panel area graphics
         *@return Animation area graphics
        public static Graphics getAnimationArea(){
            /* get the usable area of poleA */
            Container clientArea = hanoiPanel;
            //Container clientArea = this.getContentPane();
            /* get the graphics to the display panel*/
            Graphics gfx = clientArea.getGraphics();
            return gfx;
         *set up the requested amount of disks on the first pole
         *@param diskAmount the amount of disks entered by the user
        public void setup(){
            int numberOfDisks = setDiskAmount;
            this.disks = new Disk[numberOfDisks];
            int diskNumber = 0;
            int diskWidth = POLE_AREA_WIDTH - 4;
            int arcH = DISK_ARC_HEIGHT;
            int arcW = DISK_ARC_WIDTH;
            int x = startX;
            int y = startY;
            int row = 0;
            int col = 0;
            /* get the graphics to the display panel*/
            //Graphics gfx = getAnimationArea();
            /* set the color of the disks */
            //gfx.setColor(Color.MAGENTA);
            /* draw the specified number of disks */
            while (diskNumber < numberOfDisks){
                /* reduce the y position */
                y = startY - diskNumber * CELL_HEIGHT;
                /* draw the disk */
                //gfx.fillRoundRect(x, y, diskWidth, DISK_HEIGHT, arcH, arcW);
                /* create a new instance of disk */
                newDisk = new Disk(x, y, diskWidth);
                /* give the new disk an id */
                this.disks[diskNumber] = newDisk;
                /* add the id to the cell array of poleA */
                this.diskCells[row][col] = diskNumber;
                /* make the disk smaller and center */
                x = x + 8;
                diskWidth = diskWidth - 16;
                /* increase disk number */
                diskNumber++;
                /* move to the next row */
                row++;
            repaint();
         *move the disk from a source pole to the destination pole.
         *this should take the source col and destination col to determine where to draw the
         *disk.  It will also need to know to draw the disk in the first available cell
         *in a col
         *@param source the starting pole of the next move
         *@param destination the end pole of the next move
        public void moveDisk(int src, int dest){
            /* the lines below would not be necessary if I were to pass the Graphics
             *gfx object as a parameter.  I may have to use this option in order
             *to work around the current repainting issue */
            /* get the graphics to the display panel*/
            Graphics gfx = getAnimationArea();
            /* get the id of the disk to be moved */
            int disk = getDiskId(src);
            /* get the to and from rows */
            int sourceRow = getRow(src);
            int destinationRow = getRow(dest);
            /* set the co ordintates of the destination */
            int col = dest * POLE_AREA_WIDTH;
            int x = disks[disk].getStartX() + col;
            int y = startY - (destinationRow * CELL_HEIGHT);
            int width = disks[disk].getDiskWidth();
            disks[disk].setStartX(x);
            disks[disk].setStartY(y);
            disks[disk].setDiskWidth(width);
            //System.out.println("startX " + x);
            //System.out.println("startY " + y);
            //System.out.println("destination row " + destinationRow);
            //System.out.println("disk " + disk);
            //System.out.println("Width " + width);
            diskCells[sourceRow - 1][src] = EMPTY_CELL_VALUE;
            /* set the destination row to the disk id */
            diskCells[destinationRow][dest] = disk;
            drawDisk();
            repaint();
        public void drawDisk(){
            int diskNum = setDiskAmount -1;
            int i = 0;
            Graphics gfx = getAnimationArea();
            gfx.setColor(Color.MAGENTA);
            while(diskNum >= 0){
                //System.out.println("here is the disk IDs " + diskNum);
                //System.out.println("the startY during draw " + disks[diskNum].getStartY());
                gfx.fillRoundRect(disks[diskNum].getStartX(), disks[diskNum].getStartY(), disks[diskNum].getDiskWidth(), DISK_HEIGHT, DISK_ARC_WIDTH, DISK_ARC_HEIGHT);
                diskNum--;
        public void paint(Graphics gfx){
            if(windowState == 0){
                super.paint(gfx);
                setup();
            } else if (windowState == 1){
                super.paint(gfx);
                drawDisk();
         *returns the id of the disk
         *@param col the the designated column
         *@return the disk identification number
        public static int getDiskId(int col){
            int diskIdent = 0;
            /* initialize the row number to max -1, because the array start at 0 */
            int row = MAX_DISKS - 1;
            /* do a cell check while row is greater than 0
             *this is done so that is starts at the top row
             *and moves down
            while (row > EMPTY_CELL_VALUE){
                /* when the first cell that is not -1 is reached get the disk number
                 *that disk will be the smallest in the stack */
                if(diskCells[row][col] != -1){
                    //diskIdent = this.diskCells[row][col];
                    diskIdent =diskCells[row][col];
                    break;
                row--;
            return diskIdent;
         *returns the first available row
         *@param col the designated col
         *@return the first available row number
        public static int getRow(int col){
            int rowNumber = 0;
            /* cycle through the cells until it finds an empty one */
            while (diskCells[rowNumber][col] != -1){
                rowNumber++;
            return rowNumber;
         *calculate the total time it takes for the tower of hanoi game to be
         *solved, given the amount of disk and the amount of time it takes
         *to physically move a disk from one pole to the other.
        public static void totalTime(){
            int timeInSeconds = setPhysicalDelay * intMovesLeft;
            int i = 0;
            int seconds = 0;
            int minute = 0;
            int hour = 0;
            int day = 0;
            int month = 0;
            int year = 0;
            while (i < timeInSeconds){
                if(seconds > 60){
                    minute++;
                    seconds = 0;
                if(minute > 60){
                    hour++;
                    minute = 0;
                if(hour > 24){
                    day++;
                    hour = 0;
                if(day > 30){
                    month++;
                    day = 0;
                if(month > 12){
                    year++;
                    month = 0;
                seconds++;
                i++;
            updateMessageCenter("It would take:" + year + " years " + month + " months " + day +
                    " days " + hour + " hours " + minute + " minutes and " + seconds + " seconds, to move " + setDiskAmount +
                    " disks at " + setPhysicalDelay + " seconds per move");
       

    Sorry if my post was a little too cryptic...
    "How do you know? You have a problem you can't solve. Anything could be your issue." Agreed. However, using the process of elimination and debug statments I have been able to narrow the scope of the issue.
    "If you can't solve it by executing the code and adding debug statements how to you expect us to help."If I could solve but "executing code and adding debug statements" there would be no need to post the problem here.
    "We don't know exactly what you are attempting to do."Trying to paint my graphics to a JPanel based on a set of co ordinates.
    "We don't know why you are passing the Graphics object around."Why not? is it impossible to do it this way?
    "The big problem is in the alogorithm you use to determine the location of each component."Who said you couldn't read minds?
    "If you are using some kind of animation then I would use a Timer to schedule the moving of components. I don't even see how you are doing this in your code."Sorry I guess stating that I didn't post my entire code may have threw things off...I do use a timer in a seperate class. I will include it in the next post.
    "I can't tell if you are using Threads of not."guess you overlooked these lines of code:
                    /* create and start a new thread.  This is EXTREMELY important
                     *as it allows for GUI to be repainted while the soulution
                     *is animated */
                    SolveEngine s = new SolveEngine();
                    s.start();   Here is the code once again:
    public class HanoiFrame extends javax.swing.JFrame {
        private static final int STATE_SETUP = 0;
        private static final int STATE_ANIMATE = 1;
        private static final int POLE_AREA_HEIGHT = 356;
        private static final int POLE_AREA_WIDTH = 205;
        private static final int DISK_ARC_HEIGHT = 15;
        private static final int DISK_ARC_WIDTH = 15;
        private static final int DISK_HEIGHT = 23;
        private static final int MAX_DISKS = 10;
        private static final int MAX_ANIMATION_DELAY = 30000;
        private static final int MAX_PHYSICAL_DELAY = 100000;
        private static final int MIN_DISKS = 1;
        private static final int MIN_ANIMATION_DELAY = 1;
        private static final int MIN_PHYSICAL_DELAY = 1;
        private static final int EMPTY_CELL_VALUE = -1;
        private static final int CELL_HEIGHT = 25;
        private static final int COLUMNS = 3;
        private static final int ROW = 11;
        private  static final int MILLISECONDS_PER_SECOND = 1000;
        private final static String newline = "\n";
        private static Disk[] disks;
        private static Disk newDisk;
        private static int[][] diskCells;
        public static  int startX = 2;
        public static  int startY = 340;
        public static int setDiskAmount = 0;
        public static int setAnimationDelay = 0;
        public static int  setPhysicalDelay = 0;
        public static int  moveNumber = 0;
        public static int intMovesLeft = 0;
        private boolean  buttonPressed = false;
        private double movesLeft = 0;
        private int windowState = 0;
        /** Creates new form HanoiFrame */
        public HanoiFrame() {
            initComponents();
            /* initialize the "poles" to hold a certain number of "cells */
            this.diskCells = new int[ROW][COLUMNS];
            for (int row = ROW - 1;row >= 0; row--) {
                for (int col = 0;col < COLUMNS ; col++) {
                    diskCells[row][col] = EMPTY_CELL_VALUE;
        private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            /* reset all the variable to original state */
            setDiskAmount = 0;
            setAnimationDelay = 0;
            setPhysicalDelay = 0;
            moveNumber = 0;
            movesLeft = 0;
            intMovesLeft = 0;
            buttonPressed = false;
            windowState = 0;
            this.startButton.setEnabled(true);
            this.diskAmount.setEditable(true);
            this.animationDelay.setEditable(true);
            this.physicalDelay.setEditable(true);
            /* reset all the cell vales to empty (this could be its own function) */
            for (int row = ROW - 1;row >= 0; row--) {
                for (int col = 0;col < COLUMNS ; col++) {
                    diskCells[row][col] = EMPTY_CELL_VALUE;
            /* reset the content of the TextFields and Area */
            this.diskAmount.setText("");
            this.animationDelay.setText("");
            this.physicalDelay.setText("");
            this.outTextArea.setText("");
            /* repaint the display panel */
            this.hanoiPanel.repaint();
        /* i have no idea why this is here...It was generated by Netbeans when I
         *made the reset button.  As you can see it made 2.  The one above it the same
         *except it contains code.  Since it was automatically generated
         *I cannot delete it.
        private void resetButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
        /* is executed when the start button is pressed.
         *also executes a field check and intializes key variables. Finally,
         *it executes the solution and generates the solve time
        private void startButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
            /* check if the program has already been run (without being reset) */
            if (!buttonPressed){
                /* check the fields to ensure the input is correct and useable */
                if (checkFields()){
                    /* give button a pressed status if all info valid */
                    buttonPressed = true;
                    windowState = 1;
                    /* disable the button */
                    this.startButton.setEnabled(false);
                    /* disable the fields */
                    this.diskAmount.setEditable(false);
                    this.animationDelay.setEditable(false);
                    this.physicalDelay.setEditable(false);
                    /* setup the disks on the starting pole */
                    setup();
                    /* initialize the number of moves required. 2 to the power of n minus 1 */
                    movesLeft = Math.pow(2, setDiskAmount) - 1;
                    /* convert the number to an integer */
                    intMovesLeft = (int)movesLeft;
                    /* set the physical delay */
                    setPhysicalDelay = Integer.parseInt(this.physicalDelay.getText());
                    /* create and start a new thread.  This is EXTREMELY important
                     *as it allows for GUI to be repainted while the soulution
                     *is animated */
                    SolveEngine s = new SolveEngine();
                    s.start();
         *Check all the fields at once to ensure that they are valid
        private boolean checkFields(){
            String numberOfDisks = null;
            String animationDelay = null;
            String physicalDelay = null;
            numberOfDisks = this.diskAmount.getText();
            animationDelay = this.animationDelay.getText();
            physicalDelay = this.physicalDelay.getText();
            /* initiate my array of error messages */
            ArrayList errMsg = new ArrayList(0);
            /* check if the number of disks was left blank */
            if (numberOfDisks.equals("")){
                errMsg.add("Please enter the Number of Disks (MAX " + MAX_DISKS + ")");
            }else{
                /* check if the input given is valid */
                try {
                    /* parse the disk amount entered into an integer */
                    setDiskAmount = Integer.parseInt(numberOfDisks);
                    /* check the # of disks entered is greater than 0 and less than 10 */
                    if (setDiskAmount < MIN_DISKS){
                        errMsg.add("Number of Disks must be greater than " + MIN_DISKS);
                    } else if (setDiskAmount > MAX_DISKS){
                        errMsg.add("Number of Disks must be less than" + MAX_DISKS);
                } catch (NumberFormatException ex) {
                    errMsg.add("Number of Disks must be a Number");
            /* check if animation delay was left blank */
            if (animationDelay.equals("")){
                errMsg.add("Please enter disk Animation Delay");
            } else {
                /* check if the input given is valid */
                try {
                    /* parse the animation delay entered into an integer */
                    setAnimationDelay = Integer.parseInt(animationDelay);
                    /* check range of animation delay */
                    if (setAnimationDelay < MIN_ANIMATION_DELAY){
                        errMsg.add("Animation Delay must be greater than 0");
                    } else if (setAnimationDelay > MAX_ANIMATION_DELAY){
                        errMsg.add("Animation Delay must be less than " + MAX_ANIMATION_DELAY);
                } catch (NumberFormatException ex) {
                    errMsg.add("Animation Delay must be a Number");
            /* check if physical delay was left blank */
            if (physicalDelay.equals("")){
                errMsg.add("Please enter the Physical disk delay");
            } else {
                /* check if the input given is valid */
                try {
                    /* parse the physical delay entered into an integer */
                    setPhysicalDelay = Integer.parseInt(physicalDelay);
                    /* check check the range of the physical delay */
                    if (setPhysicalDelay < MIN_PHYSICAL_DELAY){
                        errMsg.add("Physical Delay must be greater than 0");
                    } else if (setPhysicalDelay > MAX_PHYSICAL_DELAY){
                        errMsg.add("Physical Delay must be less than " + MAX_PHYSICAL_DELAY);
                } catch (NumberFormatException ex) {
                    errMsg.add("Physical Delay must be a Number");
            /* if there is any problem, add the message to the popup and display */
            if (!errMsg.isEmpty()){
                JOptionPane.showMessageDialog(this, errMsg.toArray());
                return false;
            return true;
         * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new HanoiFrame().setVisible(true);
        /*returns the Animation panel area graphics
         *@return Animation area graphics
        public static Graphics getAnimationArea(){
            /* get the usable area of poleA */
            Container clientArea = hanoiPanel;
            //Container clientArea = this.getContentPane();
            /* get the graphics to the display panel*/
            Graphics gfx = clientArea.getGraphics();
            return gfx;
         *set up the requested amount of disks on the first pole
         *@param diskAmount the amount of disks entered by the user
        public void setup(){
            int numberOfDisks = setDiskAmount;
            this.disks = new Disk[numberOfDisks];
            int diskNumber = 0;
            int diskWidth = POLE_AREA_WIDTH - 4;
            int arcH = DISK_ARC_HEIGHT;
            int arcW = DISK_ARC_WIDTH;
            int x = startX;
            int y = startY;
            int row = 0;
            int col = 0;
            /* get the graphics to the display panel*/
            //Graphics gfx = getAnimationArea();
            /* set the color of the disks */
            //gfx.setColor(Color.MAGENTA);
            /* draw the specified number of disks */
            while (diskNumber < numberOfDisks){
                /* reduce the y position */
                y = startY - diskNumber * CELL_HEIGHT;
                /* draw the disk */
                //gfx.fillRoundRect(x, y, diskWidth, DISK_HEIGHT, arcH, arcW);
                /* create a new instance of disk */
                newDisk = new Disk(x, y, diskWidth);
                /* give the new disk an id */
                this.disks[diskNumber] = newDisk;
                /* add the id to the cell array of poleA */
                this.diskCells[row][col] = diskNumber;
                /* make the disk smaller and center */
                x = x + 8;
                diskWidth = diskWidth - 16;
                /* increase disk number */
                diskNumber++;
                /* move to the next row */
                row++;
            repaint();
         *move the disk from a source pole to the destination pole.
         *this should take the source col and destination col to determine where to draw the
         *disk.  It will also need to know to draw the disk in the first available cell
         *in a col
         *@param source the starting pole of the next move
         *@param destination the end pole of the next move
        public void moveDisk(int src, int dest){
            /* the lines below would not be necessary if I were to pass the Graphics
             *gfx object as a parameter.  I may have to use this option in order
             *to work around the current repainting issue */
            /* get the graphics to the display panel*/
            Graphics gfx = getAnimationArea();
            /* get the id of the disk to be moved */
            int disk = getDiskId(src);
            /* get the to and from rows */
            int sourceRow = getRow(src);
            int destinationRow = getRow(dest);
            /* set the co ordintates of the destination */
            int col = dest * POLE_AREA_WIDTH;
            int x = disks[disk].getStartX() + col;
            int y = startY - (destinationRow * CELL_HEIGHT);
            int width = disks[disk].getDiskWidth();
            disks[disk].setStartX(x);
            disks[disk].setStartY(y);
            disks[disk].setDiskWidth(width);
            //System.out.println("startX " + x);
            //System.out.println("startY " + y);
            //System.out.println("destination row " + destinationRow);
            //System.out.println("disk " + disk);
            //System.out.println("Width " + width);
            diskCells[sourceRow - 1][src] = EMPTY_CELL_VALUE;
            /* set the destination row to the disk id */
            diskCells[destinationRow][dest] = disk;
            drawDisk();
            repaint();
            String output = "";
            //must initialize row to max - 1 and increment 'til = 0
            //since the array position starts at 0
            //this will print the array from the top down
            for (int row = ROW - 1;row >= 0; row--) {
                for (int colm = 0;colm < COLUMNS ; colm++) {
                    output = output + diskCells[row][colm];
                output = output + "\n";
            System.out.println(output);
            System.out.println(newline);
        public void drawDisk(){
            int diskNum = setDiskAmount -1;
            int i = 0;
            Graphics gfx = getAnimationArea();
            gfx.setColor(Color.MAGENTA);
            while(diskNum >= 0){
                //System.out.println("here is the disk IDs " + diskNum);
                //System.out.println("the startY during draw " + disks[diskNum].getStartY());
                gfx.fillRoundRect(disks[diskNum].getStartX(), disks[diskNum].getStartY(), disks[diskNum].getDiskWidth(), DISK_HEIGHT, DISK_ARC_WIDTH, DISK_ARC_HEIGHT);
                diskNum--;
        public void paint(Graphics gfx){
            if(windowState == 0){
                super.paint(gfx);
                setup();
            } else if (windowState == 1){
                super.paint(gfx);
                drawDisk();
         *returns the id of the disk
         *@param col the the designated column
         *@return the disk identification number
        public static int getDiskId(int col){
            int diskIdent = 0;
            /* initialize the row number to max -1, because the array start at 0 */
            int row = MAX_DISKS - 1;
            /* do a cell check while row is greater than 0
             *this is done so that is starts at the top row
             *and moves down
            while (row > EMPTY_CELL_VALUE){
                /* when the first cell that is not -1 is reached get the disk number
                 *that disk will be the smallest in the stack */
                if(diskCells[row][col] != -1){
                    //diskIdent = this.diskCells[row][col];
                    diskIdent =diskCells[row][col];
                    break;
                row--;
            return diskIdent;
         *returns the first available row
         *@param col the designated col
         *@return the first available row number
        public static int getRow(int col){
            int rowNumber = 0;
            /* cycle through the cells until it finds an empty one */
            while (diskCells[rowNumber][col] != -1){
                rowNumber++;
            return rowNumber;
         *calculate the total time it takes for the tower of hanoi game to be
         *solved, given the amount of disk and the amount of time it takes
         *to physically move a disk from one pole to the other.
        public static void totalTime(){
            int timeInSeconds = setPhysicalDelay * intMovesLeft;
            int i = 0;
            int seconds = 0;
            int minute = 0;
            int hour = 0;
            int day = 0;
            int month = 0;
            int year = 0;
            while (i < timeInSeconds){
                if(seconds > 60){
                    minute++;
                    seconds = 0;
                if(minute > 60){
                    hour++;
                    minute = 0;
                if(hour > 24){
                    day++;
                    hour = 0;
                if(day > 30){
                    month++;
                    day = 0;
                if(month > 12){
                    year++;
                    month = 0;
                seconds++;
                i++;
            updateMessageCenter("It would take:" + year + " years " + month + " months " + day +
                    " days " + hour + " hours " + minute + " minutes and " + seconds + " seconds, to move " + setDiskAmount +
                    " disks at " + setPhysicalDelay + " seconds per move");
         *add the message to the diplay window
         *currently this is not necessary, but may be required to
         *dynamically display the contents of message into the text area
         *@param message the message to be added/appended the the text area
        public static void updateMessageCenter(String message){
            /* append text to the message */
            outTextArea.append(message + newline);
            /* ensure the the most recent line is viewable - auto scroll */
            outTextArea.setCaretPosition(outTextArea.getDocument().getLength());
        public class SolveEngine extends Thread {
            /** Creates a new instance of SolveEngine */
            public SolveEngine() {
            public void run(){
                /* start the engine/animation */
                solutionEngine(HanoiFrame.setDiskAmount,0, 2, 1);
                /* calculate and print the total time after the solution is complete */
                HanoiFrame.totalTime();
         *the recursive solution to the Towers of Hanoi problem
         *@param diskAmount the number of disks that need to be moved
         *@param start the identity of the starting column
         *@param end the identity of the ending column
         *@param temp the identity of the temporary column
            public void solutionEngine(int diskAmount,int start, int end, int temp){
                int disks = diskAmount;
                int startPole = start;
                int endPole = end;
                int tempPole = temp;
                try {
                    if ( disks == 1 ) {
                        HanoiFrame.moveNumber++;
                        HanoiFrame.updateMessageCenter("Moving disk from  "+ startPole + " to "+ endPole + " Move #: " + HanoiFrame.moveNumber + " of " + HanoiFrame.intMovesLeft);
                        /* delay the movement of the disk */
                        Thread.sleep(HanoiFrame.setAnimationDelay);
                        /* move the disk */
                        moveDisk(startPole, endPole);
                    } else {
                        solutionEngine( disks - 1, startPole, tempPole, endPole);
                        HanoiFrame.moveNumber++;
                        HanoiFrame.updateMessageCenter("Moving disk from  "+ startPole + " to "+ endPole + " Move #: " + HanoiFrame.moveNumber + " of " + HanoiFrame.intMovesLeft);
                        /* delay the movement of the disk */
                        Thread.sleep(HanoiFrame.setAnimationDelay);
                        moveDisk(startPole, endPole);
                        solutionEngine( disks - 1, tempPole, endPole, startPole);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
        }

  • A problem with custom painting.

    I was trying to make a bar chart from a given data. I used an extension JLabel (mentioned as inner class Leonardo) inside a scrollpane for the custom painting of the bars (rectangles).
    The problem I am facing is the component is not visible when I open the screen. If I drag the scrool bar then the next area is painted and the component looks fine. If the frame is minimized and then maximized the custom painting is not visible. Any idea why it is doing so? I am pasting the full code in case some body wants to run it.
    Thanks in advance,
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Enumeration;
    import java.awt.geom.*;
    public class Reporter extends JFrame
    private JPanel basePanel = new JPanel();
    private Leonardo1 leo = null;
    private JScrollPane scrollPane = new JScrollPane();
    public Reporter()
    try
    jbInit();
    catch(Exception e)
    e.printStackTrace();
    public static void main(String[] args)
    Reporter reporter = new Reporter();
         reporter.pack();
         reporter.setSize(700,500);
    reporter.setVisible(true);
    private void jbInit() throws Exception
    this.setSize(new Dimension(679, 497));
    this.getContentPane().setLayout(null);
    this.setTitle("Process Reporter");
    // this.addWindowListener(new Reporter_this_windowAdapter(this));
    basePanel.setLayout(null);
    basePanel.setBounds(new Rectangle(15, 60, 640, 405));
    basePanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
    /* Hard code data */
    Vector d= new Vector();
    for (int j=0; j<131;j++)
    Data d1 = new Data(j*j*1.0,new java.sql.Date(System.currentTimeMillis()+j*1000000000),"yy","xx",Color.red);
    d.add(d1);
    leo = new Leonardo1(d);
    /*End of Hard Code data*/
    leo.setBounds(new Rectangle(10, 65, 656, 346));
    scrollPane.setBounds(new Rectangle(10, 10, 620, 330));
    scrollPane.getViewport().add(leo);
    basePanel.add(scrollPane, null);
    this.getContentPane().add(basePanel, null);
    class Data implements java.io.Serializable
    public double time;
    public java.sql.Date day;
    public String timeText;
    public String dayText;
    public Color color;
    public Data(double t,java.sql.Date dt, String strT,String strD,java.awt.Color col)
    time = t;
    day=dt;
    timeText=strT;
    dayText=strD;
    color=col;
    public void setTime(double t) {time =t;}
    public void setDay(java.sql.Date t) {day =t;}
    public void setTimeText(String t) {timeText =t;}
    public void setDayText(String t) {dayText =t;}
    public void setColor(Color t) {color =t;}
    public double getTime() {return time;}
    public java.sql.Date getDay() {return day;}
    public String getTimeText() {return timeText;}
    public String getDayText() {return dayText;}
    public Color getColor() {return color;}
    class Leonardo1 extends JLabel implements java.io.Serializable
    private Vector dataVec = new Vector();
    private double xGap = 5.0;//Gap between 2 bars
    private double xWidth = 12.0;//Width of a bar
    private double yGap = 40.0;//Gap from start of Y axis to first data.
    private int xLength = 645;//Length of the component
    private int yLength = 330;//Height of the component
    private int xMargin = 50;//The gap from the corner of the axis to the y axis
    private int yMargin = 20;//The gap from the corner of the axis to the x axis
    private double avgTime = 0.0;
    protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
    public int getWidth(){ return xLength;}
    public int getHeight(){ return yLength;}
    public Dimension getMaximumSize(){return new Dimension(xLength,yLength);}
    public Dimension getMinimumSize(){return new Dimension(xLength,yLength);}
    public Dimension getSize(Dimension rv){ return new Dimension(xLength,yLength);}
    public Rectangle getBounds(Rectangle rv){ return new Rectangle(xLength,yLength); }
    public Leonardo1(Vector vec) {
         super();
    setOpaque(true);
    super.setBackground(Color.white);
    setData(vec);
    setBorder(noFocusBorder);
    protected void setData(Vector vec){
    if (vec == null) return;
    dataVec = (Vector)vec.clone();
    public void paintComponent(Graphics gin)
    super.paintComponent(gin);
    Graphics2D g2 = (Graphics2D)gin;
         // setBackground(Color.white);
    Color bl = Color.blue;
    g2.setPaint(bl);
    g2.setStroke(new BasicStroke(5.0f));
    drawData(g2,dataVec);
    g2.setPaint(Color.gray);
    g2.setStroke(new BasicStroke(0.5f));
    g2.draw(new Rectangle2D.Double(0, 0,xLength,yLength));
    g2.setStroke(new BasicStroke(1.5f));
    g2.draw(new Line2D.Double(xMargin, yMargin,xMargin,yLength -yMargin));
    g2.draw(new Line2D.Double(xMargin, yLength -yMargin,xLength -xMargin,yLength -yMargin));
    System.out.println("End1 ");
    public Dimension getPreferredSize(){
    if (dataVec.size()*(xGap+xWidth) > xLength)
    xLength = (int)(dataVec.size()*(xGap+xWidth));
    return new Dimension(xLength,yLength);
    else
    return new Dimension(xLength,yLength);
    protected void drawData(Graphics2D g2, Vector vec)
    if (vec == null || vec.size() == 0)
    return;
    //Check if expansion is required or not
    if (vec.size()*(xGap+xWidth) > (xLength - 2*xMargin))
    xLength = (int)(vec.size()*(xGap+xWidth)+xGap);//Expanded
    setPreferredSize(new Dimension(xLength,yLength));
    double maxTime = 0.0;
    double minTime = 0.0;
    double t=0.0;
    double total=0.0;
    for (int i=0;i<vec.size();i++)
    if ((t = ((Data)vec.elementAt(i)).getTime()) > maxTime)
    maxTime = t;
    if (t < minTime)
    minTime =t;
    total += t;
    avgTime = total/vec.size();
    System.out.println("Avg:"+avgTime+" tot:"+total);
    double scale = (yLength - 2*yMargin - 2*yGap )/(maxTime - minTime);
    //Now the y-axis scale is calculated, we can draw the bar.
    //Currently only bar is supported
    //I asume data are in the order of dates otherwise have to sort it here
    for (int i=1;i<=vec.size();i++)
    //Set color to fill
    if (((Data)vec.elementAt(i-1)).getColor() != null)
    g2.setPaint(((Data)vec.elementAt(i-1)).getColor() );
              //Fill
    g2.fill(new Rectangle2D.Double(xMargin+xGap+(i-1)*(xGap+xWidth), yMargin+yGap+/*length of active y axis*/
    (yLength-2*yMargin-2*yGap)-/*y value converted to scale*/(((Data)vec.elementAt(i-1)).getTime()- minTime)*scale,
    xWidth,(((Data)vec.elementAt(i-1)).getTime()- minTime)*scale+yGap));
    g2.setPaint(Color.black);
    g2.setStroke(new BasicStroke(0.5f));
    g2.draw(new Rectangle2D.Double(xMargin+xGap+(i-1)*(xGap+xWidth), yMargin+yGap+/*length of active y axis*/
    (yLength-2*yMargin-2*yGap)-/*y value converted to scale*/(((Data)vec.elementAt(i-1)).getTime()- minTime)*scale,
    xWidth,(((Data)vec.elementAt(i-1)).getTime()- minTime)*scale+yGap));
    g2.setPaint(Color.blue);
    float[] dash1 = {10.0f};
    g2.setStroke(new BasicStroke(1.5f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f));
    g2.draw(new Line2D.Double(xMargin,
    yMargin+yGap+/*length of active y axis*/(yLength-2*yMargin-2*yGap)-
    /*y value converted to scale*/(avgTime- minTime)*scale,
    xMargin+(xGap+xWidth)*vec.size()+xGap,
    yMargin+yGap+/*length of active y axis*/(yLength-2*yMargin-2*yGap)-
    /*y value converted to scale*/(avgTime- minTime)*scale));

    Replace getBounds(Rectangle) by the following.
    public Rectangle getBounds(Rectangle rv){
    if (rv != null) {
    rv.x = 0; rv.y = 0; rv.width = xLength; rv.height = yLength;
    return rv;
    } else
    return new Rectangle(xLength,yLength);

  • Drawing Graphics in AWT panel

    I was wondering if anybody knows how I would go about drawing onto a Panel as a result of an actionPerformed() call. I got the original code for displaying an image onto the Panel from aresICO on this forum and now I need to be able to click a button on the GUI and have something drawn onto that Panel.
    import java.applet.*;
    import java.awt.*;
    import java.awt.image.ImageObserver;
    public class iPane extends Panel {
         Image im;
         boolean loaded = false;
         public iPane () {
              Toolkit tk = Toolkit.getDefaultToolkit ();
              im = tk.getImage ("Map of Dublin.jpg");
              prepareImage (im, -1, -1, this);
         public void paint (Graphics g) {
              if (loaded)
              g.drawImage (im, 20, 20, this);
         public void update (Graphics g) {
              paint (g);
              // if proper values loaded then:
              //drawRoute (g);
         public void drawRoute (Graphics g) {
              g.setColor(Color.red);
              g.drawLine(273,51,334,61);
              // method takes in values from a file etc.
         public synchronized boolean imageUpdate (Image image, int infoFlags,int x, int y, int width, int height) {
              if ((infoFlags & ImageObserver.ALLBITS) != 0) {
                   loaded = true;
                   repaint();
                   return false;
              else {
                   return true;
         public Dimension getPreferredSize() {
              return new Dimension(im.getWidth(this),im.getHeight(this));
         public Dimension getMinimumSize() {
              return getPreferredSize();
    }; // end iPane class
    This is a the relevant part of the class which uses an iPane object:
    iPane mapPanel;
    public void actionPerformed(ActionEvent e)
         String buttonLabel = e.getActionCommand();
         if (e.getSource() instanceof Button)
              if (buttonLabel.equals("Get Directions"))     
    // What goes in here to draw a line for example?
    // How do I call a method from iPane to draw something?
    Thanks.

    Davey,
    I don't think your approach is correct. The golden rule of Java graphics is that ALL of painting on a component should be done within the componen'ts paint method(). If you write to iPane from outside the class, then if your iPanel needs to be repainted (because, for example, it got obscured by another window and then unobscured) then the route would not be shown.
    You have to think OOD on this.
    The route and the image are properties of the of iPanel. The button push requests a change of those properties and display of the results on the screen.
    This is what I would do:
    1. Create member variables in the class iPanel that define the route.
    2. Have a method which sets those variables. This method should call repaint after setting the variables.
    2. Maybe have another boolean which says whether the routine is to be displayed or not. This method should call repaint after setting the boolean.
    3. Change the paint method to draw the route, if required.
    4. In the class that uses iPanel, simply call the methods in iPanel that set the route properties.
    5. Change your class name to IPanel. All class names should start with an upper case letter.
    e.g.,
    private Point start;
    private Point end;
    private boolean displayRoute
    public setRoute(Point newStart, Point newEnd) {
    start = newStart;
    end = newEnd;
    repaint(); // maybe only call if new value is different
    public void setDisplayRoute(boolean value) {
    displayRoute = value;
    repaint(); // maybe only call if new value is different
    public void paint(Graphics g) {
    g.drawImage( ..... blah
    if (displayRoute) {
    g.drawline ( .... blah
    // Then
    onActionPerformed
    iPanel.setRoute(new Point(1,2), new Point(3,4));

  • TreeUI custom painting

    I am trying to paint a pattern within the visible rect of a JTree. I am doing the painting in a custom TreeUI class. My pattern should always appear at the bottom of the tree even while a user is scrolling.
    This sounded very easy. I would just retreive the visibleRect of the JTree the UIDelegate is managing within paint(Grphics,JComponent), and simply call g2D.setPaint(texturePattern), g2D.fill(rect);. This does not work well at all. What happens is the tree does not show the pattern while scrolling, although the paint is being called. When I expand a node, then it works. If I scroll up and then back down, I noticed a smeared version of pattern near the bottom of the tree.
    Here is my little test. I am putting it here so someone can try it out. This painting issue is hard to explain but easy to recreate. This version just tries to draw a red 20X20 rectangle at the top left corner of the tree, following the visibleBounds.
    public class TestTreeUI extends BasicTreeUI
       public static ComponentUI createUI(JComponent c)
          return new TestTreeUI();
       public void paint(Graphics g, JComponent c)
          Rectangle r = tree.getVisibleRect();
          // this always prints the right rect but does not always paint correctly
          System.out.println(r);
          g.setColor(Color.red);
          g.drawRect(r.x, r.y, 20, 20);
          super.paint(g,c);
    }Any clues would be greatly appreciated.

    bump

  • How can i call the public void paint(Graphics g) method on click of button?

    Hello,
    I have done this before i know its got to do something with the repaint() method but can someone show me with and example and call the paint method using AWT in a frame not in an applet....
    Thank you very much.

    Thank You very Much.... You have cleared all my doubts.
    But my main objective is to display an image in an AWT Frame !!
    here is the code that i have been trying..
    import java.awt.*;
    import java.awt.event.*;
    public class check6 extends Panel
         Frame f;
         Image i;
         public check6()
              f = new Frame("Heading");
              f.addWindowListener(new WindowAdapter(){
                   public void windowClosing(WindowEvent e){
                        System.exit(0);
              final Button b1 = new Button("Submit");
              b1.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e){
                        repaint();
              i = getImage(getCodeBase(), "titanic.jpg");
              add(b1);
              setLayout(new FlowLayout(FlowLayout.LEFT));
              f.add(this);
              f.setSize(500,500);
              f.setVisible(true);
         public void paint(Graphics g)
              g.drawImage(i,10,10,this);
         public static void main(String args[])
              new check6();
    }

  • Custom painting in JFC

    I have an app that does custom painting in a extended JWindow, however, when I add controls (an extended JButton that does custom painting) the entire window, not including any components turns gray. This happens if I use paint or paintComponent. I am starting to think that the delegate UI is causing a problem, this is because if I remove the paintComponents, I get the image as usual. Any ideas???
    A code snippet shows the gist
    class sWindow extends JWindow {
    public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(mainform, null, 0, 0);
    paintComponents(g);
    class sButton extends JButton {
    public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    switch(state) {
    case DEFAULT:
    g2.drawImage(defaultImage, 0, 0, getSize().width, getSize().height, this);
    break;
    case PRESSED:
    g2.drawImage(pressedImage, 0, 0, getSize().width, getSize().height, this);
    break;
    case HIGHLIGHTED:
    g2.drawImage(hilightedImage, 0, 0, getSize().width, getSize().height, this);
    break;
    case DISABLED:
    g2.drawImage(disabledImage, 0, 0, getSize().width, getSize().height, this);
    break;
    }

    Hi,
    You have problem only when adding custom painting JPanel to the same custom painting JPanel??? Are you layering your painting methods??? Like the most upper JPanel paints and then the childs??? What do you get for drawings??? Do you use invoque later method??? What is your priority on the Threads you are using???
    JRG

  • Custom painting on jpanel with scrollable

    i have a jpanel with custom painting. because the painting can be wider than the view of the panel i need to implement scrollbars. i have experimented with implementing scrollable and using scrollpanes but i have no result. i need a hint or a tutorial how this can be done

    ok, that was the key. but perhaps you can help me with two other problems:
    because i dont know how large my panel ( or my scroll-area) has to be, i need to expand it on demand. i tried setpreferredsize for that reason, but nothing happens. i think this method is only for initializing but doesnt seem to function properly at a later point.
    next prop:
    when i scroll my custom panting area the jpanel isnt repainted. i have some difficulties finding the right eventlistener so that i can get scrollbar events (repaint after each move)

Maybe you are looking for