java画图形
写一个Javaapplet,使用者能够输入一个数目n(10<n<100),n为边长或半径,和另一输入数目s选择画出什麼图形当s=0,画出填满的圆形s=1,画出线s=2,画...
写一个Java applet ,使用者能够输入一个数目n(10<n<100),n为边长或半径,
和另一输入数目s选择画出什麼图形
当s = 0 ,画出填满的圆形
s = 1 ,画出线
s = 2 ,画出菱形
s = 3 ,画出三角形
s = 4 ,画出正方形
s = 5 ,画出五边形
s = 6, 画出六边形
随机选择彩虹七色,
并且集中在applet 画面的中心。
暂时我知道以下所需用到的参数
输入数字用JOptionPane
图形
fillOval()
drawLink()
drawRect()
drawPolygon()
颜色
红 Color.red (255,0,0)
橙 Color.orange (255,200,0)
黄 Color.yellow (255,255,0)
绿 Color.green (0,255,0)
蓝 Color.blue (0,0,255)
靛 Color.cyan (0,255,255)
紫 Color.magenta (255,0,255)
希望各位大大帮忙!
希望各位大大能够解答!!! 展开
和另一输入数目s选择画出什麼图形
当s = 0 ,画出填满的圆形
s = 1 ,画出线
s = 2 ,画出菱形
s = 3 ,画出三角形
s = 4 ,画出正方形
s = 5 ,画出五边形
s = 6, 画出六边形
随机选择彩虹七色,
并且集中在applet 画面的中心。
暂时我知道以下所需用到的参数
输入数字用JOptionPane
图形
fillOval()
drawLink()
drawRect()
drawPolygon()
颜色
红 Color.red (255,0,0)
橙 Color.orange (255,200,0)
黄 Color.yellow (255,255,0)
绿 Color.green (0,255,0)
蓝 Color.blue (0,0,255)
靛 Color.cyan (0,255,255)
紫 Color.magenta (255,0,255)
希望各位大大帮忙!
希望各位大大能够解答!!! 展开
2个回答
展开全部
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,
};
}
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,
};
}
博思aippt
2024-07-20 广告
2024-07-20 广告
博思AIPPT是基于ai制作PPT的智能在线工具,它提供了4种AI制作PPT的方式,包括AI生成大纲、AI直接生成PPT、文本生成PPT、AI提炼文档生成PPT,一站式集成多种AI生成PPT的方式,可满足办公用户的不同需求和使用场景。ai生...
点击进入详情页
本回答由博思aippt提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询