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);
}
}