java - 如何仅使用 1 个实例且仅使用一个类在 java awt 画布中动态绘制?
问题描述
我无法弄清楚如何通过仅创建一个画布实例而不使用外部自定义画布类和重载 paint 来动态操作 awt canvas 并在按下按钮时提交每个表单。我希望它非常简单并且在一个类中。
我希望用户输入 x 坐标和 y 坐标,我将使用循环链表动态绘制多边形。(这是我的循环链表项目,我必须使用它)我尝试了很多东西,但还没有弄清楚. 到目前为止,这是我的代码。
主要课程:
package com.stabgan.java;
import com.stabgan.java.BCLL;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class methods extends Frame{
BCLL draw = new BCLL();
Canvas can = new Canvas();
Graphics g = getGraphics();
public void paint(BCLL draw , int x , int y) {
if(draw.size == 1) {
g.setColor(Color.red);
g.fillOval(x, y, 3, 3);
}
else if(draw.size == 2){
g.setColor(Color.red);
g.fillOval(x, y, 3, 3);
g.setColor(Color.black);
g.drawLine(draw.head.prev.x, draw.head.prev.y, x, y);
}
else {
g.setColor(Color.red);
g.fillOval(x, y, 3, 3);
g.setColor(Color.black);
g.drawLine(draw.head.prev.x, draw.head.prev.y, x, y);
g.drawLine(draw.head.x, draw.head.y, x, y);
}
}
public methods() {
Label title = new Label();
title.setText("Enter Coordinates");
title.setBounds(95, 40, 120, 20);
add(title);
Button b=new Button("Draw");
b.setBounds(120,150,60,20);// setting button position
TextField x = new TextField("X");
TextField y = new TextField("Y");
x.setBounds(80, 100, 50, 20);
y.setBounds(170, 100, 50, 20);
x.setBackground(Color.lightGray);
y.setBackground(Color.lightGray);
add(x);
add(y);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = x.getText();
String s2 = y.getText();
try {
int xx = Integer.parseInt(s1);
int yy = Integer.parseInt(s2);
draw.append(xx, yy);
paint(draw , xx , yy);
can.paint(g);
}
catch(Exception lol){
}
}
});
add(b);
can.setBounds(0, 300, 300, 400);
can.setBackground(Color.WHITE);
add(can);
setSize(300,800);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);
}
public static void main(String[] args){
new methods();
}
}
链表类:
package com.stabgan.java;
class BCLL {
public class Node {
int x;
int y;
Node next = null;
Node prev = null;
Node(int a,int b) {
this.x = a;
this.y = b;
this.next = this;
this.prev = this;
}
}
public int size = 0;
Node head;
public void append(int x, int y){
if(head == null) {
head = new Node(x,y);
size += 1;
}
else {
Node last = head.prev;
Node temp = new Node(x,y);
last.next = temp;
temp.next = head;
head.prev = temp;
temp.prev = last;
size += 1;
}
}
public void display() {
if (head == null){
System.out.println("Empty list");
}
else {
System.out.println("("+Integer.toString(head.x)+ ","+ Integer.toString(head.y)+")");
if (head.next != null){
Node print = head.next;
while(print != head){
System.out.println("("+Integer.toString(print.x)+ ","+ Integer.toString(print.y)+")");
print = print.next;
}
}
}
}
public int size(){
return this.size;
}
public boolean isempty() {
if (head == null) {
return true;
}
return false;
}
public boolean contains(int a, int b){
Node chk = head;
if (head == null){
System.out.println("Empty list");
return false;
}
else {
if (chk.x == a && chk.y == b){
return true;
}
else {
chk = chk.next;
while(chk != head){
if (chk.x == a && chk.y == b){
return true;
}
chk = chk.next;
}
return false;
}
}
}
public void remove(int a, int b){
Node chk = head;
if (head == null){
System.out.println("Empty list");
}
else {
if (chk.x == a && chk.y == b){
chk.prev.next = chk.next;
chk.next.prev = chk.prev;
return;
}
else {
chk = chk.next;
while(chk != head){
if (chk.x == a && chk.y == b){
chk.prev.next = chk.next;
chk.next.prev = chk.prev;
return;
}
chk = chk.next;
}
System.out.println( Integer.toString(a) +" and "+ Integer.toString(b)+" is not in list");
}
}
}
}
画布上什么也没有显示。请帮忙
解决方案
推荐阅读
- node.js - 如何在 nodejs express 重定向上启用 CORS?
- javascript - 焦点问题
- actionscript-3 - 如何在某些计数器值后停止按钮变为可点击?
- python - Django DateField TypeError 预期的字符串或类似字节的对象
- algorithm - 使用邻接矩阵与邻接链表时 Dijkstra 算法的时间复杂度
- javascript - 如何检测手机缺口?
- mysql - 试图加快对大型表的 mysql 查询
- r - 使用 R 中的文本预测连续变量
- amazon-web-services - 如何在 AWS 中为 DynamoDb 创建 CloudTrail?
- jmeter - Jmeter - 无法生成令牌