「java连续画图」java代码画图

博主:adminadmin 2022-11-27 05:14:07 43

本篇文章给大家谈谈java连续画图,以及java代码画图对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 绘图程序

我基于你原来画图的方法,添加了事件触发的命令b[j].setActionCommand("b" + j);否则你不能在事件响应处理的方法中使用e.getActionCommand(),而且字符串的比较用equals方法比较好。现在可以运行了,你可以看一下:

import java.applet.Applet;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class drawing extends Applet implements ActionListener {

Button b[] = new Button[5];

String fontname = "仿宋_GB2312";

int style = Font.PLAIN;

int size = 24;

int index = 0;

Font myfont;

public void init() {

setSize(700,700);

myfont = new Font(fontname, style, size);

b[0] = new Button("扇形");

b[1] = new Button("圆形");

b[2] = new Button("三角形");

b[3] = new Button("长方形");

b[4] = new Button("椭圆形");

for (int j = 0; j b.length; j++) {

b[j].setBounds(10, 10, 50, 20);

b[j].addActionListener(this);

b[j].setActionCommand("b" + j);

add(b[j]);

}

}

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("b0")) {

index = 0;

repaint();

}

if (e.getActionCommand().equals("b1")) {

index = 1;

repaint();

}

if (e.getActionCommand().equals("b2")) {

index = 2;

repaint();

}

if (e.getActionCommand().equals("b3")) {

index = 3;

repaint();

}

if (e.getActionCommand().equals("b4")) {

index = 4;

repaint();

}

}

public void paint(Graphics g) {

switch (index) {

case 0:

g.fillArc(0, 60, 80, 60, 30, 120);

break;

case 1:

g.drawOval( 300, 50, 60, 60);

break;

case 2:

Polygon filledPolygon = new Polygon();

filledPolygon.addPoint(380, 50);

filledPolygon.addPoint(380, 110);

filledPolygon.addPoint(450, 90);

g.drawPolygon(filledPolygon);

break;

case 3:

g.drawRect( 200, 50, 80, 60);

break;

case 4:

g.drawOval(100, 50, 80, 60);

break;

default:

g.fillArc(0, 60, 80, 60, 30, 120);

break;

}

}

/*

* public void paint(Graphics g) { g.fillArc( 0, 60, 80, 60, 30, 120);

* //绘制扇形 g.drawOval( 100, 50, 80, 60); g.drawRect( 200, 50, 80, 60);

* g.drawOval( 300, 50, 60, 60); Polygon filledPolygon=new Polygon();

* filledPolygon.addPoint(380,50); filledPolygon.addPoint(380,110);

* filledPolygon.addPoint(450,90); g.drawPolygon(filledPolygon); }

*/

}

java画图怎么解决

java创建图形 用的是

java.awt.Grahpic

这个类完成的 包括 自己绘制 使用现成的 .jpg格式的文件什么的.

具体内容太多了..自己看帮助把

----------------------------------------------------

继承

JPanel 类 并且从写 paintComponent 方法 里边有画图的方法

注意这个 Graphics 的对象 画图主要就用这个 需要程序自己去调用自己不能调用 具体的画法 也都是这个类里的方法你自己去看看把

Graphics 是 java.awt包里的类

class A

{

JFrame frame;

public static void main(String[] args)

{

A a=new A();

a.go();

}

public void go()

{

frame =new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyDrawPanel drawPanel =new MyDrawPanel();

frame.getContentPane().add(BorderLayout.CENTER,drawPanel);

frame.setSize(300,300);

frame.setVisible(true);

}

class MyDrawPanel extends JPanel

{

public void paintComponent(Graphics g)

{

g.setColor(Color.red);

g.fillOval(0,0,this.getWidth(),this.getHeight());

}

}

}

java如何实现多线程绘图

首先,如果你只是要实现电子时钟,根本就不需要用到多线程。

如果你真的是要使用,就新建一个类,实现Runnable接口就是了。

如:

class MyThread1 implements Runnable{

public MyThread(){

}

public void run(){

}

}

使用的时候,就:

Thread myThread=new Thread(new MyThread());

myThread.start();

如果要画图,你就直接把组件通过构造方法传到MyThread中就是了

java调用其他类的Paint方法,为何不能连续画图

你的这个repaint 方法不刷新子view 的 视图;

我给你贴出来 方法的内部实现 ;

只刷新你的ht类 的view

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

/**

* Repaints this component.

* p

* If this component is a lightweight component, this method

* causes a call to this component's codepaint/code

* method as soon as possible. Otherwise, this method causes

* a call to this component's codeupdate/code method as soon

* as possible.

* p

* bNote/b: For more information on the paint mechanisms utilitized

* by AWT and Swing, including information on how to write the most

* efficient painting code, see

* a href=""Painting in AWT and Swing/a.

*

* @see #update(Graphics)

* @since JDK1.0

*/

public void repaint() {

repaint(0, 0, 0, width, height);

}

java连续画图的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码画图、java连续画图的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。