这里绘制了一个五角星和一个随窗口大小变化的立方体,然后通过菜单进行切换
java中有直接绘制矩形的函数,但是这里就直接用直线构造,将每个点的坐标都化作窗口宽度的几分之几,这样就可以随之变化啦,当时函数也可以实现,只要我们构造点的时候采用相对坐标就行
立方体
package poly;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class cubeShape extends JPanel{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
double x1=2*this.getWidth()/15;
double y1=2*this.getHeight()/15;
double x2=12*this.getWidth()/15;
double y2=2*this.getHeight()/15;
double x3=4*this.getWidth()/15;
double y3=4*this.getHeight()/15;
double x4=14*this.getWidth()/15;
double y4=4*this.getHeight()/15;
double x5=2*this.getWidth()/15;
double y5=12*this.getHeight()/15;
double x6=12*this.getWidth()/15;
double y6=12*this.getHeight()/15;
double x7=4*this.getWidth()/15;
double y7=14*this.getHeight()/15;
double x8=14*this.getWidth()/15;
double y8=14*this.getHeight()/15;
g2d.setPaint(Color.blue);
g2d.setStroke(new BasicStroke(2.f));
g2d.draw(new Line2D.Double(x1,y1,x2,y2));
g2d.draw(new Line2D.Double(x1,y1,x3,y3));
g2d.draw(new Line2D.Double(x1,y1,x5,y5));
g2d.draw(new Line2D.Double(x1,y1,x2,y2));
g2d.draw(new Line2D.Double(x7,y7,x5,y5));
g2d.draw(new Line2D.Double(x7,y7,x3,y3));
g2d.draw(new Line2D.Double(x7,y7,x8,y8));
g2d.draw(new Line2D.Double(x4,y4,x3,y3));
g2d.draw(new Line2D.Double(x4,y4,x8,y8));
g2d.draw(new Line2D.Double(x4,y4,x2,y2));
float[] dashes= {10};
g2d.setPaint(Color.BLACK);
g2d.setStroke (new BasicStroke(1, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND, 10, dashes, 0));
g2d.draw(new Line2D.Double(x6,y6,x5,y5));
g2d.draw(new Line2D.Double(x6,y6,x8,y8));
g2d.draw(new Line2D.Double(x6,y6,x2,y2));
}
}
五角星
package poly;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class Shape2JPnel extends JPanel{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//Random random = new Random();
int[] xPoints = {55,67,109,73,83,55,27,37,1,43};
int[] yPoints = {0,36,36,54,96,72,96,54,36,36};
Graphics2D g2d = (Graphics2D) g;
GeneralPath star = new GeneralPath();
star.moveTo(xPoints[0], yPoints[0]);
for(int cnt=1;cnt100, 100);
g2d.setColor(new Color(61,89,171));
g2d.fill(star);
}
}
主程序
package poly;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.geom.*;
public class shape2 {
public static void main (String[] args ){
JFrame frame = new JFrame ("");
frame.setSize (315,350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible (true);
cubeShape p = new cubeShape();
Shape2JPnel s = new Shape2JPnel();
JMenuBar menuBar = new JMenuBar ();
JMenu fileMenu = new JMenu ("菜单");
menuBar .add(fileMenu);
JMenuItem rectMenuItem = new JMenuItem("正方体");
JMenuItem StarMenuItem = new JMenuItem("五角星");
fileMenu .add(rectMenuItem);
fileMenu .add(StarMenuItem);
frame.add (p);
p.setVisible (false);
frame.add(s);
s.setVisible (false);
JButton b1 = new JButton("正方体");
JButton b2 = new JButton ("五角星");
rectMenuItem .addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
frame.remove (s);
frame.add (p);
p.setVisible (true);
s.setVisible(false);
}
});
StarMenuItem.addActionListener (new ActionListener (){
public void actionPerformed (ActionEvent e) {
frame.remove(p);
frame.add(s);
s.setVisible (true);
p.setVisible(false);
}
});
frame .setJMenuBar (menuBar);
//frame.setLayout(new FlowLayout());
//frame.setLayout(null);
frame.setVisible (true);
}
}