Results 1 to 3 of 3
  1. #1
    KernelPanic is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default [Java 7 SE+MySQL+NetBeans 7.2.1+Graphics (2D)]paint and paintComponent problem

    Dear Sirs and Madams!

    In the following code, I have class, subclassed from JPanel, which acts as my "drawing board"-canvas. In this class I fetch records from database and I want to show them as JLabel elements in JPanel's GridLayout. In addition to I have implemented following functionalities in this class:
    • drawing AutoCad like mouse cursor (cross cursor), which is drawn on mouseMove() event
    • drawing Ruler

    Now, I had to implement double buffering, because screen was flickering if I moved a mouse, but now I do not see neither ruler neither my JLabel items, fetched from mysql database. Since I do not understand Java Graphics yet, I've overriden methods paint() and paintComponents and I think, I've messed things up. Can someme give me a little guidance? Here is the code:
    Java Code:
    /**
     *
     */
    package ha.configurator.gui;
    
    import ha.global.dbmanager.CDatabaseManager;
    import ha.global.dbmanager.entities.CEntityMultimedia;
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.Stroke;
    import java.awt.Toolkit;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    import java.awt.image.BufferedImage;
    import java.text.NumberFormat;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EtchedBorder;
    
    /**
     * @author markof
     */
    public class CPanelGraphics extends JPanel implements MouseListener,
            MouseMotionListener,MouseWheelListener
    {
            private class CZoomHandler implements MouseWheelListener
            {
                @Override
                public void mouseWheelMoved(MouseWheelEvent e)
                {
                    double distanceX=maxX-minX;
                    double distanceY=maxY-minY;
    
                    double cursorX=minX+distanceX/2.0;
                    double cursorY=minY+distanceY/2.0;
    
                    int rotation=e.getWheelRotation();
                    if(rotation<0)
                    {
                        distanceX/=COEFF_ZOOM;
                        distanceY/=COEFF_ZOOM;
                    }
                    else
                    {
                        distanceX*=COEFF_ZOOM;
                        distanceY*=COEFF_ZOOM;
                    }
    
                    minX = cursorX - distanceX / 2.0;
                    maxX = cursorX + distanceX / 2.0;
                    minY = cursorY - distanceY / 2.0;
                    maxY = cursorY + distanceY / 2.0;
    
                    repaint();
                }
            }   // class CZoomHandler
    
        private static final long serialVersionUID=7300896943408520338L;
    
        public static final int GRID_NUMBER_OF_COLUMNS=4;
        public static final int GRID_NUMBER_OF_ROWS=4;
        public static final int GRID_GAP_HORIZONTAL=2;
        public static final int GRID_GAP_VERTICAL=2;
        private static final Color COLOR_BACKGROUND=Color.WHITE;
        private static final Color COLOR_MAJOR_GRID=Color.RED/*GRAY.brighter()*/;
        private static final Color COLOR_MINOR_GRID=Color.RED.brighter();/*new Color(220,
                220,
                220);*/
        private static final Color COLOR_AXIS=Color.BLACK;
        private static final float STROKE_AXIS=1.2f;
        private static final float STROKE_GRID=1.0f;
        private static final float COEFF_ZOOM=1.1f;
        private int m_iMouseXPos=0;
        private int m_iMouseYPos=0;
        private Image mImage;
    
        private GridLayout m_oLayouMain=new GridLayout(CPanelGraphics.GRID_NUMBER_OF_ROWS,
                CPanelGraphics.GRID_NUMBER_OF_COLUMNS,
                CPanelGraphics.GRID_GAP_HORIZONTAL,
                CPanelGraphics.GRID_GAP_VERTICAL);
        protected GridLayout layoutMain()
            { return this.m_oLayouMain; }
        protected void setLayoutMain(final GridLayout gl /* GridLayout */)
            { this.m_oLayouMain=gl; }
    
        private List<CEntityMultimedia> m_oListMultimediaEntities=new ArrayList<>();
        protected List<CEntityMultimedia> listMultimediaEntites()
            { return this.m_oListMultimediaEntities; }
        protected void setListMultimediaEntities(final ArrayList<CEntityMultimedia> lae /* ListAbstractEntity */)
            { this.m_oListMultimediaEntities=lae; }
    
        private List<JLabel> m_oListMultimediaEntitesButtons=new ArrayList<>();
        protected List<JLabel> listMultimediaEntitiesButtons()
            { return this.m_oListMultimediaEntitesButtons; }
        protected void setListMultimediaEntitiesButtons(final List<JLabel> lmmeb /* ListMultiMediaListButtons */)
            { this.m_oListMultimediaEntitesButtons=lmmeb; }
    
        protected double minX;
        protected double maxX;
        protected double minY;
        protected double maxY;
    
        private double originX;
        private double originY;
    
        private double majorX;
        private int minorX;
        private double majorY;
        private int minorY;
    
        private NumberFormat formatter;
    
        private CZoomHandler zoomHandler;
    
        /**
         *
         */
        public CPanelGraphics()
        {
            super();
    
            this.init();
        }   // default constructor
    
        @Override
        public void mouseClicked(final MouseEvent me)
        {
    //        this.m_iMouseXPos=(int)me.getPoint().getX();
    //        this.m_iMouseYPos=(int)me.getPoint().getY();
    //
    //        final Graphics g=this.getGraphics();
    //        g.drawString("("+Integer.toString(this.m_iMouseXPos)+", "
    //                +Integer.toString(this.m_iMouseYPos)+")", this.m_iMouseXPos-10,
    //                this.m_iMouseYPos+10);
    //        // g.dispose();
        }
    
        @Override
        public void mouseDragged(final MouseEvent me)
        {
        }
    
        @Override
        public void mouseEntered(final MouseEvent me)
        {
        }
    
        /*
         * (non-Javadoc)
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
         */
        @Override
        public void mouseExited(final MouseEvent me)
        {
        }
    
        @Override
        public void mouseMoved(final MouseEvent me)
        {
            final Graphics g=this.getGraphics();
    
            this.m_iMouseXPos=(int)me.getPoint().getX();
            this.m_iMouseYPos=(int)me.getPoint().getY();
    
            this.repaint();
        }   // mouseMoved
    
        /*
         * (non-Javadoc)
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
         */
        @Override
        public void mousePressed(final MouseEvent me)
        {
        }
    
        /*
         * (non-Javadoc)
         * @see
         * java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
         */
        @Override
        public void mouseReleased(final MouseEvent me)
        {
        }
    
        /*
         * (non-Javadoc)
         * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.
         * MouseWheelEvent)
         */
        @Override
        public void mouseWheelMoved(final MouseWheelEvent e)
        {
        }
    
        @Override
        public void paint(Graphics g)
        {
            Graphics2D g2=(Graphics2D)g;
            Stroke stroke=g2.getStroke();
            Dimension d=getSize();
    
            this.checkOffScreenImage();
            Graphics offG=mImage.getGraphics();
            offG.setColor(getBackground());
            offG.fillRect(0,
                    0,
                    d.width,
                    d.height);
            this.paintOffScreen(mImage.getGraphics());
            g.drawImage(mImage,
                    0,
                    0,
                    null);
        }   // paint
    
        @Override
        protected void paintComponent(Graphics g)
        {
            Graphics2D g2=(Graphics2D)g;
    
            this.drawGrid(g2);
            this.drawAxis(g2);
        }   // paintComponent
    
        private void init()
        {
            final Cursor blankCursor=Toolkit.getDefaultToolkit()
                    .createCustomCursor(
                            new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB),
                            new Point(0, 0), "blank cursor");
    
            this.formatter=NumberFormat.getInstance();
            this.zoomHandler=new CZoomHandler();
            this.addMouseWheelListener(this.zoomHandler);
            this.setCursor(blankCursor);
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
            this.addMouseWheelListener(this);
            this.setDoubleBuffered(true);
            this.initLayout();
        }   // init
    
        private void initLayout()
        {
            this.setLayout(this.layoutMain());
        }   // initLayout
    
        /**
         *
         * @throws Exception
         */
        public void populatePanelWithItems() throws Exception
        {
            try
            {
                int iNumberOfColumns=GRID_NUMBER_OF_COLUMNS;
                int iNumberOfRows=0;
                int iNumberOfElementsLastRow=0;
    
                this.setListMultimediaEntities(CDatabaseManager.getMultimediaEntitiesArrayList());
                int iNumberOfElements=this.listMultimediaEntites().size();
                iNumberOfRows=iNumberOfElements/iNumberOfColumns;
                iNumberOfElementsLastRow=iNumberOfElements%iNumberOfColumns;
    
                for(int iIndexY=0; iIndexY<iNumberOfRows; iIndexY++)
                {
                    if((iIndexY==(iNumberOfRows-1))&&(iNumberOfElementsLastRow>0))
                    {
                        for(int iIndexX=0; iIndexX<iNumberOfElementsLastRow; iIndexX++)
                        {
                            JLabel item=new JLabel(this.listMultimediaEntites().
                                    get(iIndexY*iNumberOfColumns+iIndexX).name());
                            item.setBorder(new EtchedBorder(EtchedBorder.RAISED));
                            //item.setBackground(Color.WHITE);
                            this.listMultimediaEntitiesButtons().add(item);
                        }
                    }
                    else
                    {
                        for(int iIndexX=0; iIndexX<iNumberOfColumns; iIndexX++)
                        {
                            JLabel item=new JLabel(this.listMultimediaEntites().
                                    get(iIndexY*iNumberOfColumns+iIndexX).name());
                            item.setBorder(new EtchedBorder(EtchedBorder.RAISED));
                            this.listMultimediaEntitiesButtons().add(item);
                        }   // x-coord loop
                    }   // if
                }   // y-coord loop
    
                for(int iIndex=0; iIndex<this.listMultimediaEntitiesButtons().size(); iIndex++)
                {
                    this.add(this.listMultimediaEntitiesButtons().get(iIndex));
                    //this.listMultimediaEntitiesButtons().get(iIndex).setVisible(true);
                }   // for
            }
            catch(Exception ex)
            {
                throw(ex);
            }   // try-catch
        }   // populatePanelWithItems
    
        private void checkOffScreenImage()
        {
            Dimension d=getSize();
            if(mImage==null||
                    mImage.getWidth(null)!=d.width||
                    mImage.getHeight(null)!=d.height)
            {
                mImage = createImage(d.width,
                        d.height);
            }
        }   // checkOffScreenImage
    
        private void paintOffScreen(Graphics g)
        {
            g.drawLine(this.m_iMouseXPos,
                    0,
                    this.m_iMouseXPos,
                    this.getHeight());
            g.drawLine(0,
                    this.m_iMouseYPos,
                    this.getWidth(),
                    this.m_iMouseYPos);
    /*
            g.drawString("("+Integer.toString(this.m_iMouseXPos)+", "
                    +Integer.toString(this.m_iMouseYPos)+")", this.m_iMouseXPos+10,
                    this.m_iMouseYPos-10);
    */
        }   // paintOffScreen
    
        private void drawHorizontalGrid(Graphics2D g2)
        {
            double minorSpacing=majorY/minorY;
            double axisV=xPositionToPixel(originX);
    
            Stroke gridStroke=new BasicStroke(STROKE_GRID);
            Stroke axisStroke=new BasicStroke(STROKE_AXIS);
    
            for(double y=originY+majorY; y<maxY+majorY; y+=majorY)
            {
                g2.setStroke(gridStroke);
                g2.setColor(COLOR_MINOR_GRID);
                for(int i=0; i<minorY; i++)
                {
                    int position=(int)yPositionToPixel(y-i*minorSpacing);
                    g2.drawLine(0,
                            position,
                            getWidth(),
                            position);
                }   // for
    
                int position=(int)yPositionToPixel(y);
                g2.setColor(COLOR_MAJOR_GRID);
                g2.drawLine(0,
                        position,
                        getWidth(),
                        position);
    
                g2.setStroke(axisStroke);
                g2.setColor(COLOR_AXIS);
                g2.drawLine((int)axisV-3,
                        position,
                        (int)axisV+3,
                        position);
            }
        }   // drawHorizontalGrid
    
        private void drawVerticalGrid(Graphics2D g2)
        {
            double minorSpacing=majorX/minorX;
            double axisH=yPositionToPixel(originY);
    
            Stroke gridStroke=new BasicStroke(STROKE_GRID);
            Stroke axisStroke=new BasicStroke(STROKE_AXIS);
    
            for(double x=originX+majorX; x<maxX+majorX; x+=majorX)
            {
                g2.setStroke(gridStroke);
                g2.setColor(COLOR_MINOR_GRID);
                for(int i=0; i<minorX; i++)
                {
                    int position=(int)xPositionToPixel(x-i*minorSpacing);
                    g2.drawLine(position,
                            0,
                            position,
                            getHeight());
                }
    
                int position=(int)xPositionToPixel(x);
                g2.setColor(COLOR_MAJOR_GRID);
                g2.drawLine(position,
                        0,
                        position,
                        getHeight());
    
                g2.setStroke(axisStroke);
                g2.setColor(COLOR_AXIS);
                g2.drawLine(position,
                        (int)axisH-3,
                        position,
                        (int)axisH+3);
            }   // for
    
            for(double x=originX-majorX; x>minX-majorX; x-=majorX)
            {
                g2.setStroke(gridStroke);
                g2.setColor(COLOR_MINOR_GRID);
                for (int i=0; i<minorX; i++)
                {
                    int position=(int)xPositionToPixel(x+i*minorSpacing);
                    g2.drawLine(position,
                            0,
                            position,
                            getHeight());
                }
    
                int position=(int)xPositionToPixel(x);
                g2.setColor(COLOR_MAJOR_GRID);
                g2.drawLine(position,
                        0,
                        position,
                        getHeight());
    
                g2.setStroke(axisStroke);
                g2.setColor(COLOR_AXIS);
                g2.drawLine(position,
                        (int)axisH-3,
                        position,
                        (int)axisH+3);
            }
        }   // drawVerticalGrid
    
        private void drawAxis(Graphics2D g2)
        {
            double axisH=yPositionToPixel(originY);
            double axisV=xPositionToPixel(originX);
    
            g2.setColor(COLOR_AXIS);
            Stroke stroke=g2.getStroke();
            g2.setStroke(new BasicStroke(STROKE_AXIS));
    
            g2.drawLine(0,
                    (int)axisH,
                    getWidth(),
                    (int)axisH);
            g2.drawLine((int)axisV,
                    0,
                    (int)axisV,
                    getHeight());
    
            FontMetrics metrics=g2.getFontMetrics();
            g2.drawString(formatter.format(0.0),
                    (int)axisV+5,
                    (int)axisH+metrics.getHeight());
    
            g2.setStroke(stroke);
        }
    
        private void drawGrid(Graphics2D g2)
        {
            Stroke stroke=g2.getStroke();
    
            this.drawVerticalGrid(g2);
            this.drawHorizontalGrid(g2);
    
    //        if (drawText)
    //        {
    //            drawVerticalLabels(g2);
    //            drawHorizontalLabels(g2);
    //        }   // if
    
            g2.setStroke(stroke);
        }   // drawGrid
    
        protected double yPositionToPixel(double position)
        {
            double height=(double)getHeight();
    
            return height-((position-minY)*height/(maxY-minY));
        }
    
        protected double xPositionToPixel(double position)
        {
            return (position-minX)*(double)getWidth()/(maxX-minX);
        }
    
        protected double xPixelToPosition(double pixel)
        {
            double axisV=xPositionToPixel(originX);
    
            return (pixel-axisV)*(maxX-minX)/(double)getWidth();
        }
    
        protected double yPixelToPosition(double pixel)
        {
            double axisH=yPositionToPixel(originY);
    
            return (getHeight()-pixel-axisH)*(maxY-minY)/(double)getHeight();
        }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: [Java 7 SE+MySQL+NetBeans 7.2.1+Graphics (2D)]paint and paintComponent problem

    Moved to the Swing forum.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: [Java 7 SE+MySQL+NetBeans 7.2.1+Graphics (2D)]paint and paintComponent problem

    Quote Originally Posted by KernelPanic View Post
    I've overriden methods paint() and paintComponents and I think, I've messed things up.
    Yup. Neither one is the correct method to override.

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 5
    Last Post: 10-02-2011, 02:55 AM
  2. paint and paintcomponent
    By wired-in=p in forum New To Java
    Replies: 3
    Last Post: 10-01-2011, 06:49 PM
  3. Graphics wont show up(paintComponent)
    By TheBreadCat in forum New To Java
    Replies: 3
    Last Post: 02-13-2011, 06:00 PM
  4. paint vs paintComponent for a JPanel
    By lightstream in forum AWT / Swing
    Replies: 4
    Last Post: 01-29-2009, 02:26 AM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 03:43 AM

Posting Permissions

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