Draw 2D graphics object on Click base

Draw 2D graphics object on Click base

By : user2951961
Date : November 18 2020, 03:49 PM
With these it helps Here is my code below. It draw triangles on execution. I want to make a little change in it. Change is , Try the below code, its a sample created from your own code.
code :
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.MouseEvent;
   import java.awt.event.MouseListener;
   public class MyTriangle{
   static  JFrame frame = new JFrame();
   public static void main(String[] args)
    frame.setSize(1000, 1500);
    frame.setTitle("Triangle Draw");

    //add panel to frame and make it visible
    Polygon triangle1 = new Polygon();
    triangle1.addPoint(100, 500); // first
    triangle1.addPoint(600, 500);//last
    triangle1.addPoint(350, 300);//middel
    addTriangle(new Triangle(triangle1));

public static void addTriangle(Triangle triangle1)
static class Triangle extends JPanel implements MouseListener{
    private Polygon triangle;

    public Triangle(Polygon triangle)
        this.triangle = triangle;


    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D)g;


    public void mouseClicked(MouseEvent e) {
        Point p = e.getPoint();
            Polygon triangle2 = new Polygon();
            triangle2.addPoint(200, 300);
            triangle2.addPoint(200, 200);
            triangle2.addPoint(300, 200);
            MyTriangle.addTriangle(new Triangle(triangle2));
            Graphics2D g2d = (Graphics2D)this.getGraphics();


Share : facebook icon twitter icon
C++: Best way to draw a Graphics* object inside another Graphics* object

C++: Best way to draw a Graphics* object inside another Graphics* object

By : Farah Douibi
Date : March 29 2020, 07:55 AM
around this issue You've got the wrong mental image of a Graphics object. It doesn't contain any graphics, it merely has methods that can draw graphics. The only context it keeps is where the graphics are drawn. To a bitmap, the screen, the printer, depending how the object was created. The exact equivalent in the native winapi is a device context, a HDC.
Use a Bitmap or a GraphicsPath instead. Or a function pointer to a drawing function.
C# Graphics Draw Any Object

C# Graphics Draw Any Object

By : Kent Mitsasushi
Date : March 29 2020, 07:55 AM
I wish this help you I want to draw many different shapes on a Windows Form. The following code works only for rectangles. , Something like this:
code :
public abstract class MyShape
   public abstract void Draw(PaintEventArgs args);

public class MyRectangle : MyShape
    public int Height { get; set; }
    public int Width { get;set; }
    public int X { get; set; }
    public int Y { get; set; }

    public override void Draw(Graphics graphics)
            new SolidBrush(Color.Black),
            new Rectangle(X, Y, Width, Height));

public class MyCircle : MyShape
    public int Radius { get; set; }
    public int X { get; set; }
    public int Y { get; set; }

    public override void Draw(Graphics graphics)
        /* drawing code here */

private void Form1_Paint(object sender, PaintEventArgs e)
    List<MyShape> toDraw = new List<MyShape> 
        new MyRectangle
            Height = 10,
            Width: 20,
            X = 0,
            Y = 0
        new MyCircle
           Radius = 5,
           X = 5,
           Y = 5

    toDraw.ForEach(s => s.Draw(e.Graphics));
public static class ShapeExtensions
    public static void Draw(this Rectangle r, Graphics graphics)
        graphics.FillRectangle(new SolidBrush(Color.Black), r);
d3js with angular directive - each click appending new graphics instead of re-draw or updating the data

d3js with angular directive - each click appending new graphics instead of re-draw or updating the data

By : Kovachx
Date : March 29 2020, 07:55 AM
like below fixes the issue The problem is that you're appending items each time you click, and you're not removing anything.
Draw graphics on click and make them stay

Draw graphics on click and make them stay

By : shark
Date : March 29 2020, 07:55 AM
like below fixes the issue From MSDN:
F# WPF script, draw graphics on button click

F# WPF script, draw graphics on button click

By : WildFox
Date : March 29 2020, 07:55 AM
To fix this issue If you add your Paint event handler before the form is drawn for the first time, then it will draw using that handler.
If you add it after, you need to make sure the form then redraws itself. You could for instance call Refresh or Invalidate on it. Ex.:
code :
button.Click.Add(fun _ -> form.Paint.Add(paint); form.Invalidate()) 
Related Posts Related Posts :
  • Content autocomplete in spring tool suite 4
  • Super Noob Help ~ MouseEvent/Frame
  • Making one project with dependencies stand-alone in Eclipse (Java)
  • Jsoup- getting certain attributes from website
  • MongoDB, how to make a link between collections with JAVA code
  • Overriding an abstract method means class abstract class or not in java?
  • Bukkit teleport - nullPointerException
  • How to check if a Float variable is negative in java
  • Servlet: Cannot forward after response has been committed
  • sql query to select a specific cell in excel
  • How to make new picture when mouse dragged in java
  • Use Constants interface in GWT Project
  • Setting size of a JPanel in GroupLayout
  • Construct a map from two equal size arrays
  • Implementing Read-Write Locks with Double-Checked Locking
  • Disable NO_BUTTON from JOptionPane Java if a condition is true
  • UDP multiple socket
  • Android Threading: This Handler class should be static or leaks might occur
  • The type org.openqa.selenium.firefox.FirefoxDriver is not accessible
  • Limitations (drawbacks) of java programming language?
  • Error: ')' expected Error: illegal start of expression
  • TableRowSorter use LookAndFeel of TableHeader, but own Comparator
  • Difference between @GeneratedValue and @GenericGenerator
  • Design Scenario: Smartphone class and will have derived classes like IPhone,AndroidPhone,WindowsMobilePhone can be even
  • Java date parsing without separators?
  • how to map an array of custom type from postgres to java using hibernate
  • Why hibernate always call "update" statement after using "select" statement in MySQL?
  • An interview topic: What can go wrong with this code?
  • How do I regex remove whitespace and newlines from a text, except for when they are in a json's string?
  • How to turn off Preferences > Compiles > User External Build?
  • how to convert arraylist to string array with all items in double quote
  • PMD+Maven+JAVA Error:: Can't find resource rulesets/comments.xml. Make sure the resource is a valid file or URL or is on
  • How to extend AbstractScheduledService.Scheduler
  • Primefaces tabview: set Active Index after some counting
  • Need help in Regex to exclude splitting string within "
  • Get header from HttpUrlConnection object
  • encoding xlsx as base64 and send it as an attachment
  • How to acess HttpServletRequest in Jaas login module in Tomcat
  • How to implement a basic pointer
  • Java Swing Exit Icon?
  • Why onNext has no effect
  • IntelliJ Idea 2018.2 no option for importing gradle project
  • Remove a negative element from an array w/o array.copy
  • Could this prevent overflow in Java?
  • In Java Swing, can I receive Caret events in real time?
  • I need help not using brute force when using JFrames and DrawWindows
  • How do I refactor ArrayList<MyClass> into MyClassLIst?
  • My program keeps saying that the method cannot be resolved
  • BubbleSort -my code returns random addresses
  • JavaFX components inside HTML?
  • Character in Java game not responding
  • Working around access denied in a FileWalking Tree in Java7
  • How to avoid if/else when using multple suppliers?
  • How to pass object from table cell editor to Table Model?
  • Paypal Sandbox payment state pending
  • sejda-console.bat passing parameters with spaces
  • Multiple Consumers Spring Kafka
  • Bitbucket pipeline: environmental variables in build.gradle file not recognized
  • Java static enum method to return a default enum value
  • What is the complexity of empty for loop?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org