Draw line inside canvas

user149251 :

I want to draw a line inside the canvas using java. The below program is working fine if I use constant values in g.drawLine. The current code is drawing canvas without lines.

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class Drawing extends Canvas {
    int x1;
    int y1;
    int x2;
    int y2;

    public static void main(String[] args) {

        Drawing dr = new Drawing(100, 100, 200, 200);
        JFrame frame = new JFrame("My Drawing");
        Canvas canvas = new Drawing();
        canvas.setSize(400, 400);
        canvas.setBackground(Color.white);

        frame.add(dr);

        frame.add(canvas);
        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g, Drawing d) {
        super.paint(g);
        g.drawLine(d.x1, d.y1, d.x2, d.y2);

    }

    Drawing(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    Drawing() {

    }
}
MadProgrammer :

First, you need to go read Painting in AWT and Swing to get a better understanding of how painting works in Swing and AWT.

Next, you need to go read through the JavaDocs for Canvas to better understand what functionality you can override.

One of the difficult concepts to understand is, you don't actually control the painting system, that's taken care of for you (it's like black magic), you just need to work with it, by overriding the appropriate methods and interacting with the API to request updates when needed.

The biggest problem with your code is public void paint(Graphics g, Drawing d) {. Nothing is going to call it, as it's not a method that the paint system recognises. It'd also question why you need to pass a reference of Drawing to an instance of Drawing, not sure about all that.

The other issue you're having is ...

Drawing dr = new Drawing(100, 100, 200, 200);
JFrame frame = new JFrame("My Drawing");
Canvas canvas = new Drawing();
canvas.setBackground(Color.white);

frame.add(dr);

frame.add(canvas);

You're adding two instances of Drawing to the JFrame, because of the way the default, BorderLayout works, only the second one will ever get laid out, the first will be ignored.

Again, not sure why, just add the first one and be done with it.

Something that "works" might look something like...

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;

public class Drawing extends Canvas {

    int x1;
    int y1;
    int x2;
    int y2;

    public static void main(String[] args) {

        Drawing dr = new Drawing(100, 100, 200, 200);
        JFrame frame = new JFrame("My Drawing");
        frame.add(dr);
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawLine(x1, y1, x2, y2);
    }

    Drawing(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    Drawing() {

    }
}

You seem to be making fundamental mistakes with the use of the API and I would strongly encourage you to spend some time reading through Creating a GUI With JFC/Swing. You'll also find that using something like JPanel instead of Canvas will give you better performance and results

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161364&siteId=1