Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys so Im trying to recreate a version of mspaintm, and I have a couple of questions. First when i rapidly paint in the canvas it becomes apparent that not every location is painted, would the problem be solved using mutli threading? Also how would i save the portion of the screen and save it as a file, thanks for looking

import javax.swing.*;


public class CanvasInitializer {
    public static void main(String [] args){
        CanvasPanel canvasPanel = new CanvasPanel();
        canvasPanel.setSize(500,500);
        canvasPanel.setTitle("Canvas");
        canvasPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvasPanel.setVisible(true);
    }
}


import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;



public class CanvasPanel extends JFrame {
    private Brush brush;
    private LinkedList<Brush> brushList;
    private JButton clear, erase;
    private Map<String,Color> map ;
    private Canvas canvas;
    private JComboBox<String> jComboBox , brushCombo;
    private String [] brushTips = {"Small", "Medium","Large"};
    private Color [] colors = {Color.BLACK,Color.BLUE, Color.CYAN, Color.GRAY,Color.DARK_GRAY, Color.LIGHT_GRAY, Color.GREEN,
                                            Color.MAGENTA,Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW};
    private String [] colorArray = {"Black","Blue","Cyan","Gray","DarkGray","LightGray",
                                   "Green","Magneta","Orange","Pink","Red","White","Yellow"};
    private Color colorSelected = Color.black;
    private int xGrid,yGrid, width, height;

    public CanvasPanel(){

        clear = new JButton("Clear");
        erase = new JButton("Erase");
        map = new HashMap<String, Color>();
        brushList = new LinkedList<Brush>();
        jComboBox = new JComboBox<String>(colorArray);
        brushCombo = new JComboBox<String>(brushTips);
        canvas = new Canvas();
        canvas.requestFocus();

        for(int i = 0; i< colorArray.length; ++i){
                map.put(colorArray[i], colors[i]);
        }

        JPanel topPanel = new JPanel();
        topPanel.add(jComboBox);
        topPanel.add(brushCombo);
        topPanel.add(clear);
        topPanel.add(erase);

        clear.addActionListener(new ButtonListener());
        erase.addActionListener(new ButtonListener());
        jComboBox.addActionListener(new ComboBox());
        brushCombo.addActionListener(new BrushTip());
        canvas.addMouseMotionListener(new PointListener());


        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel, canvas);
        setLayout(new BorderLayout());
        add(sp);
    }




    public class Canvas extends JPanel implements Runnable{
           public Canvas(){
          setBackground(Color.WHITE);
        }
        protected void paintComponent(Graphics page){
            super.paintComponent(page);
            if(!brushList.isEmpty())
                for(int i =0; i< brushList.size(); ++i){
                    brushList.get(i).drawImage(page);
                }


        }


public class Brush {

     private int x, y, width, height;
     private Color color;

    public Brush(int x, int y,int width, int height, Color color){
     this.x = x;
     this.y = y;
     this.width = width;
     this.height = height;
     this.color = color;

    }

    protected void drawImage(Graphics graphics){
        graphics.setColor(color);
        graphics.fillOval(x,y,width,height);

    }

}
Posted

1 solution

Multithreading may or may not help you here. If you use multithreading, one cardinal sin you must not commit is to have 2 or more threads performing UI functions. You might (emphasis: "might") see some improvements if you are able to spin off non-UI processing to another thread. The reason that this may or may not work is that interfacing between 2 or more threads will cause extra processing to occur which might make performance worse.

One of the multithreading techniques/patterns that might be helpful for you is use of the "Actor model". This is described here[^] and there is a CP example (C++) here[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900