I am trying to draw at my mouse a certain shape that I have set. I defined some shapes where they extend shape and draw circles and stuff. But when I click on panel it seems the paint doesnt put anything there. Debugger tells me shapes are saved though.
public void mouseClicked(MouseEvent e) { currentX = e.getX(); currentY = e.getY(); Shape newShape = owner.currentBrush.clone(); picture.add(newShape); repaint(); } public void paint(Graphics g){ super.paint(g); for( int i = 0; i < myShapes.size(); i++ ){ picture.get(i).draw(g); } } public void draw(Graphics g){ Graphics g2d = (Graphics2D) g; g2d.setColor(Color.BLUE); g2d.fillOval(getX(), getY(), radius, radius); g.drawOval(getX(), getY(), radius, radius); }
drawpanel:
import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class DrawPanel extends JPanel implements MouseListener{ Application owner; Stack myShapes = new Stack(); Stack undoStack = new Stack(); ArrayList<Shape> picture; int currentX = 10; int currentY = 10; public DrawPanel(Application parent){ //this.setOpaque(true); this.owner = parent; this.setPreferredSize(new Dimension(600,800)); this.setBackground(Color.WHITE); this.setVisible(true); picture = new ArrayList<Shape>(); addMouseListener(this); repaint(); //addMouseMotionListener(this); } public void paint(Graphics g){ super.paint(g); for( int i = 0; i < owner.myDraw.picture.size(); i++ ){ owner.myDraw.picture.get(i).draw(g); System.out.println("I BET THIS NEVER PRINTS"); } System.out.println("I AM TRYING TO PAINT"); } public void mousePressed(MouseEvent e) { currentX = e.getX(); currentY = e.getY(); //System.out.println(e.getY()); Shape newShape = owner.currentBrush; picture.add(newShape); repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseReleased(MouseEvent e){ } public void mouseClicked(MouseEvent e) { owner.myDraw.currentX = e.getX(); owner.myDraw.currentY = e.getY(); // System.out.println(e.getY()); Shape newShape = owner.getBrush(); owner.myDraw.picture.add(newShape); System.out.println(picture.size()); repaint(); } public void redo(){ try{ owner.myDraw.picture.add((Shape)undoStack.pop()); } catch(LinkedListException p){ System.out.println("Empty stack no poppy please"); } } public void undo(){ try{ owner.myDraw.undoStack.push(picture.remove(picture.size()-1)); } catch(LinkedListException e){ System.out.println("Nothing to undo"); } } }
main app:
import java.util.*; import javax.swing.*; import java.awt.*; import java.io.Serializable; public class Application extends JFrame{ protected ButtonPanel myButton; protected DrawPanel myDraw; Shape currentBrush; JFrame frame; int currentX; int currentY; public Application() { frame = new JFrame("Main App"); currentBrush = new Circle(5,5,5); this.myDraw = new DrawPanel(this); this.myButton = new ButtonPanel(this); frame.setSize(800, 800); frame.add(new ButtonPanel(this)); frame.add(new DrawPanel(this)); frame.setVisible(true); frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS)); } public Application(int x, int y, int r) { System.out.println("Setting to custom brush"); currentBrush = new Circle(x,y,r); } public void add(){ try{ myDraw.myShapes.insert(currentBrush); } catch(LinkedListException e){ System.out.println("Good day sir"); } } public Shape getBrush(){ return new Shape(currentBrush); } public void setCurrentBrush(Shape newBrush){ this.currentBrush = new Shape(newBrush); } public void setX(int nx){ this.currentX = nx; } public void setY(int ny){ this.currentY = ny; } }