java-swing画图
MyRect.java
package Main; public class MyRect { public int x = 0; public int y = 0; public MyRgb rgb = new MyRgb(); long createtime = 0; public MyRect() { } public MyRect(int mx, int my, MyRgb myrgb, long time) { x = mx; y = my; rgb = myrgb; createtime = time; } }
MyRgb.java
package Main; public class MyRect { public int x = 0; public int y = 0; public MyRgb rgb = new MyRgb(); long createtime = 0; public MyRect() { } public MyRect(int mx, int my, MyRgb myrgb, long time) { x = mx; y = my; rgb = myrgb; createtime = time; } }
GraphicsTest.java
package Main; import java.awt.*; import javax.swing.*; import java.awt.image.BufferedImage; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.Dimension; import java.awt.Color; import java.awt.Graphics2D; import java.awt.event.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.im.InputContext; import java.util.Random; import java.util.ArrayList; import java.util.Locale; import Main.MyRgb; public class GraphicsTest extends JPanel { int keyoffsetx = 0; int keyoffsety = 0; ArrayList<MyRgb> rgbList = new ArrayList<MyRgb>(); public GraphicsTest() { MyRgb rgbs1 = new MyRgb(); for(int i = 0; i < 20; i++) { rgbs1 = getRandRgb(); rgbList.add(rgbs1); } } public void keyPressed(String key) { System.out.println("keyPressed: " + key); if(key.equals("A")) { keyoffsetx -= 5; } if(key.equals("D")) { keyoffsetx += 5; } if(key.equals("W")) { keyoffsety -= 5; } if(key.equals("S")) { keyoffsety += 5; } this.updateUI(); } public int randInt(int min, int max) { Random rand1 = new Random(); int randomNum = rand1.nextInt((max - min) + 1) + min; return randomNum; } public Color getColorWithRgbs(MyRgb rgbs) { return new Color(rgbs.r, rgbs.g, rgbs.b); } public MyRgb getRandRgb() { MyRgb res = new MyRgb(randInt(1,255), randInt(1,255), randInt(1,255)); return res; } public MyRgb getReverseRgb(MyRgb rgbs) { MyRgb res = new MyRgb(); res.r = 255 - rgbs.r; res.g = 255 - rgbs.g; res.b = 255 - rgbs.b; return res; } public void paintComponent(Graphics g) { super.paintComponent(g); int panel_width = getWidth(); int panel_height = getHeight(); System.out.println("画布宽度:"+panel_width); System.out.println("画布高度:"+panel_height); System.out.println(); //清除指定矩形区域的像素 //void clearRect(int x, int y, int width, int height) //g.clearRect(0, 0, panel_width, panel_height); //设置画板背景颜色 setBackground(new Color(128, 0, 128)); //设置画板背景图片 Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage("bg001.jpeg"); //void drawImage(Image img, int x, int y, ImageObserver observer) //g.drawImage(image, 0, 0, panel_width, panel_height, this); MyRgb rgbs1 = new MyRgb(); MyRgb rgbs2 = new MyRgb(); int rgbindex = 0; int offsetx = (int)((panel_width-330)/2); int offsety = (int)((panel_height-240)/2); offsetx = offsetx + keyoffsetx; offsety = offsety + keyoffsety; //矩形 rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void drawRect(int x, int y, int width, int height) g.drawRect(offsetx+5, offsety+5, 50, 50); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("矩形", offsetx+20, offsety+30); //填充矩形 rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void fillRect(int x, int y, int width, int height) g.fillRect(offsetx+5, offsety+65, 50, 50); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("填矩", offsetx+20, offsety+90); //绘制一个圆角矩形 rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) g.drawRoundRect(offsetx+5, offsety+125, 50, 50, 8, 8); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("圆矩", offsetx+20, offsety+155); //填充一个圆角矩形 rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) g.fillRoundRect(offsetx+5, offsety+185, 50, 50, 15, 15); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("圆角", offsetx+20, offsety+220); //椭圆 rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void drawOval(int x, int y, int width, int height) g.drawOval(offsetx+65, offsety+5, 50, 50); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("椭圆", offsetx+77, offsety+32); //填充椭圆 rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void fillOval(int x, int y, int width, int height) g.fillOval(offsetx+65, offsety+65, 50, 60); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("填椭", offsetx+77, offsety+97); //线 rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void drawLine(int x1, int y1, int x2, int y2) g.drawLine(offsetx+75, offsety+185, offsetx+145, offsety+205); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("线", offsetx+97, offsety+185); //圆弧 rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) g.drawArc(offsetx+125, offsety+5, 78, 81, 100, 121); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("圆弧", offsetx+137, offsety+37); //填充圆形,扇形 rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) g.fillArc(offsetx+125, offsety+85, 88, 91, 110, 131); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("扇形", offsetx+137, offsety+132); //绘制一个多边形 int[] xPoints1 = new int[]{offsetx+183, offsetx+223, offsetx+331, offsetx+210, offsetx+190}; int[] yPoints1 = new int[]{offsety+10, offsety+41, offsety+71, offsety+81, offsety+91}; int nPoints1 = 5; rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) g.drawPolygon(xPoints1, yPoints1, nPoints1); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("多边形", offsetx+197, offsety+57); //填充一个多边形 int[] xPoints2 = new int[]{offsetx+183, offsetx+223, offsetx+331, offsetx+210, offsetx+190}; int[] yPoints2 = new int[]{offsety+115, offsety+133, offsety+198, offsety+242, offsety+277}; int nPoints2 = 5; rgbs1 = rgbList.get(rgbindex);rgbindex++; rgbs2 = getReverseRgb(rgbs1); g.setColor(getColorWithRgbs(rgbs1)); //void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) g.fillPolygon(xPoints2, yPoints2, nPoints2); g.setColor(getColorWithRgbs(rgbs2)); g.drawString("填多边", offsetx+217, offsety+197); } private static void createAndShowGUI() { // 获取屏幕分辨率 Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize(); double percent = 0.6; int screen_width = screenSize.width; int screen_height = screenSize.height; System.out.println("屏幕宽度:"+screen_width); System.out.println("屏幕高度:"+screen_height); System.out.println(); int window_width = (int)(screen_width * percent); int window_height = (int)(screen_height * percent); System.out.println("窗体宽度:"+window_width); System.out.println("窗体高度:"+window_height); System.out.println(); JFrame frame = new JFrame("GraphicsTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation((screen_width-window_width)/2, (screen_height-window_height)/2); frame.setSize(window_width, window_height); GraphicsTest gtPanel = new GraphicsTest(); // 注册键盘监听器 frame.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { if (e.isAltDown()) { System.out.println("alt"+"键被按下了"); } else if(e.isShiftDown()) { System.out.println("Shift"+"键被按下了"); } else if(e.isControlDown()) { System.out.println("Control"+"键被按下了"); } else { boolean flag = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK); //键入某个键时调用此方法 if(flag) { //System.out.println("现在是大写键状态"); } else { //System.out.println("现在是小写键状态"); } char ch = e.getKeyChar(); //System.out.println("输入的字符是"+ch); } } @Override public void keyPressed(KeyEvent e) { //按下某个键时调用此方法 //System.out.println("Key pressed: " + KeyEvent.getKeyText(e.getKeyCode())); gtPanel.keyPressed(KeyEvent.getKeyText(e.getKeyCode())); } @Override public void keyReleased(KeyEvent e) { //释放某个键时调用此方法 //System.out.println("Key Released: " + KeyEvent.getKeyText(e.getKeyCode())); //System.out.println(); } }); frame.add(gtPanel); frame.setVisible(true); } public static void main(String[] args) { //设置输入法 Locale locale = new Locale("en", "US"); InputContext context = InputContext.getInstance(); context.selectInputMethod(locale); SwingUtilities.invokeLater(GraphicsTest::createAndShowGUI); } }
效果:
本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18392539