久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <small id='gOSNO'></small><noframes id='gOSNO'>

      <bdo id='gOSNO'></bdo><ul id='gOSNO'></ul>

        <i id='gOSNO'><tr id='gOSNO'><dt id='gOSNO'><q id='gOSNO'><span id='gOSNO'><b id='gOSNO'><form id='gOSNO'><ins id='gOSNO'></ins><ul id='gOSNO'></ul><sub id='gOSNO'></sub></form><legend id='gOSNO'></legend><bdo id='gOSNO'><pre id='gOSNO'><center id='gOSNO'></center></pre></bdo></b><th id='gOSNO'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='gOSNO'><tfoot id='gOSNO'></tfoot><dl id='gOSNO'><fieldset id='gOSNO'></fieldset></dl></div>
        <tfoot id='gOSNO'></tfoot>

        <legend id='gOSNO'><style id='gOSNO'><dir id='gOSNO'><q id='gOSNO'></q></dir></style></legend>

        在jframe中創建java的正方形、矩形、三角形

        Create the square, rectangle, triangle of java in jframe(在jframe中創建java的正方形、矩形、三角形)
            • <legend id='abki7'><style id='abki7'><dir id='abki7'><q id='abki7'></q></dir></style></legend>

            • <i id='abki7'><tr id='abki7'><dt id='abki7'><q id='abki7'><span id='abki7'><b id='abki7'><form id='abki7'><ins id='abki7'></ins><ul id='abki7'></ul><sub id='abki7'></sub></form><legend id='abki7'></legend><bdo id='abki7'><pre id='abki7'><center id='abki7'></center></pre></bdo></b><th id='abki7'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='abki7'><tfoot id='abki7'></tfoot><dl id='abki7'><fieldset id='abki7'></fieldset></dl></div>

              <small id='abki7'></small><noframes id='abki7'>

                <tbody id='abki7'></tbody>
                <bdo id='abki7'></bdo><ul id='abki7'></ul>
                  <tfoot id='abki7'></tfoot>

                  本文介紹了在jframe中創建java的正方形、矩形、三角形的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  Java 有問題據我所知沒有用Java 來繪制幾何圖形,代碼和下面的你能幫我嗎?

                  這是代碼:

                  公共類 Gioco {公共靜態無效主要(字符串參數[]){油漆矩形();}公共靜態無效PaintRect(){g.drawRect(100,100,300,300);g.drawLine(100,100,100,100);g.setBackground(Color.BLACK);System.out.println("Trasut");credits.setText("Kitebbiv");credits.setBackground(null);credits.setEditable(false);credits.setFocusable(false);credits.setBounds(0,0,100,100);credits.setForeground(Color.BLACK);panel.add(學分);g.getPaint();}

                  如何創建一個 JFrame 三角形、正方形和矩形?更正我的代碼謝謝

                  解決方案

                  在我開始寫我的答案之前,我需要鼓勵你仔細閱讀:如何創建一個有效的

                  import java.awt.Dimension;導入 java.awt.Graphics;導入 javax.swing.JFrame;導入 javax.swing.JPanel;導入 javax.swing.SwingUtilities;公共類 ShapesDrawing {私有 JFrame 框架;私有 JPanel 窗格;公共靜態無效主要(字符串[]參數){SwingUtilities.invokeLater(new Runnable() {公共無效運行(){新 ShapesDrawing().createAndShowGui();}});}公共無效createAndShowGui(){框架 = 新 JFrame(getClass().getSimpleName());窗格 = 新 JPanel() {@覆蓋受保護的無效paintComponent(圖形g){super.paintComponent(g);//總是先調用這個方法!g.drawRect(10, 10, 50, 50);//畫正方形g.drawRect(10, 75, 100, 50);//繪制矩形g.drawPolygon(new int[] {35, 10, 60}, new int[] {150, 200, 200}, 3);//繪制三角形g.dispose();}@覆蓋公共維度 getPreferredSize() {返回新維度(300, 300);}};框架.添加(窗格);框架.pack();frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}

                  我真的希望你閱讀我在這里發布的每一個鏈接,因為它是值得的

                  如果您需要填充形狀,請調用 fillRectfillPolygon 而不是 drawRectdrawPolygon:

                  @Override受保護的無效paintComponent(圖形g){super.paintComponent(g);//總是先調用這個方法!g.drawRect(10, 10, 50, 50);//畫正方形g.fillRect(150, 10, 50, 50);//填充一個正方形g.drawRect(10, 75, 100, 50);//繪制矩形g.fillRect(150, 70, 100, 50);//填充一個正方形g.drawPolygon(new int[] {35, 10, 60}, new int[] {150, 200, 200}, 3);//繪制三角形g.fillPolygon(new int[] {185, 150, 215}, new int[] {150, 200, 200}, 3);//填充三角形g.dispose();}

                  <小時>

                  編輯

                  根據@MadProgrammer 的評論:

                  <塊引用>

                  我們是否可以避免使用 draw/fillPolygon 來支持使用更新后的 Shapes API ...提供更多功能并且通常更易于使用:P

                  這是使用 Shapes API 的更新后的 paintComponent 方法:

                  @Override受保護的無效paintComponent(圖形g){super.paintComponent(g);//總是先調用這個方法!Graphics2D g2d = (Graphics2D) g;g2d.draw(new Rectangle2D.Double(10, 10, 50, 50));g2d.fill(new Rectangle2D.Double(150, 10, 50, 50));g2d.draw(new Rectangle2D.Double(10, 75, 100, 50));g2d.fill(new Rectangle2D.Double(150, 75, 100, 50));g2d.draw(new Polygon(new int[] {35, 10, 60}, new int[] {150, 200, 200}, 3));g2d.fill(new Polygon(new int[] {185, 150, 215}, new int[] {150, 200, 200}, 3));g2d.dispose();g.dispose();}

                  產生以下輸出:

                  I have a problem with Java As I understood not come Draw Geometric figures in Java, the code and the Following you can help me?

                  This is the code:

                  public class Gioco {
                  
                  
                      public static void main (String args [])
                      {
                          PaintRect();
                  
                      }
                  
                      public static void PaintRect() {
                  
                          g.drawRect(100,100,300,300);
                          g.drawLine(100,100,100,100);
                          g.setBackground(Color.BLACK);
                          System.out.println("Trasut");
                          credits.setText("Kitebbiv");
                          credits.setBackground(null);
                          credits.setEditable(false);
                          credits.setFocusable(false);
                          credits.setBounds(0,0,100,100);
                          credits.setForeground(Color.BLACK);
                          panel.add(credits);
                          g.getPaint();
                      }
                  

                  How can I create a JFrame the triangle, square and rectangle? correct my code thanks

                  解決方案

                  Before I even start writing my answer I need to encourage you to read carefully to: How to create a valid Minimal, Complete and Verifiable Example and a Short, Self Contained, Correct Example.


                  1. From your (now deleted) code, I see you haven't gone through the Swing Tutorial on Custom Painting yet or you didn't even pay attention to it, this line will cause you problems

                    static Graphics2D g = new Graphics2D() 
                    

                  2. The excessive use of static modifier will harm you, static isn't a cross-method magic word to make your variables be accessible anywhere in your program, you should instead create an instance of your program and call the methods from there (they not being static), see Why are static variables considered evil?, and you should really go back and learn the essentials before adding more complexity to your learning with a GUI and even more with Swing custom painting.

                  3. You're making use of setBounds() method, which suggests (and I can confirm in your deleted code) that you're using a null-layout:

                    panel.setLayout(null);
                    

                    You should really consider checking the layout managers

                  4. You're making use of a deprecated method JFrame#show() instead you should be using JFrame#setVisible() method.

                  5. You're manually setting the size of your JFrame, you should instead use a layout manager and call the method JFrame#pack() which will calculate the preferred size for your JFrame or override your component's getPreferredSize().

                  6. On your deleted code, you had a MouseListener attached to a JButton, instead you need to use an ActionListener, see How to use Actions to learn this.

                  7. You're not placing your program on the Event Dispatch Thread (EDT) which could make your program to freeze, because Swing is not Thread safe. You can correct this by writing your main method as follows:

                    public static void main(String[] args) {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                //Your constructor here
                            }
                        });
                    }
                    

                  8. You should be more polite: "correct my code thanks" sounds like an order, I would have said that like could you help me? which sounds like a request / petition for someone to give you a hand, because they can, not because they must help you, all the above points correct your code.


                  After all the above has being said (and which you should read carefully) we can continue to the coding part:

                  In order to draw a rectangle we need to learn something about a rectangle:

                  • A rectangle has a width and a height, both are different
                  • The way to draw a rectangle in Swing is with drawRect(x, y, width, height) draw(Shape) of the Graphics2D method where Shape would be an instance of Rectangle2D

                  To draw a square we need to know that:

                  • A square has a width and a height, both are equal size
                  • The way to draw a square in Swing is with drawRect(x, y, width, height) draw(Shape) of the Graphics2D method where Shape would be an instance of Rectangle2D

                  You must be saying... "But the method you're using to draw the square is the same as the rectangle!", well... yep, we are, the difference lies in that we're going to pass a width and height equal size for the square and different size for the rectangle.

                  To draw a triangle you need to know that:

                  • A triangle has 3 sides, they can be same or different sizes
                  • We have no method to drawTriangle in Swing, but we have drawPolygon(xPoints, yPoints, nPoints) draw(Shape) of the Graphics2D method, which will draw a Polygon of nPoints (3 in this case), taking the coords from each array element of xPoints for the X coords and yPoints for the Y coords and where Shape would be an instance of Polygon

                  Now, putting all that together we should have all that code in an overridden method of our JPanel called paintComponent() as shown in the tutorial (See point #1). It should look like this:

                  @Override
                  protected void paintComponent(Graphics g) {
                      super.paintComponent(g); //ALWAYS call this method first!
                      g.drawRect(10, 10, 50, 50); //Draws square
                      g.drawRect(10, 75, 100, 50); //Draws rectangle
                      g.drawPolygon(new int[] {35, 10, 60}, new int[] {150, 200, 200}, 3); //Draws triangle
                  }
                  

                  But we also need to override another method getPreferredSize() on our JPanel, (see: Should I avoid the use of setPreferred|Maximum|MinimumSize in Swing? the general consensus says yes), otherwise our JFrame will be smaller than what we want, so it should look like this:

                  @Override
                  public Dimension getPreferredSize() {
                      return new Dimension(300, 300);
                  }
                  

                  Don't forget to call @Override in those methods!

                  With only those methods we have completed our program to draw the shapes, but I know that if I don't post the whole code you'll end up writing the above methods in a place that won't work or cause you compilation errors, so the whole code (which in fact is a MCVE or SSCCE (see the very first paragraph in this answer to see what each means)) that produces the below output is:

                  import java.awt.Dimension;
                  import java.awt.Graphics;
                  
                  import javax.swing.JFrame;
                  import javax.swing.JPanel;
                  import javax.swing.SwingUtilities;
                  
                  public class ShapesDrawing {
                  
                      private JFrame frame;
                      private JPanel pane;
                  
                      public static void main(String[] args) {
                          SwingUtilities.invokeLater(new Runnable() {
                              public void run() {
                                  new ShapesDrawing().createAndShowGui();
                              }
                          });
                      }
                  
                      public void createAndShowGui() {
                          frame = new JFrame(getClass().getSimpleName());
                          pane = new JPanel() {
                              @Override
                              protected void paintComponent(Graphics g) {
                                  super.paintComponent(g); //ALWAYS call this method first!
                                  g.drawRect(10, 10, 50, 50); //Draws square
                                  g.drawRect(10, 75, 100, 50); //Draws rectangle
                                  g.drawPolygon(new int[] {35, 10, 60}, new int[] {150, 200, 200}, 3); //Draws triangle
                                  g.dispose();
                              }
                  
                              @Override
                              public Dimension getPreferredSize() {
                                  return new Dimension(300, 300);
                              }
                          };
                  
                          frame.add(pane);
                          frame.pack();
                          frame.setVisible(true);
                          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      }
                  }
                  

                  I really hope you read every link I posted here, because it's worth it

                  And if you need to fill the shapes then call fillRect and fillPolygon instead of drawRect and drawPolygon:

                  @Override
                  protected void paintComponent(Graphics g) {
                      super.paintComponent(g); //ALWAYS call this method first!
                      g.drawRect(10, 10, 50, 50); //Draws square
                      g.fillRect(150, 10, 50, 50); //Fills a square
                      g.drawRect(10, 75, 100, 50); //Draws rectangle
                      g.fillRect(150, 70, 100, 50); //Fills a square
                      g.drawPolygon(new int[] {35, 10, 60}, new int[] {150, 200, 200}, 3); //Draws triangle
                      g.fillPolygon(new int[] {185, 150, 215}, new int[] {150, 200, 200}, 3); //Fills triangle
                      g.dispose();
                  }
                  


                  Edit

                  As per @MadProgrammer's comment:

                  Could we avoid using draw/fillPolygon in favor of using the updated Shapes API ... provides much more functionality and is generally easier to use :P

                  Here's the updated paintComponent method using the Shapes API:

                  @Override
                  protected void paintComponent(Graphics g) {
                      super.paintComponent(g); //ALWAYS call this method first!
                  
                      Graphics2D g2d = (Graphics2D) g;
                      g2d.draw(new Rectangle2D.Double(10, 10, 50, 50));
                      g2d.fill(new Rectangle2D.Double(150, 10, 50, 50));
                  
                      g2d.draw(new Rectangle2D.Double(10, 75, 100, 50));
                      g2d.fill(new Rectangle2D.Double(150, 75, 100, 50));
                  
                      g2d.draw(new Polygon(new int[] {35, 10, 60}, new int[] {150, 200, 200}, 3));
                      g2d.fill(new Polygon(new int[] {185, 150, 215}, new int[] {150, 200, 200}, 3));
                  
                      g2d.dispose();
                      g.dispose();
                  }
                  

                  Which produces the following output:

                  這篇關于在jframe中創建java的正方形、矩形、三角形的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                  How to convert Integer to int?(如何將整數轉換為整數?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)
                    <tbody id='0DMqJ'></tbody>

                  <small id='0DMqJ'></small><noframes id='0DMqJ'>

                    <bdo id='0DMqJ'></bdo><ul id='0DMqJ'></ul>

                      <i id='0DMqJ'><tr id='0DMqJ'><dt id='0DMqJ'><q id='0DMqJ'><span id='0DMqJ'><b id='0DMqJ'><form id='0DMqJ'><ins id='0DMqJ'></ins><ul id='0DMqJ'></ul><sub id='0DMqJ'></sub></form><legend id='0DMqJ'></legend><bdo id='0DMqJ'><pre id='0DMqJ'><center id='0DMqJ'></center></pre></bdo></b><th id='0DMqJ'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0DMqJ'><tfoot id='0DMqJ'></tfoot><dl id='0DMqJ'><fieldset id='0DMqJ'></fieldset></dl></div>
                    1. <tfoot id='0DMqJ'></tfoot>

                          1. <legend id='0DMqJ'><style id='0DMqJ'><dir id='0DMqJ'><q id='0DMqJ'></q></dir></style></legend>
                            主站蜘蛛池模板: 日本国产欧美 | 国产精品日本一区二区在线播放 | 久久精品免费 | 欧美成ee人免费视频 | 欧美一区二区免费在线 | 欧美精品福利视频 | 久久久久久国产精品久久 | 九九伦理电影 | 日韩成人免费视频 | 99视频精品 | 色综合久久天天综合网 | 午夜一区二区三区在线观看 | 亚洲精品久久 | av一级久久 | 一区二区视频在线观看 | 天天综合亚洲 | 精品视频在线观看 | 99久久婷婷国产综合精品首页 | 欧美性吧 | 天天拍天天操 | 精品丝袜在线 | 91精品国产91久久久久福利 | 精品一区二区视频 | 国产精品无 | 国产成人精品免高潮在线观看 | 中文字幕成人av | 牛牛热在线视频 | 欧美极品在线播放 | 国产特级毛片aaaaaa | 日本电影一区二区 | 两性午夜视频 | 午夜精品久久久久久久久久久久 | 91精品国产91久久久久久最新 | 盗摄精品av一区二区三区 | 亚洲精品乱码8久久久久久日本 | 精久久久| 97国产一区二区精品久久呦 | 中文字幕一区二区三区在线观看 | 国产激情在线观看 | 黄色国产在线视频 | 中文字幕精品一区二区三区精品 |