Cet article partage avec vous la méthode pour dessiner Gozi Chess Game Board à Java pour votre référence. Le contenu spécifique est le suivant
Module en damier:
Dessiner la carte d'échecs Gozi: 19 lignes horizontales, 19 lignes verticales
Étape 1: Montrez l'échec
J'ai un échec appelé Chessboard.png, situé au répertoire racine / res / drawable / échecboard / png, et maintenant je souhaite afficher cette image.
Drawchessboard.java
Package xchen.test.simplegobang; import java.awt.graphics; import java.awt.image; import java.awt.toolkit; import javax.swing.jpanel; La classe publique Drawchessboard étend JPanel {public Image BoardImg; public drawchessboard () {boardImg = toolkit.getDefaultToolkit (). getImage ("res / drawable / échecboard.png"); if (boardimg == null) System.err.println ("png n'existe pas"); } @Override Protected void PaintComponent (Graphics G) {// TODO Méthode générée automatique Stub super.paintComponent (G); int imgwidth = boardimg.getwidth (this); int imgheight = boardimg.getheight (this); int fwidth = getWidth (); int fheight = getheight (); int x = (fwidth-imgwidth) / 2; int y = (fheight-iMgheight) / 2; G.DrawImage (BoardImg, X, Y, NULL); }} Main.java
Package xchen.test.simplegobang; Importer Java.awt.Container; import javax.swing.jframe; import xchen.test.simplegobang.Drawchessboard; La classe publique Main étend JFrame {Private Drawchess Drawchessboard; public main () {drawchessboard = new Drawchessboard (); // Cadre Title Setttitle ("autonome goji"); Conteneur ContainerPane = getContentPane (); ContainerPane.Add (Drawchessboard); } public static void main (String [] args) {main m = new Main (); M.SetVisible (vrai); }}Courir
Étape 2: Tracez des lignes horizontales et verticales pour les échecs
Drawchessboard.java
Package xchen.test.simplegobang; import java.awt.graphics; import java.awt.image; import java.awt.toolkit; import javax.swing.jpanel; La classe publique Drawchessboard étend JPanel {public Image BoardImg; lignes int privées finales = 19; public drawchessboard () {boardimg = toolkit.getDefaultToolkit (). getImage ("Res / Drawable / Chessboard2.png"); if (boardimg == null) System.err.println ("png n'existe pas"); } @Override Protected void PaintComponent (Graphics G) {// TODO Méthode générée automatique Stub super.paintComponent (G); int imgwidth = boardimg.getwidth (this); int imgheight = boardimg.getheight (this); int fwidth = getWidth (); int fheight = getheight (); int x = (fwidth-imgwidth) / 2; int y = (fheight-iMgheight) / 2; G.DrawImage (BoardImg, X, Y, NULL); int margin = x; int span_x = imgwidth / lignes; int span_y = imgheight / lignes; // dessine la ligne horizontale pour (int i = 0; i <lignes; i ++) {g.drawline (x, y + i * span_y, fwidth-x, y + i * span_y); } // dessiner une ligne verticale pour (int i = 0; i <lignes; i ++) {g.drawline (x + i * span_x, y, x + i * span_x, fheight-y); }}}Main.java est inchangé
Problèmes rencontrés:
1) Eclipse ne reconnaît pas les images sous les dossiers
Problème: il y a des images dans le dossier, mais elles ne sont pas affichées dans la barre du projet Eclipse
Solution: Dans Eclipse, sélectionnez le répertoire racine, F5 Refresh, et il sera affiché.
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.