I'm starting a new paint project to get some experience with java. I've been working with Processing for a while, and I'm struggling to transition. The goal is to make a program in which several graphics objects are created, then the selected graphic is modified (drawn on) when the mouse is dragged. The output I'm looking for is the graphics layered on top of each other. I'm pretty lost, and I would definitely appreciate some help from someone more experienced that myself. Here's what I have:
import java.awt.*; import javax.swing.*; class SlugPaint2{ public static void main(String[] args){ JFrame frame = new JFrame("SimplePaint"); Container mainPane = frame.getContentPane(); DrawingCanvas layers = new DrawingCanvas(); PaintListener mouse = new PaintListener(); layers.addMouseMotionListener(mouse); frame.setSize(600,400); mainPane.add(layers); frame.setVisible(true); } }import java.awt.*; import java.awt.event.*; import java.awt.Color.*; import javax.swing.*; public class PaintListener implements MouseMotionListener, MouseListener{ public void mouseDragged(MouseEvent e){ DrawingCanvas layerOneCanvas = (DrawingCanvas)e.getSource(); Graphics layerOne = layerOneCanvas.getCanvasGraphics(); Graphics2D layerOne2D = (Graphics2D)layerOne; updateMousePos(e); updateMenu(e); layerOne2D.setBackground(Color.WHITE); layerOne2D.setStroke(new BasicStroke(2)); layerOne2D.drawLine(currentMousePosX, currentMousePosY, lastMousePosX, lastMousePosY); layerOneCanvas.repaint(); } public void mouseMoved(MouseEvent e){ updateMousePos(e); } public void updateMousePos(MouseEvent e){ lastMousePosX=currentMousePosX; lastMousePosY=currentMousePosY; currentMousePosX=e.getX(); currentMousePosY=e.getY(); } public void updateMenu(MouseEvent e){ DrawingCanvas guiLayer = (DrawingCanvas)e.getSource(); Graphics paintingMenu = guiLayer.getMenuGraphics(); Graphics2D paintingMenu2D = (Graphics2D)paintingMenu; paintingMenu2D.drawString("this is the top layer. It will display the images that will work as a custom gui.", 10,200); paintingMenu2D.drawString("the lower layer that shows the painting is underneath this. go to 'void paintComponent' in", 10,220); paintingMenu2D.drawString("DrawingCanvas.java and swap the order of which layer is drawn first.",10,240); guiLayer.repaint(); } public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} int lastMousePosX, lastMousePosY; int currentMousePosX, currentMousePosY; public static boolean firstLayerVisible = true; }import java.awt.*; import javax.swing.*; class DrawingCanvas extends JComponent{ protected void paintComponent(Graphics g){ if(layerOneImage!=null) g.drawImage(layerOneImage,0,0,SIZE, SIZE, null); if(paintingMenuImage!=null) //comment out this line. thise draw the top layer g.drawImage(paintingMenuImage,0,0,SIZE, SIZE, null); //comment out this line as well. you will see the drawing canvas } //without the top layer being shown to block the bottom public Graphics getCanvasGraphics(){ if(layerOneImage==null){ layerOneImage = createImage(SIZE, SIZE); layerOneGraphics = layerOneImage.getGraphics(); } return layerOneGraphics; } public Graphics getMenuGraphics(){ if(paintingMenuImage==null){ paintingMenuImage = createImage(SIZE, SIZE); paintingMenuGraphics = paintingMenuImage.getGraphics(); } return paintingMenuGraphics; } public Dimension getMinimumSize(){ return new Dimension(SIZE,SIZE); } public Dimension getPreferredSize(){ return new Dimension(SIZE,SIZE); } private static final int SIZE = 600; private Image layerOneImage; private Graphics layerOneGraphics; private Image paintingMenuImage; private Graphics paintingMenuGraphics; }