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, 画出六边形