Results 1 to 7 of 7
- 11-20-2010, 01:10 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Java Paint Program problem (JPanel)
Okay, so, I am making a java Paint like program and I want to draw in a panel. The problem is, when I draw, the lines go outside of the panel, and i don't want this. Any help would be appreciated
here is the GUI
and here is the actual appJava Code:import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class GUI extends JFrame implements ChangeListener { private JButton pen; private JButton brush; private JButton image; private JButton animator; private JButton change_Color; private JMenuBar menubar; private JMenu file; private JMenu edit; private JColorChooser palette; private JPanel drawing_Area; private Color current_Color = Color.BLACK; private int X_Value; private int Y_Value; private Boolean is_Pen = true; private Boolean is_Brush = false; public GUI(){ setTitle ("Artist"); setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH); getContentPane().setLayout(null ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100,100,300,300); setVisible(true); drawing_Area = new JPanel(); drawing_Area.setSize (800,600); drawing_Area.setVisible(true); drawing_Area.setBackground(Color.WHITE); drawing_Area.addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged( MouseEvent event ){ X_Value = event.getX(); Y_Value = event.getY(); repaint(X_Value,Y_Value,10,10); } // end mouseDragged }); // end addMouseMotionListener drawing_Area.setBounds(0,100,900,600); getContentPane().add(drawing_Area); // adds a menu bar, and menus (doesn't work, should later) menubar = new JMenuBar(); setJMenuBar(menubar); JMenu fileMenu = new JMenu("File"); JMenu editMenu = new JMenu("Edit"); menubar.add(fileMenu); menubar.add(editMenu); JMenuItem newAction = new JMenuItem("New"); JMenuItem openAction = new JMenuItem("Open"); JMenuItem exitAction = new JMenuItem("Exit"); exitAction.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); JMenuItem cutAction = new JMenuItem("Cut"); JMenuItem copyAction = new JMenuItem("Copy"); JMenuItem pasteAction = new JMenuItem("Paste"); fileMenu.add(newAction); fileMenu.add(openAction); fileMenu.add(exitAction); editMenu.add(cutAction); editMenu.add(copyAction); editMenu.add(pasteAction); // sets the pen button pen = new JButton("Pen"); pen.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { is_Pen = true; is_Brush= false; } // end of (ActionPerformed(pen)) }); // end of (addActionListener) pen.setBounds(20,20, 20, 20); getContentPane().add(pen); // sets the brush button brush = new JButton("Brush"); brush.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { is_Pen = false; is_Brush= true; } // end of (ActionPerformed(pen)) }); // end of (addActionListener) getContentPane().add(brush); // image chooser image= new JButton("Image"); getContentPane().add(image); // animator animator= new JButton("Animator"); getContentPane().add(animator); // sets the color chooser change_Color = new JButton("Change Color"); change_Color.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { current_Color = JColorChooser.showDialog( GUI.this, "Choose Color", current_Color ); } // end of (ActionPerformed(pen)) }); // end of (addActionListener) getContentPane().add(change_Color); show(); } public void paint(Graphics g){ if((X_Value >0) && (Y_Value < 600)&&(X_Value <900) && (Y_Value > 100)){ g.setColor(current_Color); if(is_Pen){ g.fillOval( X_Value, Y_Value, 1, 1 ); } if( is_Brush){ g.fillOval( X_Value, Y_Value, 10, 10 ); } } } // end of function (paint) @Override public void stateChanged(ChangeEvent arg0) { Color newColor = palette.getColor(); setForeground(newColor); } }
Java Code:import java.awt.Frame; public class Artist { public static void main(String[] args){ Frame f; f = new GUI(); } // end main }
- 11-20-2010, 02:09 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
JPanel? paint method? so who wrote your code?
- 11-20-2010, 03:30 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
I wrote my code.
- 11-20-2010, 03:48 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Also, the drawing is a bit laggy, is there anyway to optimize it without a timer function?
and lastly, when the JColorChooser pops up, it removes all color behind it, is there a way to prevent that?
-
What do you mean "go outside of the panel"? Can you elaborate exactly how your program is misbehaving?
I see that you're drawing directly in the JFrame's paint method. This suggests that you're trying to do Swing graphics without first reading a tutorial on this, which will lead to grief, side effects, and lag. I urge you to go through the graphics tutorials first before writing this where you'll learn how to draw in a JPanel's paintCompoment method amongst other important lessons. Luck!
- 11-20-2010, 04:29 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
What i mean is that i want a hard-programmed border so that if i drag the mouse off of the panel, it stops drawing, so that it wont spill over into the frame.
-
Similar Threads
-
Paint Program Help
By ngiannini in forum AWT / SwingReplies: 12Last Post: 05-10-2010, 04:24 PM -
Simple Paint program question
By StressaJune in forum New To JavaReplies: 1Last Post: 03-30-2009, 08:46 PM -
paint vs paintComponent for a JPanel
By lightstream in forum AWT / SwingReplies: 4Last Post: 01-29-2009, 02:26 AM -
just started paint program. am i going the right direction?
By diggitydoggz in forum New To JavaReplies: 3Last Post: 12-31-2008, 05:57 AM -
help?! paint on top of swing components on JPanel
By beam2008 in forum AWT / SwingReplies: 1Last Post: 12-05-2008, 04:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks