AWT标签/按钮/文字字段+油漆?

时间:2017-07-10 17:26:55

标签: java awt draw paint

如何在标签/按钮下方绘图? -Java

我想为ax ^ 2 + bx + c写一个坐标计算程序,但这不是重点......

我想通过AWT和paint方法绘制图形。 (尚未实现),但我无法在前台设置paint方法。你能帮我解决一下这个问题,以便油漆至少可见吗?

我附上了我的想法。 抱歉我的英文不好

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();   
    }

}

`

2 个答案:

答案 0 :(得分:0)

您没有在

之前设置颜色
g.fillRect(10, 10, 300, 300);

答案 1 :(得分:0)

我解决了这个问题大约两个月左右,但我现在有点动机,所以我将自己回答我的问题。信息:repaint()调用paint,因此您不必自己实现此方法。

在我忘记的任务中:

  • - 生成画布
  • - 使用布局管理器
  • - 使用方法repaint()调用paint;或更新();

问题只是如何绘画,阅读以下代码后应该清楚:

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);
    }

}