2013-05-09 45 views
-1

如何繪製獨特的矩形(立方體)的橢圓形內:使用JComponent的繪製獨特的矩形

http://i.stack.imgur.com/pmHTl.jpg

enter image description here

對我來說是棘手的用圖形來繪製一個矩形等如所示。任何建議在做什麼。

好的。我會盡力讓自己儘可能清楚。到目前爲止,我所擁有的是橙色的橢圓形和它背後的苗條灰色橢圓形。我只需要在課堂上創建這些「點」中的一個,我會製作它們的許多對象。我需要幫助的任務是繪製您看到的「矩形」形狀,該形狀可能使用J Component在橙色點中。根據要求,我會添加一張我目前所擁有的圖片,如果此編輯不符合您理解我的問題的需要。

感謝

編輯:這是我創造的橢圓形的代碼,如果是感興趣的你 -

public void paint(Graphics g) { 
    Color c = (Color.orange); 
    g.setColor(Color.gray); 
    g.fillOval(3,3,60,60); 
    g.setColor(c); 
    g.fillOval(0,0,60,60); 
    } 

編輯:我在嘗試SSCCE - > 毫微級(如果我創造油漆機器人)

/** 
* @author (Omar Ahmed) 
*/ 
import javax.swing.*; 
import java.awt.*; 
public class NanoBot extends Image 
{ 


public NanoBot(int x, int y, int w, int h) 
{ 
    super(x,y,w,h); 
} 

public void paint(Graphics g) { 
    Color c = (Color.orange); 
    g.setColor(Color.gray); 
    g.fillOval(3,3,60,60); 
    g.setColor(c); 
    g.fillOval(0,0,60,60); 
    //g.setColor(Color.black); 
    //g.fillOval(10,20,10,10); 
    //g.fillOval(40,20,10,10); 

} 
} 

和司機:

/** Bot Swarm 
* Date: May, 2013 
* Author: Omar Ahmed 
*/ 
import java.awt.*; 
import javax.swing.*; 
public class Driver { 
    private JFrame win; 
    private NanoBot bot1; 
     public Driver() { 
     win = new JFrame(" Swarm "); 
     win.setLayout(null); 
     win.setVisible(true); 
     win.setBounds(20, 20, 800, 700); 
     win.getContentPane().setBackground(Color.white); 
     bot1=new NanoBot(50,50,70,70); 
     win.add(bot1,0); 

} 

Hope This Helps 
+0

1)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/ )。 2)你描述了一個問題,以及你如何做不到,但至今沒有提出問題(更不用說具體的可回答的問題)。你的問題是什麼? – 2013-05-09 03:24:29

+2

你可以嘗試使用'AffineTransform'來剪切形狀,但對我個人而言,我只是簡單地創建一個自定義形狀來爲我做... ...先看看[Drawing Arbitrary Shapes](http:/ /docs.oracle.com/javase/tutorial/2d/geometry/arbitrary.html),你甚至可以看看[this](http://stackoverflow.com/questions/16434273/drawing-sierpinskis-triangle-in -java/16434439#16434439)它使用了一對'Path2D'來生成一系列的三角形... – MadProgrammer 2013-05-09 03:25:19

+0

我很困惑。我只是問如何在點內繪製圖像。我是否需要放大以便知道我在問什麼? – 2013-05-09 03:26:02

回答

2

第一步是要打破你的要求......

enter image description here

您需要繪製3層的形狀,前部,頂部,側面。

正面的y位置偏移了總高度的0.412。它的寬度是整個寬度的0.77。

頂部的高度是整體高度的0.412,並且其具有的總寬度爲0.2的水平插圖...

側的x位置是由總寬度的0.77偏移和具有一個嵌總寬度的0.47。

這都是非常重要的,因爲你要確保形狀可以調整得相當好...

現在,你可以簡單地使用Graphics#drawLineGraphics#drawRectangle打造的造型,但是,對我來說,大量的工作...

相反,2D Graphics是非常強大的,包含許多美好的事物,今天有趣的是Shape API,它允許你定義許多不同的形狀,可以繪製或填充。

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Shape; 
import java.awt.geom.Path2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestChip { 

    public static void main(String[] args) { 
     new TestChip(); 
    } 

    public TestChip() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
     } 

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

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 

      int width = 44; 
      int height = 17; 
      Front front = new Front(width, height); 
      Top top = new Top(width, height); 
      Side side = new Side(width, height); 

      draw(g2d, front, Color.BLACK, Color.YELLOW); 
      draw(g2d, top, Color.BLACK, Color.GRAY); 
      draw(g2d, side, Color.BLACK, Color.DARK_GRAY); 

      g2d.dispose(); 
     } 

     protected void draw(Graphics2D g2d, Shape shape, Color foreground, Color background) { 
      g2d.setColor(background); 
      g2d.fill(shape); 
      g2d.setColor(foreground); 
      g2d.draw(shape); 
     } 
    } 

    public class Front extends Path2D.Float { 

     public Front(float width, float height) { 
      float frontWidth = width * 0.77f; 
      float yOffset = height * 0.412f; 

      moveTo(0, yOffset); 
      lineTo(frontWidth, yOffset); 
      lineTo(frontWidth, height); 
      lineTo(0, height); 

      closePath(); 
     } 
    } 

    public class Side extends Path2D.Float { 

     public Side(float width, float height) { 
      float xOffset = width * 0.77f; 
      float inset = height * 0.47f; 
      moveTo(xOffset, inset); 
      lineTo(width, 0); 
      lineTo(width, inset); 
      lineTo(xOffset, height); 
      closePath(); 
     } 
    } 

    public class Top extends Path2D.Float { 

     public Top(float width, float height) { 
      float inset = width * 0.2f; 
      float shapeHeight = height * 0.412f; 
      moveTo(inset, 0); 
      lineTo(width, 0); 
      lineTo(width - inset, shapeHeight); 
      lineTo(0, shapeHeight); 
      closePath(); 
     } 
    } 
} 

你的任務是到現在走了,學習的例子,學習參考的教程,學習相關的API文檔,並找出如何在你圈對準上述形狀和吸取它的腿...

提示。製作一個「Bug」類,知道如何渲染所有這些,並根據需要簡單地翻譯Graphics的位置...