绘制图形java代码,java图形代码大全

用java 在窗体中画一个简单图形。

帮你改了一下。

成都创新互联公司是一家专注网站建设、网络营销策划、微信小程序开发、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立十多年以来,已经为上1000家成都效果图设计各业的企业公司提供互联网服务。现在,服务的上1000家客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。

你要画在panel上,然后frame.add(panel)就能显示。

是不是和applet搞混了,applet复写一些方法就能显示,但现在你编的是java gui

import java.awt.*;

import java.awt.Event.*;

import javax.swing.*; //import javax.swing.Timer;

import java.awt.BasicStroke;

//import java.util.Date;

//import java.text.*;

//import java.util.*;

public class TestGui {

public void paint(Graphics g) {

Graphics2D a2d = (Graphics2D) g;

int x = 120, y = 90, width = 150, height = 150;

a2d.setColor(Color.red);

a2d.setStroke(new BasicStroke(3.0f)); // 设置线条宽度,3.0即线的宽度

a2d.drawOval(x, y, width, height);

}

public static void main(String[] args) {

JFrame frame = new JFrame();

// frame.add(new paint(),BorderLayout.CENTER);

frame.setSize(500, 500);

frame.setLocation(200, 200);

frame.setVisible(true);

Panel p = new Panel();

frame.add(p);

// frame.paint(null);

// TODO code application logic here

}

}

class Panel extends JPanel {

// 重新覆盖paint方法

public void paint(Graphics g) {

super.paint(g);

Graphics2D a2d = (Graphics2D) g;

int x = 120, y = 90, width = 150, height = 150;

a2d.setColor(Color.red);

a2d.setStroke(new BasicStroke(3.0f)); // 设置线条宽度,3.0即线的宽度

a2d.drawOval(x, y, width, height);

}

}

求一个最简单的在JAVA上画图形的代码。

import java.awt.*;

class MyFrame extends Frame{

MyPanel p = new MyPanel();

public MyFrame(){

this.add(p);

this.setSize(300,300);

//省略了关闭窗口的代码

this.setVisible(true);

}

public static void main(String[] args){

new MyFrame();

}

class MyPanel extends Panel

{

public void paint(Graphics g){

g.drawLine(0,0,300,300);

}

}

}

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画图形

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.BorderFactory;

import javax.swing.ComboBoxModel;

import javax.swing.DefaultComboBoxModel;

import javax.swing.JApplet;

import javax.swing.JComboBox;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JSpinner;

import javax.swing.SpinnerNumberModel;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

public class GraphTest extends JApplet {

private static final long serialVersionUID = -3038851324293731224L;

private PaintingPane paintingPane;

private Painter currentPainter = Painter.CIRCLE;

private final static int DEFAULT_RADIUS = 50;

private int radius = DEFAULT_RADIUS;

private Color color = getColor();

public void init() {

this.setLayout(new BorderLayout());

paintingPane = new PaintingPane();

paintingPane.setPreferredSize(new Dimension(250, 250));

this.add(paintingPane, BorderLayout.CENTER);

JPanel controlPane = new JPanel();

final SpinnerNumberModel sm = new SpinnerNumberModel(DEFAULT_RADIUS, 10, 100, 1);

JSpinner spinner = new JSpinner(sm);

spinner.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e) {

radius = sm.getNumber().intValue();

color = getColor();

repaint();

}

});

final ComboBoxModel cbm = new DefaultComboBoxModel(Painter.values());

JComboBox combo = new JComboBox(cbm);

combo.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

if (e.getStateChange() == ItemEvent.SELECTED) {

currentPainter = (Painter) cbm.getSelectedItem();

color = getColor();

repaint();

}

}

});

JLabel spinnerLabel = new JLabel("Radius: ");

JLabel comboLabel = new JLabel("Figure: ");

spinnerLabel.setLabelFor(spinner);

comboLabel.setLabelFor(combo);

controlPane.setPreferredSize(new Dimension(150, 250));

controlPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));

controlPane.add(spinnerLabel);

controlPane.add(spinner);

controlPane.add(comboLabel);

controlPane.add(combo);

this.add(controlPane, BorderLayout.EAST);

this.setSize(400, 200);

this.setVisible(true);

}

private enum Painter {

CIRCLE {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

g.setColor(color);

g.fillOval(d.width / 2 - parameter, d.height / 2 - parameter,

parameter * 2, parameter * 2);

}

public String toString() { return "Circle"; }

},

LINE {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xOffset = d.width / 2, yOffset = d.height / 2;

g.setColor(color);

g.drawLine(xOffset - parameter, yOffset, xOffset + parameter,

yOffset);

}

public String toString() { return "Line"; }

},

DIAMOND {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int offset = (int) (parameter / SQUARE_ROOT_2);

int[] xCoors = {

xCenter - offset, xCenter, xCenter + offset, xCenter,

};

int[] yCoors = {

yCenter, yCenter + offset, yCenter, yCenter - offset,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 4);

}

public String toString() { return "Diamond"; }

},

TRIANGLE {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int offsetPos = (int) (parameter / SQUARE_ROOT_3);

int offsetNeg = offsetPos / 2;

int offsetX = parameter / 2;

int[] xCoors = {

xCenter - offsetX, xCenter + offsetX, xCenter,

};

int[] yCoors = {

yCenter + offsetNeg, yCenter + offsetNeg,

yCenter - offsetPos,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 3);

}

public String toString() { return "Triangle"; }

},

SQUARE {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int offset = parameter / 2;

int[] xCoors = {

xCenter - offset, xCenter + offset, xCenter + offset,

xCenter - offset,

};

int[] yCoors = {

yCenter + offset, yCenter + offset,

yCenter - offset, yCenter - offset,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 4);

}

public String toString() { return "Square"; }

},

PENTAGON {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int xOffset2 = parameter / 2;

int yOffset3 = (int) (xOffset2 / TANGENT_36);

int yOffset1 = (int) (xOffset2 / SINE_36);

int xOffset1 = (int) (yOffset1 * COSINE_18);

int yOffset2 = (int) (yOffset1 * SINE_18);

int[] xCoors = {

xCenter - xOffset1, xCenter - xOffset2, xCenter + xOffset2,

xCenter + xOffset1, xCenter,

};

int[] yCoors = {

yCenter - yOffset2, yCenter + yOffset3, yCenter + yOffset3,

yCenter - yOffset2, yCenter - yOffset1,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 5);

}

public String toString() { return "Pentagon"; }

},

HEXAGON {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int xOffset = parameter / 2;

int yOffset = (int) (xOffset * SQUARE_ROOT_3);

int[] xCoors = {

xCenter - parameter, xCenter - xOffset, xCenter + xOffset,

xCenter + parameter, xCenter + xOffset, xCenter - xOffset,

};

int[] yCoors = {

yCenter, yCenter + yOffset, yCenter + yOffset,

yCenter, yCenter - yOffset, yCenter - yOffset,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 6);

}

public String toString() { return "Hexagon"; }

};

abstract void paint(Graphics g, JPanel panel, int parameter,

Color color);

static final double SQUARE_ROOT_2 = Math.pow(2.0, 0.5);

static final double SQUARE_ROOT_3 = Math.pow(3.0, 0.5);

static final double SINE_36 = Math.sin(Math.PI / 5);

static final double TANGENT_36 = Math.tan(Math.PI / 5);

static final double COSINE_18 = Math.cos(Math.PI / 10);

static final double SINE_18 = Math.sin(Math.PI / 10);

}

private class PaintingPane extends JPanel {

private static final long serialVersionUID = 8856506825301606303L;

public void paint(Graphics g) {

super.paint(g);

currentPainter.paint(g, this, radius, color);

}

}

private static Color getColor() {

return possibleColors[(int) (Math.random() * 7)];

}

private static final Color[] possibleColors = {

Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN,

Color.CYAN, Color.BLUE, Color.MAGENTA,

};

}

关于java中画图形的paint方法

代码如下:

/**分析下例:我们只是new了一个对象并没有调用Paint()方法那为什么会画出图呢?

* Graphics这个类的对象就是一只画笔,当某容器调用paint()时就会在该容器中画图。

* 当窗口产生时本身就存在一只画笔,我们只需要拿到画笔重写Paint()便可以随心作画。

*每次需要重画的时候就会自动调用paint(Graphics g)(什么时候需要重画呢?如当窗口被覆盖又重新置顶时,当窗口最小化又最大化时等等)

*/

总结:我们想要在容器中画图时只需要做的就是:  在该容器中重写Paint() 系统会自动传给我们画笔,自动调用paint方法按照我们的意愿进行作画。

public class TestGraphics extends Frame. {

public static void main(String []args) {

new TestGraphics("画图",100,100,200,200,Color.white);

}

public TestGraphics(String s,int x,int y,int w,int h,Color c) {

super(s);

this.setBounds(x, y, w, h);

this.setBackground(c);

this.setVisible(true);

}

public void paint(Graphics g) {

Color c = g.getColor();

g.setColor(Color.magenta);

g.fillOval(100, 100, 50, 50);

g.setColor(Color.green);

g.fill3DRect(60, 100, 50, 50, false);

g.setColor(c);

}

}

小例子2:

import java.awt.*;

import java.awt.event.*;

import java.util.*;

/**

原理是:在Frame中增加成员变量-容器ArrayList,用它来容纳点,每次点击鼠标就触发了事件:往容器中添加一个点,然后立即调用repaint方法强制画出容器中所有的点来

所以我们利用容器来"装"点,然后通过iterator来遍历画出所有的点。

适配器类:使用适配器类可以只重写我们需要的方法

因为这些适配器类都已经实现了相应的接口即把所有的方法都空实现了一遍 我们只需要重写我们需要的方法即可

repaint -调用- update() - 调用 - paint();

*/

public class MyFrame. extends Frame. {

ArrayListPointal ;//泛型指定容器中只能放入Point

public MyFrame(String s) {

super(s);

al =new ArrayListPoint();

this.setBounds(100, 100, 200, 200);

this.setBackground(Color.darkGray);

this.setVisible(true);

this.addMouseListener(new MouseAdapter(){//匿名类

@Override

public void mousePressed(MouseEvent e) {

MyFrame. f = (MyFrame)e.getSource();//e是事件,e.getSource()是获取事件源即窗口 f

f.al.add(new Point(e.getX(),e.getY())); //而e.getX(),e.getY()则是获取事件发生的x,y坐标

repaint();//每次点击鼠标触发事件时都有了新的点,所以强制要求重画,才能立刻显示出该点否则只有窗口被最小化又最大化后才能看到刚才的点

}

});

//匿名类:在参数处我们传递的是new WindowAdapter()  {匿名类体} );他没有名字,我们把它当成WindowAdapter来用,为什么可以这样呢?因为语法上规定了匿名类要么是实现了前面的接口,要么是从前面的类继承,就着前面父类的名字来写类体。当此类与其他类关系不大时可以用匿名类

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

public void paint(Graphics g) {

Iterator Pointit= al.iterator();//泛型指定取出元素时只能是point

while(it.hasNext()) {

Point p = it.next();//由于使用泛型这时候就不用强制转换了

Color c = g.getColor();//保护原有颜色

g.setColor(Color.pink);

g.fillOval(p.x-6, p.y-6, 12, 12);

g.setColor(c);

}

}

public static void main(String []args) {

new MyFrame("点击");

}

}


本文标题:绘制图形java代码,java图形代码大全
文章链接:http://cdiso.cn/article/dsgcdhp.html

其他资讯