问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

java画图形

发布网友 发布时间:2022-04-23 12:55

我来回答

2个回答

热心网友 时间:2023-07-14 06:02

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

}

热心网友 时间:2023-07-14 06:02

当s = 0 ,画出填满的圆形
s = 1 ,画出线
s = 2 ,画出菱形
s = 3 ,画出三角形
s = 4 ,画出正方形
s = 5 ,画出五边形
s = 6, 画出六边形
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
修复征信是不是真的? 市面上征信修复公司可靠吗? 征信修复公司是真是假,真的可以修复吗? Emulex FC HBA卡 皮肤毛囊炎怎样治 银行自动扣款什么原因 银行什么情况自动扣款 贷款自动扣款什么意思 粤省事办理准生证需要多久 粤省事办理准生证步骤 为什么于嘉说山猫队和网队的比赛,少了易建联山猫进攻都没那么流畅了... 怎么解锁移动定制版的iphone6? 猫为什么多次咬破自己的生殖器呢? 猫咪配种回家,依然翘臀,嚎叫,打滚,添屁屁是什么情况? 猫咪小鸡一伸一缩 母猫怀孕总舔屁股正常吗 今天,猫经常翘*、在地上打滚、低声哼哼、**,*有透明的水流出,伴有异味,请问是怎么了? 小公猫睡觉总是吃自己的生殖器怎么回事? 猫咪看到男主人生殖器会咬吗 猫绝育后老是舔丁丁正常吗 猫猫今天特别不正常,一直不正常的抖尾巴,不是晃尾巴,然后还总*毛,尤其是屁屁附近和尾巴根部,还放屁 如何让电脑显示始终以这种方式打开对话框? 为什么我的猫会舔它的小猫?(甚至是生殖器官) 电脑总是出现这个对话框,无法找到入口,怎么解决? 小公猫做完绝育了老是舔他那个那个小生殖器有什么问题没 两只母猫·一直2个多月·一只一个月·大猫总*小猫的生殖器,是怎么回事? 电脑启动总是出现一个对话框 发现猫舍有粉红色液体味道很重,还老 舔生殖器 电脑打开时出现对话框怎么修复呀? 电脑一打开就弹出这样的对话框怎么办 猫咪老是舔屁股怎么回事?如何给猫咪预防寄生虫? 200分求javascrapt代码 什么JAVA游戏好玩 养猪需要注意哪些事项 新手该怎么养猪,养殖过程中要注意些什么问题 推荐几个手机JAVA游戏。 新手养猪第一年,最应该注意的问题是什么? 开始养猪,应该注意一些什么呀? 高手养猪和新手养猪有什么区别,应该注意些什么? 养猪需要注意什么? 新手养肥猪应该注意哪些求大家多多指教 养猪需要注意哪些问题? 请问养猪需注意什么 养猪新手刚开始养猪需要注意什么问题? 新手养猪请教,养猪需要注意些什么? 养猪要注意哪些问题。。。 现在养猪行情还可以,新手养猪有什么需要注意的吗?yZ023.com 用word编辑东西,一直无法重新编号,帮帮我。 想要在农村搞小成本的生态养猪,新手需要注意什么? 养猪应该注意什么 word编号不连续怎么办?