Reputation: 825
I'm stuck working on a project where I had to implement a series of several classes which eventually form a functional drawing program. My problem is that the drawing doesn't work properly when I run the test program. To give you a basic idea, I implemented subclasses for shapes MyLine, MyRectangle, MyOval
all of which are under superclass MyShape
. Each of these subclasses implements their own draw
method which takes a Graphics parameter as an argument. I then implemented two classes to design the interface on which these shapes can be drawn using the mouse. These two classes, DrawPanel
and DrawFrame
, extend JPanel
and JFrame
, respectively.
I have a feeling that the error is in the overridden paintComponent
method of DrawPanel
, or in the way that the repaint()
methods are called. What happens when I run the program is that the entire window shows up properly with all the menus etc. but either one of two things happens when I try to draw a shape:
Also, I noticed that if I add the super.paintComponent(g2)
command, I get the proper white background and I can see shapes being dragged out really quickly but they never actually stay on screen.
Here is my code for DrawPanel
, I'm pretty sure that's where the error occurs:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicTabbedPaneUI.MouseHandler;
public class DrawPanel extends JPanel {
private MyShape[] shapes;
private int shapeCount;
private int shapeType;
private MyShape currentShape;
private Color currentColor;
private boolean filledShape;
private JLabel statusLabel;
private Graphics gTest;
DrawPanel(JLabel P){
shapes= new MyShape[100];
shapeCount=0;
statusLabel=P;
currentColor= Color.black;
currentShape=null;
setBackground(Color.white);
shapeType=1;
Mouse handler= new Mouse();
addMouseListener(handler);
addMouseMotionListener(handler);
}
@Override
protected
void paintComponent(Graphics g2){
super.paintComponent(g2);
while(shapeCount>1 && currentShape!=null)
{
currentShape.draw(g2);
shapeCount--;
}
}
//set methods
void setColor(Color c){
currentColor=c;
}
void setFill(boolean f){
filledShape= f;
}
void setShape(int s){
shapeType=s;
}
void clearLastShape(){
if(shapeCount==0)
System.out.print("There are no more shapes to clear");
else
shapeCount--;
repaint();
}
void clearDrawing(){
shapeCount=0;
repaint();
}
class Mouse extends MouseAdapter implements MouseMotionListener{
@Override
public
void mousePressed(MouseEvent e){
if(shapeType==1)
currentShape= new MyLine();
else if(shapeType==2)
currentShape= new MyRectangle();
else
currentShape= new MyOval();
currentShape.setX1(e.getX());
currentShape.setY1(e.getY());
currentShape.setColor(currentColor);
if(shapeType==2 || shapeType==3){
((MyBoundedShape) currentShape).setFill(filledShape);
}
}
@Override public void mouseReleased(MouseEvent e){
currentShape.setX2(e.getX());
currentShape.setY2(e.getY());
shapes[shapeCount]=currentShape;
shapeCount++;
currentShape=null;
repaint();
}
@Override
public void mouseMoved(MouseEvent e){
statusLabel.setText("("+e.getX()+","+e.getY()+")");
}
@Override
public void mouseDragged(MouseEvent e){
mouseMoved(e);
currentShape.setX2(e.getX());
currentShape.setY2(e.getY());
repaint();
}
}
}
Any help is appreciated.
Upvotes: 1
Views: 1423
Reputation: 285405
You never iterate through the array to draw the Shapes held by the array. You change the shapeCount variable, but that by itself does nothing if you don't use it to grab one of the Shapes in the array. I suggest that you try doing this, if that is your goal.
In fact, you probably shouldn't change the shapeCount variable inside of paintComponent as this will clear your drawing.
Upvotes: 2