Simple Paint in Java
Simple Paint in Java - YouTube
________________________________________________ How to make Simple Paint in Java?
package SimpleProjects; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimplePaint{ public static void main(String[] args){ JFrame frame = new JFrame("Simple Paint"); Container content = frame.getContentPane(); content.setLayout(new BorderLayout()); final PadDraw drawPad = new PadDraw(); content.add(drawPad, BorderLayout.CENTER); JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(100, 68)); panel.setMinimumSize(new Dimension(100, 68)); panel.setMaximumSize(new Dimension(100, 68)); JButton one = new JButton("1"); one.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Clear(); } }); JButton YellowBtn = new JButton(""); YellowBtn.setBackground(Color.YELLOW); YellowBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Yellow(); } }); JButton GreenBtn = new JButton(""); GreenBtn.setBackground(Color.GREEN); GreenBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Green(); } }); JButton PinkBtn = new JButton(""); PinkBtn.setBackground(Color.PINK); PinkBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Pink(); } }); JButton CyanBtn = new JButton(""); CyanBtn.setBackground(Color.CYAN); CyanBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Cyan(); } }); JButton BlackBtn = new JButton(""); BlackBtn.setBackground(Color.BLACK); BlackBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Black(); } }); YellowBtn.setPreferredSize(new Dimension(80, 20)); GreenBtn.setPreferredSize(new Dimension(80, 20)); PinkBtn.setPreferredSize(new Dimension(80, 20)); CyanBtn.setPreferredSize(new Dimension(80, 20)); BlackBtn.setPreferredSize(new Dimension(80, 20)); panel.add(YellowBtn); panel.add(GreenBtn); panel.add(PinkBtn); panel.add(CyanBtn); panel.add(BlackBtn); content.add(panel, BorderLayout.NORTH); JRadioButton redButtonPX1 = new JRadioButton("2x"); JRadioButton redButtonPX2 = new JRadioButton("6x"); JRadioButton redButtonPX3 = new JRadioButton("14x"); panel.add(redButtonPX1); panel.add(redButtonPX2); panel.add(redButtonPX3); ButtonGroup bg = new ButtonGroup(); bg.add(redButtonPX1); bg.add(redButtonPX2); bg.add(redButtonPX3); redButtonPX1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Small(); } }); redButtonPX2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Medium(); } }); redButtonPX3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Big(); } }); JButton clearBtn = new JButton("Clear"); clearBtn.setBackground(Color.WHITE); clearBtn.setFont(UIManager.getFont("TextArea.font")); clearBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.Clear(); } }); panel.add(clearBtn); frame.setSize(454, 440); frame.setLocation(350, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class PadDraw extends JComponent{ private Image image; private Graphics2D graphics2D; private int X, Y, X1, Y1; public PadDraw() { setDoubleBuffered(false); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { X1 = e.getX(); Y1 = e.getY(); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { X = e.getX(); Y = e.getY(); if(graphics2D != null) { graphics2D.drawLine(X1, Y1, X, Y); repaint(); X1 = X; Y1 = Y; } } }); } public void paintComponent(Graphics g) { if(image == null) { image = createImage(getSize().width, getSize().height); graphics2D = (Graphics2D)image.getGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Clear(); } g.drawImage(image, 5, 5, null); } public void Clear() { graphics2D.setPaint(Color.white); graphics2D.fillRect(0, 0, getSize().width, getSize().height); graphics2D.setPaint(Color.black); repaint(); } public void Pink() { graphics2D.setPaint(Color.pink); repaint(); } public void Black() { graphics2D.setPaint(Color.black); repaint(); } public void Yellow() { graphics2D.setPaint(Color.yellow); repaint(); } public void Cyan() { graphics2D.setPaint(Color.cyan); repaint(); } public void Green() { graphics2D.setPaint(Color.green); repaint(); } public void Small() { graphics2D.setStroke(new BasicStroke(2)); } public void Medium() { graphics2D.setStroke(new BasicStroke(6)); } public void Big() { graphics2D.setStroke(new BasicStroke(14)); } }
________________________________________________
Dark Hers
YouTube
https://instagram.com/darkhers
https://www.facebook.com/darkhers
TikTok
https://www.tiktok.com/@darkhers
https://www.pinterest.co.uk/darkhers
Comments
Post a Comment