Results 1 to 6 of 6
Thread: repaint() problem
- 11-15-2011, 12:56 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
repaint() problem
Hello,
My program displays a gui that can either draw (this code is working) or choose a shape, color, fill or no fill.
I've got event handlers in place. I'm testing the code incrementally, and when the jcombobox is selected,
and a shape chosen, the repaint() isn't working. Help, this is due tonight. Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//import java.awt.event.ItemListener;
//import java.awt.event.ItemEvent;
//import java.awt.event.MouseEvent;
//import java.awt.event.MouseAdapter;
//import java.awt.event.MouseMotionAdapter;
public class PaintPanel extends JPanel
{
//Create the GUI Compoenents
private Container container;
private static JPanel choicePanel;
private JComboBox shapeComboBox;
private JCheckBox fillJCheckBox;
private int solid; // stores the fill type
private JButton changeColorJButton;
private Color color = Color.LIGHT_GRAY; //stores the color chosen
//set up variable for free-form drawing portion:
private int pointCount = 0;// count number of points
// array of 10000 java.awt.Point references
private Point[] points = new Point[ 10000 ];
private static final String[] shapes = {"rectangle", "square", "oval",
"circle", "line", "rounded rectangle"};
private static int choice;
// set up GUI and register event handlers:
public PaintPanel()
{
setBackground(Color.WHITE);
MouseHandler mouseHandler = new MouseHandler();
addMouseListener( mouseHandler );
addMouseMotionListener( mouseHandler );
//build choicePanel:
shapeComboBox = new JComboBox( shapes );
ShapeHandler shapeHandler = new ShapeHandler();
shapeComboBox.addItemListener (shapeHandler);
//Set up changeColorJButton and register its event handler
changeColorJButton = new JButton ( "Change Color" );
ColorHandler colorHandler = new ColorHandler();
changeColorJButton.addActionListener ( colorHandler );
//Set up fillJCheckBox and register its event handler
fillJCheckBox = new JCheckBox ( "filled");
CheckBoxHandler checkBoxHandler = new CheckBoxHandler();
fillJCheckBox.addItemListener (checkBoxHandler);
choicePanel = new JPanel();
choicePanel.setLayout( new FlowLayout( FlowLayout.LEFT));
choicePanel.add(shapeComboBox);
choicePanel.setLayout ( new FlowLayout( FlowLayout.CENTER));
choicePanel.add(changeColorJButton);
choicePanel.add(fillJCheckBox);
add(choicePanel);
}//end PaintPanel constructor
private class MouseHandler extends MouseAdapter
{
public void mouseClicked( MouseEvent event )
{
int xPos = event.getX(); //get x-position of mouse
int yPos = event.getY(); //get y-position of mouse
}//end mouse Clicked method
//store drag coordinates and repaint
public void mouseDragged( MouseEvent event)
{
if ( pointCount < points.length )
{
points[ pointCount] = event.getPoint();
pointCount++;
repaint();
}//end if
}//end method mouseDragged
}//end class MouseHandler
private class ShapeHandler implements ItemListener {
//respond to JComboBox events
public void itemStateChanged( ItemEvent event )
{
JOptionPane.showMessageDialog(null, "In ShapeHandler");
//determine whether item selected
// if ( event.getStateChange() == ItemEvent.SELECTED )
// {
choice = shapeComboBox.getSelectedIndex();
JOptionPane.showMessageDialog(null, "The shape is: " + choice);
repaint();
// }//end if
}//end method itemStateChanged
}//end private inner class ShapeHandler
private class ColorHandler implements ActionListener
{
//respond to color button events
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog(null, "In ColorHandler");
color = JColorChooser.showDialog(
PaintPanel.this, "Choose a color", color);
JOptionPane.showMessageDialog(null, "The color is: " + color);
// set default color, if no color is returned
if ( color == null)
color = Color.RED;
}//end method actionPerformed
}//end private inner class ColorHandler
private class CheckBoxHandler implements ItemListener
{
//respond to Checkbox events
public void itemStateChanged( ItemEvent event )
{
JOptionPane.showMessageDialog(null, "In CheckBoxHandler");
//determine whether item selected
if ( event.getStateChange() == ItemEvent.SELECTED )
solid = 1; //fill shape
else
solid = 2; //draw shape
JOptionPane.showMessageDialog(null, "solid = " + solid);
}//end method itemStateChanged
}//end private inner class CheckBoxHandler
public void paintComponent ( Graphics g )
{
super.paintComponent( g ); //call superclass's paintComponent
g.setColor(color);
g.drawLine(5,30,380,30);
System.out.printf( "in paintcomponent choice = %d",choice);
g.setColor(color);
if ((choice == 2) && (solid ==2))
{
g.drawRect(20,20,0,0);
}
// draw all points in array if mouse was dragged to draw
for ( int i = 0; i< pointCount; i++)
g.fillOval( points[ i ].x, points[i].y, 4, 4);
}//end method paintComponent
public static void main( String args[])
{
JFrame application = new JFrame( "Draw" );
// PaintPanel paintPanel = new PaintPanel();
PaintPanel paintPanel = new PaintPanel();
application.add( paintPanel, BorderLayout.CENTER);
application.add( choicePanel,BorderLayout.SOUTH);
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.setSize( 600, 400); //set width and height;
application.setVisible( true ); //display GUI, Automatically calls paintComponent
}// end main method
}//end class PaintPanel
-
Re: repaint() problem
- What do you mean by "isn't working"? Please be as specific as possible, so our help can be specific.
- Please edit your post above to use [code] and [/code] tags around your code so that the formatting is retained and your code is readable. Please note that the tags only work if the code that has been pasted in the forum is properly formatted.
- Have you added println statements throughout your code to help debug its state at various times of running?
- You'll want to leave off all statements of urgency or due date or time as they often have the opposite effect you've intended. If you want to know why, ask, but believe me, I've seen it, and it's true.
Luck
-
Re: repaint() problem
Also -- get those JOptionPanes out of your listeners as they can mess your program up.
Also -- check the API for Graphics#drawRect(...). I'm not surprised that this would show nothing:
Java Code:g.drawRect(20, 20, 20, 20);
- 11-15-2011, 02:03 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: repaint() problem
Thanks for the help. I did put in the println(s) to help debug the logic. Also,
thanks for pointing out the bad drawrect code; was aiming for a square and
realize I should have used drawRoundRect. I'll use the code tags next time too.
-
Re: repaint() problem
You can add code tags to your original post simply by editing it. :)
- 11-15-2011, 01:37 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
problem with repaint bar
By vitaly87 in forum New To JavaReplies: 2Last Post: 07-12-2011, 02:37 PM -
Problem with repaint();
By dunafrothint in forum AWT / SwingReplies: 8Last Post: 01-07-2010, 12:33 AM -
Repaint problem
By citizenXL in forum New To JavaReplies: 4Last Post: 10-28-2009, 03:02 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 1Last Post: 02-16-2008, 09:12 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 0Last Post: 01-06-2008, 03:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks