本篇文章的主要内容是关于在Java里用paint方法画图,具有一定参考价值,感兴趣的朋友可以了解一下,希望能对你有所帮助。
java使用paint方法画图
需要继承JFrame类来画窗口 => public class Game extends JFrame {}
setTitle(String s); //设置窗口标题
setLocation(int x, int y); //设置窗口位置
setSize(int width, int height); //设置窗口宽和高
setVisible(true); //设置窗口可见,默认为flase,这个方法放在setLocation()和setSize后面较好,我放在前面窗口为黑色,本来默认为白色的
paint方法画图
定义后自动调用
public class paint(Graphics g) { Color c = g.getColor(); //记录原来的颜色 Font f = g.getFont(); //记录原来的字体 g.setColor(Color.BLACK); //设置画线的颜色 g.drawLine(int x1, int y1, int x2, int y2); //两点画直线 g.drawRect(int x, int y, int width, int height); //左上角顶点加宽高画矩形 g.fillRect(int x, int y, int width, int height); //画填充矩形 g.setFont(new Font("楷体", Font.BOLD, 40)); //设置字体为楷体,粗体,大小为40 g.drawString(str, int x, int y); //画出str字符串 g.setColor(c); //变回原来的颜色 g.setFont(f); //变回原来的字体}
GameUtil工具类导入图片
import java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; public class GameUtil { // 工具类最好将构造器私有化。 private GameUtil() { } public static Image getImage(String path) { BufferedImage bi = null; try { URL u = GameUtil.class.getClassLoader().getResource(path); bi = ImageIO.read(u); } catch (IOException e) { e.printStackTrace(); } return bi; }}
在Game类里面调用GameUtil
Image imag = GameUtil.getImage(“images/picture.png”); //我建立的一个images包,用来存储图片,引号里面为图片的路径
g.drawImage(imag, x, y, width, height, null); //imag图片,位置,宽高,观察者
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import javax.swing.JFrame; public class MyGame extends JFrame{ Image imag = GameUtil.getImage("images/text1.png"); //指定图片 @Override public void paint(Graphics g) { Color c = g.getColor(); Font f = g.getFont(); g.setColor(Color.BLUE); //设置线体颜色 g.drawLine(100, 100, 650, 100); //直线 g.drawRect(50, 150, 200, 200); //空心矩形 g.fillRect(550, 150, 200, 200); //实体矩形 g.drawOval(300, 150, 200, 200); //圆形 g.setFont(new Font("楷体", Font.BOLD, 90)); //设置字体 g.drawString("How are you?", 100, 100); //写字 g.drawImage(imag, 250, 400, 300, 300, null); //插入图片 g.setColor(c); //线条颜色变为原来的样子 g.setFont(f); //字体变为原来的样子 } public void launchJFrame() { this.setTitle("我的游戏"); //设置窗口标题 this.setSize(800, 800); //设置窗口大小 this.setLocation(100, 100); //设置窗口位置 this.setVisible(true); //设置窗口可见 /*this.addWindowListener(new WindowAdapter() { //叉掉窗口后,结束窗口所在的应用程序 @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); */ this.setDefaultCloseOperation(EXIT_ON_CLOSE); //叉掉窗口后,结束窗口所在的应用程序 } public static void main(String args[]) { MyGame game = new MyGame(); game.launchJFrame(); }}
设置图片的大小
public Image getScaledInstance(int width, int height, int hints) //hints - 指示用于图像重新取样的算法类型的标志(这句话不知道是什么意思,照着下面的写就对了)
Image img = GameUtil.getImage("images/text1.jpg");img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
如果是要获取图片的大小,直接使用getWidth()和getHeight()方法就可以了
width = img.getWidth();height = img.getheight();
双缓冲技术解决闪烁
原理大概是:先将所需要画的东西加载到缓冲区,然后将缓冲区中的内容全部画到屏幕上,这样就可以避免因为屏幕加载的东西太多导致屏幕疯狂闪烁
public void paint(Graphics g){ BufferedImage imag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //构建缓冲区 Graphics g2 = imag.creatGraphics(); //新建一支画笔,使用这支画笔来将内容画到缓冲区中 g2.drawRect(...); //括号里面的参数就不写了,此处用来说明一些画图操作 g2.drawImag(...); g2.fillOval(...); g.drawImage(imag, x, y, width, height, null); //将内容画到屏幕上}
相关教程:Java视频教程
以上是java如何使用paint方法画图的详细内容。更多信息请关注PHP中文网其他相关文章!

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代码在不同平台上运行时会有性能差异。1)JVM的实现和优化策略不同,如OracleJDK和OpenJDK。2)操作系统的特性,如内存管理和线程调度,也会影响性能。3)可以通过选择合适的JVM、调整JVM参数和代码优化来提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑战WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript开发工具