Create a drawing dot gui application in java

Problem:

Create a drawing dot java gui application.



Prompt Picture Description




Solution:

//***************DrawingDotFrame.java************************/
import javax.swing.JFrame;

public class DrawingDotFrame {

 
  public static void main(String[] args) {
   
   JFrame frame = new JFrame("Drawing Dot");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.getContentPane().add(new DrawingDotPanel());
   frame.pack();
   frame.setVisible(true);
   frame.setResizable(false);
   frame.setLocationRelativeTo(null);
  }
}
//***************DrawingDotPanel.java************************/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JPanel;

public class DrawingDotPanel extends JPanel {

 private ArrayList<Point> points;
 private final int WIDTH = 400, HEIGHT = 400;
 private final int Radius = 3;
 
 public DrawingDotPanel() {

  
  points = new ArrayList<>();
  
  addMouseListener(new DotsListener());
  
  
  setPreferredSize(new Dimension(WIDTH,HEIGHT));
  setBackground(Color.BLACK);
 }
 @Override
 protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.setColor(Color.GREEN);
  
  for(Point p : points)
  g.fillOval(p.x-Radius, p.y-Radius, 2*Radius, 2*Radius); 
  g.drawString("Count: " + points.size(), 5,15);
  
 }
 private class DotsListener implements MouseListener {

  @Override
  public void mouseClicked(MouseEvent arg0) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseEntered(MouseEvent arg0) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseExited(MouseEvent arg0) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void mousePressed(MouseEvent arg0) {
   // TODO Auto-generated method stub
   
   points.add(arg0.getPoint());
   repaint();
  }

  @Override
  public void mouseReleased(MouseEvent arg0) {
   // TODO Auto-generated method stub
   
  }
  
 }
}









No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact