Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
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 12-Jun-13 19:12pm

1 solution

Rate this: bad
good
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[^].
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 16 Jun 2013
Copyright © CodeProject, 1999-2014
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