简体   繁体   中英

AWT Label/Button/Textfield + paint?

how to draw below a label / button? -JAVA

I wanted to write a coordinate calculation program for ax^2 + bx + c, but thats not the important point...

I'd like to draw the graph only via AWT and the paint method. (not implemented yet), but I'm not able to set the paint method in the foreground. Can you please help me to fix the problem so that the paint is at least visible?

I have attached a outline of my thoughts. Sorry for my bad english

idea

import java.awt.Button;
import java.awt.Canvas;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;

public class MyWindow extends Frame {
    Frame frame;
    Label l1;
    Label l2;
    Label l3;
    Button b1;
    TextField t1;
    TextField t2;
    TextField t3;
    Panel panel;
    Canvas canvas;

    MyWindow() {
        frame = new Frame("GraphPlotter");
        frame.setSize(800, 800);
        frame.setLayout(new FlowLayout());

        l1 = new Label("f(x)= ");
        l2 = new Label(" x^2 + ");
        l3 = new Label(" x + ");

        t1 = new TextField("1");
        t2 = new TextField("2");
        t3 = new TextField("3");

        b1 = new Button("Plot");


        frame.add(l1);
        frame.add(t1);
        frame.add(l2);
        frame.add(t2);
        frame.add(l3);
        frame.add(t3);
        frame.add(b1);
        frame.setVisible(true);


    }

    public void paint(Graphics g) {
        g.fillRect(10, 10, 300, 300); // ist im "Hintergrund"??
    }
}

`   public class Start {

    public static void main(String[] args) {
        MyWindow window = new MyWindow();   
    }

}

`

您之前没有设置颜色

g.fillRect(10, 10, 300, 300);

I solved that problem like two months or so, but I'm a bit motivated at the moment, so I am going to answer my question on my own. Info: repaint() calls paint, so you don't have to implement this method on your own.

in the task I forgot to:

  • -generate a canvas
  • -use layout manager
  • -call paint with the methods repaint(); or update();

The question was only how to paint and this should be clear after reading following code:

package delete;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;

public class AwtFrame extends Frame {
    AwtCanvas c;
    Panel p1;
    Label la_ax2, la_bx, la_c;
    TextField tf_Ax2, tf_bx, tf_c;

    public AwtFrame() {
        super("paint example.");
        this.setSize(800, 800);

        initComp();
        addComp();

        // TODO: write a graph drawing method
        c.repaint(); // paints a line of your graph

        this.setVisible(true);
    }

    public void initComp() {
        p1 = new Panel();
        la_ax2 = new Label("x^2 ");
        la_bx = new Label("+ x ");
        la_c = new Label("+ c ");

        tf_Ax2 = new TextField(0);
        tf_bx = new TextField(0);
        tf_c = new TextField(0);
        c = new AwtCanvas();
    }

    public void addComp() {
        this.setLayout(new BorderLayout());
        p1.setLayout(new FlowLayout());

        this.add(p1, BorderLayout.NORTH);
        this.add(c, BorderLayout.CENTER);

        p1.add(tf_Ax2);
        p1.add(la_ax2);
        p1.add(tf_bx);
        p1.add(la_bx);
        p1.add(tf_c);
        p1.add(la_c);
    }

    public static void main(String[] args) {
        new AwtFrame();
    }
}



package delete;

import java.awt.Canvas;
import java.awt.Graphics;
import java.util.LinkedList;

public class AwtCanvas extends Canvas {
//  LinkedList<ToImplement> coords = new LinkedList<ToImplement>();


    // TODO: implement plotter
    public void paint(Graphics g) {
        g.drawLine(40, 40, 100, 100);
        g.drawLine(54, 22, 300, 200);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM