Click here to Skip to main content
13,048,011 members (110,530 online)
Rate this:
Please Sign up or sign in to vote.
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();

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",
    private Color colorSelected =;
    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();
        for(int i = 0; i< colorArray.length; ++i){
                map.put(colorArray[i], colors[i]);
        JPanel topPanel = new JPanel();
        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());


    public class Canvas extends JPanel implements Runnable{
           public Canvas(){
        protected void paintComponent(Graphics page){
                for(int i =0; i< brushList.size(); ++i){


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){
Posted 12-Jun-13 18:12pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^].

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 16 Jun 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100