Simple Paint Program Issue

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 863 Views - Last Post: 02 March 2012 - 05:08 PM Rate Topic: -----

#1 Tronex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-December 11

Simple Paint Program Issue

Posted 22 February 2012 - 10:28 AM

Hi,

Im in the process of making a paint application for my class and I'm currently having some difficulty with getting the freehand drawing tool to work (Which is basically just drawing many small squares). We have been told to use arrays in which will hold different information such as position, size, color, etc as shown below.

private final int MAX_FREEHAND_PIXELS = 1000;
    private Color[] freehandColour = new Color[MAX_FREEHAND_PIXELS];
    private int[][] fxy = new int[MAX_FREEHAND_PIXELS][3];
    private int freehandPixelsCount = 0;


The next thing I have tried to do is get the values for the array which will hold the position and size.

public void mouseDragged(MouseEvent event)
            {
                
                
                //Freehand Drawing Array
                fxy[freehandPixelsCount][0] = event.getX();
                fxy[freehandPixelsCount][1] = event.getY();
                fxy[freehandPixelsCount][2] = 10;
                

                mouseMoved(event);
                canvas.repaint();
            }
        }


However Im now not sure how I get from here to actually drawing these small squares on the screen. Ive used the g.fillRect method in the past so I'm guessing this could be used but im just struggling with bringing all this together.

Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Paint Program Issue

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Simple Paint Program Issue

Posted 22 February 2012 - 12:02 PM

The same answer as this applies
Was This Post Helpful? 0
  • +
  • -

#3 Tronex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-December 11

Re: Simple Paint Program Issue

Posted 22 February 2012 - 12:26 PM

Ive looked at a few different basic paint apps and not many of them use arrays like I am in this program. I understand that this probably isnt the most efficent thing to do but I have to use them in this case.
Was This Post Helpful? 0
  • +
  • -

#4 Tronex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-December 11

Re: Simple Paint Program Issue

Posted 22 February 2012 - 01:40 PM

Can anybody help me with getting the values from the 2d array and drawing the squares on a canvas.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Simple Paint Program Issue

Posted 22 February 2012 - 02:22 PM

Quote

not many of them use arrays like I am in this program.


That's because an array isn't really a suitable container for drawing objects, being inherently fixed size.

But actually that doesn't matter as the difference is trivial. Just imagine the Vector called 'lines' in the example i cited is your array
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Simple Paint Program Issue

Posted 22 February 2012 - 03:02 PM

You have a

freehandPixelsCount++;

missing in your mouseDragged() method.

In your paintComponenet method simply do a

for(int i = 0; i < freehandPixelsCount; ++i) {
and use g.drawRectangle(... for each of your points
Was This Post Helpful? 0
  • +
  • -

#7 Tronex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-December 11

Re: Simple Paint Program Issue

Posted 23 February 2012 - 07:34 AM

Cheers pbl that helps.

One question I've got which may seem like a stupid one is how can I get the values ive got in a 2d array and then have these points drawn using the g.fillRect method. Ive done some basic shape drawing using this method, i.e g.fillRect(20, 30, 200, 100), but I'm struggling with getting array values to print out in this way.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Simple Paint Program Issue

Posted 23 February 2012 - 08:14 AM

I would use a BufferedImage here. See the setRGB() method.

http://docs.oracle.c...feredImage.html
Was This Post Helpful? 0
  • +
  • -

#9 Tronex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-December 11

Re: Simple Paint Program Issue

Posted 23 February 2012 - 03:34 PM

Is there any other ways to get my values from the 2d array and use them in the g.fillRect method?
Was This Post Helpful? 0
  • +
  • -

#10 Tronex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-December 11

Re: Simple Paint Program Issue

Posted 01 March 2012 - 10:59 AM

I'm thinking for the for loop its going to be something as follows

 for(int i = 0; i < freehandPixelsCount; i++)
        {
            g.fillRect(fxy[i][0], fxy[i][1], 10, 10);
            freehandPixelsCount++;
        }


How would i go about only drawing on the canvas when a particular radio button is selected? I know im going to need some form of listener but I've played around with this and cant seem to get it to work.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Simple Paint Program Issue

Posted 01 March 2012 - 11:22 AM

Please post your current code
Was This Post Helpful? 0
  • +
  • -

#12 Tronex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-December 11

Re: Simple Paint Program Issue

Posted 01 March 2012 - 02:42 PM

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.io.*;


public class DrawingApplication extends JFrame
{
    // GUI Component dimentsions.
    private final int CANVAS_INITIAL_WIDTH = 800;
    private final int CANVAS_INITIAL_HEIGHT = 640;
    private final int CONTROL_PANEL_WIDTH = 200;
    private final int MESSAGE_AREA_HEIGHT = 100;

    
    // Drawing area class (inner class).
    class Canvas extends JPanel
    {
        // Called every time there is a change in the canvas contents.
        public void paintComponent(Graphics g)
        {
            
            super.paintComponent(g);        
            draw(g);
        }   
    } // end inner class Canvas
    
    
    private Canvas canvas;
    
    private JPanel controlPanel;
    private JLabel coordinatesLabel;
    private JRadioButton lineRadioButton, ovalRadioButton, rectangleRadioButton, freehandRadioButton;
    private ButtonGroup radioButtonGroup;
    private JSlider freehandSizeSlider;
    private JCheckBox fineCheckBox, coarseCheckBox;
    private JButton colourButton, clearButton, animateButton;
    
    private JTextArea messageArea;
    
    private JMenuBar menuBar;
    
    private final int MAX_FREEHAND_PIXELS = 1000;
    private Color[] freehandColour = new Color[MAX_FREEHAND_PIXELS];
    private int[][] fxy = new int[MAX_FREEHAND_PIXELS][3];
    private int freehandPixelsCount = 0;
    
           
    
    /*****************************************************************
     * 
     * Constructor method starts here
     *    ... and goes on for quite a few lines of code 
     */
    public DrawingApplication()
    {
        
        class CanvasMouseMotionListener implements MouseMotionListener
        {
            public void mouseMoved(MouseEvent event)
            {
                coordinatesLabel.setText("Mouse at (" + event.getX() + ", " + event.getY() + ")");
            }
            
            public void mouseDragged(MouseEvent event)
            {
                coordinatesLabel.setText("Mouse at (" + event.getX() + ", " + event.getY() + ")");
                
                //Freehand Drawing Array
                fxy[freehandPixelsCount][0] = event.getX();
                fxy[freehandPixelsCount][1] = event.getY();
                
                
                freehandPixelsCount++;
                
                mouseMoved(event);
                canvas.repaint();
            }
        }
        
        class CheckBoxChangeListener implements ChangeListener
        {
            public void stateChanged(ChangeEvent e)
            {
                repaint();
            }
            
        }
        
        
        
       

        setTitle("Drawing Application (da1)");
        setLayout(new BorderLayout());  // Layout manager for the frame.
        
        // Canvas
        canvas = new Canvas();
          canvas.setBorder(new TitledBorder(new EtchedBorder(), "Canvas"));
          canvas.setPreferredSize(new Dimension(CANVAS_INITIAL_WIDTH, CANVAS_INITIAL_HEIGHT));
          // next line changes the cursor's rendering whenever the mouse drifts onto the canvas
          canvas.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        add(canvas, BorderLayout.CENTER);
        
        CanvasMouseMotionListener listenerObj = new CanvasMouseMotionListener();
        canvas.addMouseMotionListener(listenerObj);
        
        CanvasMouseListener listenerob = new CanvasMouseListener();
        canvas.addMouseListener(listenerob);

        
        // Menu bar
        menuBar = new JMenuBar();
          JMenu fileMenu = new JMenu("File");
            JMenuItem fileSaveMenuItem = new JMenuItem("Save");
            fileMenu.add(fileSaveMenuItem);
            JMenuItem fileLoadMenuItem = new JMenuItem("Load");
            fileMenu.add(fileLoadMenuItem);
            fileMenu.addSeparator();
            JMenuItem fileExitMenuItem = new JMenuItem("Exit");
            fileMenu.add(fileExitMenuItem);
          menuBar.add(fileMenu);
          JMenu helpMenu = new JMenu("Help");
            JMenuItem helpAboutMenuItem = new JMenuItem("About");
            helpMenu.add(helpAboutMenuItem);
          menuBar.add(helpMenu);
        add(menuBar, BorderLayout.PAGE_START);
        
        // Control Panel
        controlPanel = new JPanel();
          controlPanel.setBorder(new TitledBorder(new EtchedBorder(), "Control Panel"));
          controlPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH, CANVAS_INITIAL_HEIGHT));
          // the following two lines put the control panel in a scroll pane (nicer?).      
          JScrollPane controlPanelScrollPane = new JScrollPane(controlPanel);
          controlPanelScrollPane.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH + 30, CANVAS_INITIAL_HEIGHT));
        add(controlPanelScrollPane, BorderLayout.LINE_START);        

        
        // Control Panel contents are specified in the next section: 
        //    mouse coords panel; 
        //    shape tools panel; 
        //    trace-slider panel; 
        //    grid panel; 
        //    colour choice panel; 
        //    "clear" n "animate" buttons
        
        // Mouse Coordinates panel
        JPanel coordinatesPanel = new JPanel();
          coordinatesPanel.setBorder(new TitledBorder(new EtchedBorder(), "Drawing Position"));
          coordinatesPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 60));
          coordinatesLabel = new JLabel();
          coordinatesLabel.setText("Test Text");
          coordinatesPanel.add(coordinatesLabel);
        controlPanel.add(coordinatesPanel);
        
        // Drawing tools panel
        JPanel drawingToolsPanel = new JPanel();
          drawingToolsPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 140));
          drawingToolsPanel.setLayout(new GridLayout(0, 1));
          drawingToolsPanel.setBorder(new TitledBorder(new EtchedBorder(), "Drawing Tools"));
        controlPanel.add(drawingToolsPanel);
        
        // Freehand trace size slider
        JPanel freehandSliderPanel = new JPanel();
          freehandSliderPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 90));
          drawingToolsPanel.setLayout(new GridLayout(0, 1));
          freehandSliderPanel.setBorder(new TitledBorder(new EtchedBorder(), "Freehand Size"));
        controlPanel.add(freehandSliderPanel);

        // Grid Panel
        JPanel gridPanel = new JPanel();
          gridPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 80));
          gridPanel.setLayout(new GridLayout(0, 1));
          gridPanel.setBorder(new TitledBorder(new EtchedBorder(), "Grid"));
        controlPanel.add(gridPanel);
        
        
        // Colour Panel
        JPanel colourPanel = new JPanel();
          colourPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 90));
          colourPanel.setBorder(new TitledBorder(new EtchedBorder(), "Colour"));
          colourButton = new JButton();
          colourButton.setPreferredSize(new Dimension(50, 50));
          colourPanel.add(colourButton);
        controlPanel.add(colourPanel);
        
        //Check Boxes
        fineCheckBox = new JCheckBox("Fine");
        gridPanel.add(fineCheckBox);
        
        coarseCheckBox = new JCheckBox("Coarse");
        gridPanel.add(coarseCheckBox);
        
        
        fineCheckBox.addChangeListener(new CheckBoxChangeListener());
        coarseCheckBox.addChangeListener(new CheckBoxChangeListener());
        
        //Radio Buttons
        lineRadioButton = new JRadioButton("Line");
        rectangleRadioButton = new JRadioButton("Rectangle");
        ovalRadioButton = new JRadioButton("Oval");
        freehandRadioButton = new JRadioButton("Freehand");
        radioButtonGroup = new ButtonGroup();
        
        radioButtonGroup.add(lineRadioButton);
        radioButtonGroup.add(rectangleRadioButton);
        radioButtonGroup.add(ovalRadioButton);
        radioButtonGroup.add(freehandRadioButton);
        
        drawingToolsPanel.add(lineRadioButton);
        drawingToolsPanel.add(rectangleRadioButton);
        drawingToolsPanel.add(ovalRadioButton);
        drawingToolsPanel.add(freehandRadioButton);
        
       
        

        // Clear button
        clearButton = new JButton("Clear Canvas");
          clearButton.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 50));
        controlPanel.add(clearButton);

        // Animate button 
        animateButton = new JButton("Animate");
          animateButton.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 50));
        controlPanel.add(animateButton);
        
        // that completes the control panel section

        
        // Message area
        messageArea = new JTextArea();
        messageArea.setEditable(false);
        messageArea.setBackground(canvas.getBackground());
        JScrollPane textAreaScrollPane = new JScrollPane(messageArea);
        textAreaScrollPane.setBorder(new TitledBorder(new EtchedBorder(), "Message Area"));
        textAreaScrollPane.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH + CANVAS_INITIAL_WIDTH, MESSAGE_AREA_HEIGHT));
        add(textAreaScrollPane, BorderLayout.PAGE_END);

        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        pack();
        setVisible(true);
        
        
        
        
        
    }  // end of the DrawingApplication constructor method
    
    // Called by the canvas' paintComponent method
    void draw(Graphics g)
    {
    if(fineCheckBox.isSelected()){
            //Fine Grid Lines
            for(int i = 0; i <= canvas.getWidth(); i+= 10)
            {
                g.drawLine(i,0,i, canvas.getHeight());
                g.setColor(new Color(0.8F,0.8F,0.8F));
            }
            
            for(int i = 0; i <= canvas.getHeight(); i+= 10)
            {
                g.drawLine(0,i,canvas.getWidth(), i);
                g.setColor(new Color(0.8F,0.8F,0.8F));
            }
            
    if(coarseCheckBox.isSelected()){
            //Coarse Grid Lines
            for(int i = 0; i <= canvas.getWidth(); i+= 50)
            {
                g.drawLine(i,0,i, canvas.getHeight());
                g.setColor(new Color(0.6F,0.6F,0.6F));
            }
            
            for(int i = 0; i <= canvas.getHeight(); i+= 50)
            {
                g.drawLine(0,i,canvas.getWidth(), i);
                g.setColor(new Color(0.6F,0.6F,0.6F));
            }
            
            
           
        }
        

        //g.fillRect(20, 30, 200, 100);
    } // end draw method   
}
    
    public static void main(String args[])
    {
        DrawingApplication drawingApplicationInstance = new DrawingApplication();
    } // end main method
    
} // end of DrawingApplication class


Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Simple Paint Program Issue

Posted 01 March 2012 - 03:13 PM

The gui looks good i think but i must confess the presence of the array mystifies me. Why is it there? How are you going to use it?
Was This Post Helpful? 0
  • +
  • -

#14 Tronex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-December 11

Re: Simple Paint Program Issue

Posted 01 March 2012 - 04:14 PM

Were just using arrays as in class were only using what we have been tought, so even though its not the most efficent way of doing this I've gotta stick with it for now.
Was This Post Helpful? 0
  • +
  • -

#15 Tronex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 08-December 11

Re: Simple Paint Program Issue

Posted 02 March 2012 - 04:58 PM

Any thoughts on this?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2