You are here: DEVPPL Forum Programming Java Forum
NOTIFICATIONS
54.060
MEMBERS
15.677
TOPICS
62.241
POSTS
  562
FLASH GAMES
7.740
TUTORIALS
 

Login

E-mail:
Password:

paint apps

0

Loading

paint apps

Postby Teddybear » Fri Aug 31, 2007 5:28 am

Hello there, this is my first post. I am still sort of new to Java, I have written a few apps, but I am having a small issue with my paint app. It is very basic and allows a line to be drawn, then altered by its size and color. The issue that I am having is when I change color or size, all of the previous lines change with it. I think that it has to do with storing and retreiving the current size and color in an arraylist, but I may be wrong. I am grateful for any suggestions.

cheers

Code: Select all

import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Painter extends JFrame
{
   private int pointCount = 0;
   private int size1 = 4;
   private int size2 = 4;
   
   private JRadioButton thinButton;
   private JRadioButton mediumButton;
   private JRadioButton thickButton;
   private JButton changeColorButton;
   private Color color = Color.BLACK;
   private Container container;

   // array of 1000 java.awt.Point references
   private Point points[] = new Point[ 1000 ]; 

   // set up GUI and register mouse event handler
   public Painter()
   {
      super( "A simple paint program" );
     
      class MouseDraggedPoint extends MouseMotionAdapter
      {

          // store drag coordinates and repaint
         public void mouseDragged( MouseEvent event )
            {
               if ( pointCount < points.length )
               {
                  points[ pointCount ] = event.getPoint();
                  ++pointCount;
                  repaint();
               }
            }   
      }
     
      MouseDraggedPoint draggedPoint  = new MouseDraggedPoint();
      addMouseMotionListener(draggedPoint);
      setSize( 800, 300 ); 
      setVisible( true ); 
     
      JPanel buttonPanel = new JPanel();
      ButtonGroup group = new ButtonGroup();
      thinButton = addRadioButton(buttonPanel, group, "Thin", true);
      mediumButton = addRadioButton(buttonPanel, group, "Medium", false);
      thickButton = addRadioButton(buttonPanel, group, "Thick", false);

      thinButton.addItemListener(new SelectItemListener());
      mediumButton.addItemListener(new SelectItemListener());
      thickButton.addItemListener(new SelectItemListener());

      // set up changeColorButton and register its event handler
      changeColorButton = new JButton( "Change Color" );
     
      ActionListener listener = new ColorListener();
      changeColorButton.addActionListener(listener);
     
      getContentPane().setLayout( new FlowLayout() );
      getContentPane().add( new JLabel( "Drag the mouse to draw"));
      getContentPane().add(buttonPanel);
      getContentPane().add(changeColorButton);

  }

  public JRadioButton addRadioButton(JPanel buttonPanel, ButtonGroup g,
      String buttonName, boolean v) {
    JRadioButton button = new JRadioButton(buttonName, v);

    g.add(button);
    buttonPanel.add(button);
    return button;

  }
  public JButton addJButton(JPanel buttonPanel, String buttonName)
  {
      JButton button = new JButton(buttonName);
      buttonPanel.add(button);
      return(button);
    } 
  class SelectItemListener implements ItemListener
  {
        public void itemStateChanged(ItemEvent e)
        {
                Object source = e.getSource();
                if (source == thinButton)
                size1 = 2;
                else if (source == mediumButton)
                size1 = 12;
                else if (source == thickButton)
                size1 = 24;

        }
    }
 
       /**
      * display JColorChooser when user clicks button
      */
     class ColorListener implements ActionListener
        {             
            public void actionPerformed( ActionEvent event )
            {
               color = JColorChooser.showDialog(
                        Painter.this, "Choose a color", color );
            }

        } // end ColorListener class
    // end Painter constructor

   /** draw oval in a 4-by-4 bounding box at specified location on window */
   
   public void paint( Graphics g )
   {
      super.paint( g ); // clears drawing area
      g.setColor(color);

      for ( int i = 0; i < points.length && points[ i ] != null; i++ )
         g.fillOval( points[ i ].x, points[ i ].y, size1, size2 );

        }

   public static void main( String args[] )
   {
      Painter application = new Painter();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

} // end class Painter
:?
Teddybear
 
Reputation: 0
Posts: 2
Joined: Fri Aug 31, 2007 5:12 am
Highscores: 0
Arcade winning challenges: 0

paint apps - Sponsored results

Sponsored results

Login to get rid of ads

 

0

Loading

Postby Teddybear » Sun Sep 02, 2007 11:15 am

meh, i have worked the last few days on my problem, but still cant fix it. can anybody out there help?
Teddybear
 
Reputation: 0
Posts: 2
Joined: Fri Aug 31, 2007 5:12 am
Highscores: 0
Arcade winning challenges: 0
^ Back to Top