2017-01-04 15 views
1

主なアイデアは - スライダの値が大きくなると、より多くの行がコンポーネントの等しい部分を分割し、ポリゴンの線を横切らない(画像の左上隅のように)。私はすべてのコーナーでこれをやりたいと思っていますが、今はそのうちの1人だけとしました。DrawLine in Java Swing

誰かが私の行を幅の1/3にするために変更する必要があることを教えてもらえますか?

私の値は1/3ではなく、1/3ではなく、nはスライダの変数です。

enter image description here

マイコード:

import java.awt.*; 
import javax.swing.*; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class Mariusz extends JFrame { 

    private int n = 5; 
    private Color kolor = Color.RED; 

    MyComponent komponent = null; 

    private class MyComponent extends JComponent 
    { 
     protected void paintComponent (Graphics grafika) 
     { 
      grafika.setColor(kolor); 
      grafika.drawLine(getWidth() * 1/3, 0, 0, getHeight() * 1/3); 
      grafika.drawLine(0, getHeight() * 1/3, getWidth() * 1/3, getHeight()); 
      grafika.drawLine(getWidth() * 1/3, getHeight(),getWidth(), getHeight() * 1/3); 
      grafika.drawLine(getWidth(), getHeight() * 1/3, getWidth() * 1/3, 0); 

      for (int i = 0; i < n ; i++) 
      { 
       if (i <= n/3) 
       { 
        grafika.drawLine(getWidth() * i /n, 0, getWidth() * i /n, (getHeight() - getHeight() * 2/3) - getHeight() * i/n); //lewy gorny 
        grafika.drawLine( getWidth() * i/n,(getHeight() - getHeight() * 2/3) + getHeight() * i/n + getHeight() *1/3, getWidth() * i/n, getHeight()); 
       } 
       if (i > n/3) 
       { 
        grafika.drawLine(getWidth() * i/n , 0, getWidth() * i /n, getHeight() * 2 * i /n/3 - getHeight() * 1 /3 ); 
       } 
      } 
     } 
    } 

    public Mariusz(String string) 
    { 
     super(string); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     Toolkit kit = Toolkit.getDefaultToolkit(); 
     Dimension d = kit.getScreenSize(); 
     setBounds(d.width/4, d.height/4, d.width/2, d.height/2); 

     add (komponent = new MyComponent()); 

     JPanel panel = new JPanel(new BorderLayout()); 
     add(panel,BorderLayout.SOUTH); 

     final JSlider slider = new JSlider(3,40,n); 
     slider.addChangeListener(new ChangeListener() { 

      @Override 
      public void stateChanged(ChangeEvent e) { 
       // TODO Auto-generated method stub 
       n = slider.getValue(); 
       komponent.repaint(); 

      } 
     }); 
     panel.add(slider); 

     setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     // TODO Auto-generated method stub 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new Mariusz("triangles"); 
      } 
     }); 

    } 
} 
+3

に分割してください[MCVE] – Frakcool

+0

説明有効な、より良いものを作品とものではありませんし、どのようにあなたはそれが仕事をしたいです。 2つの文では不十分です。 – user1803551

+0

[example](http://stackoverflow.com/a/39812618/230513)の 'setStroke()'を探していますか? – trashgod

答えて

0

あなたのアプローチは、アドホックであり、私はそれが他の措置(4/5など)にsuceedだろうということに傾けます。 H1 = H/3

従って

h1/h2=(2*w/3)/(2*w/3-(1/n)*(2*w/3)) 

h2=h1/(.....) 
を:

右上から開始し、後方の作業:相続人のアプローチは同様の三角形に基づいてthatsの

我々は同じように動作しますが、要因F2でコード左上のために

double f=2./3, f2=1-f; 

    g2.setColor(Color.blue); 

    for (int i=n-1; i>-1; i--) 
    { 
     grafika.drawLine(getWidth() * (n-i)/n, 0, getWidth() * (n-i)/n, 
     (int)((getHeight()/ 3)/(f/(f-1.*i/n)))); 
    } 

にこれを変換する:

g2.setColor(Color.green); 

    for (int i=0; i<n; i++) 
    { 
     grafika.drawLine(getWidth() * i/n , 0, getWidth() * i /n, (int)((getHeight()/ 3)/(f2/(f2-1.*i/n)))); 
    } 

同じ左下のために:

g2.setColor(Color.magenta); 

    for (int i=0; i<n; i++) 
    { 
     grafika.drawLine(getWidth() * i/n , getHeight()-(int)((2*getHeight()/ 3)/(f2/(f2-1.*i/n))), getWidth() * i /n, getHeight()); 
    } 

右下

g2.setColor(Color.black); 

    for (int i=n-1; i>-1; i--) 
    { 
     grafika.drawLine(getWidth() * (n-i)/n , getHeight()-(int)((2*getHeight()/ 3)/(f/(f-1.*i/n))), getWidth() * (n-i) /n, getHeight() ); 
    } 

たとえば、4/5小節が必要な場合は、f = 4./5と2 * getHeight()/ 3から4 * getHeight()/ 5(getHeight()/ 3)をすべて変更する必要があります。 getHeight()/ 5など)。

+0

式 '(2 * w/3)/(2 * w/3-(1/n)*(2 * w/3))'はちょうど '1 /(1-n)'です。 – user1803551

0

私は別のアプローチを提案しましょう。ポリゴンを満たすまで、線を線から線で描く数学に苦労する代わりに、規則的な、完全な、縦の線を描き、その上にポリゴンをオーバーレイします。

まず、設定するための3つの事:

protected void paintComponent(Graphics graphics) { 

    super.paintComponent(graphics); 

    Graphics2D grafika = (Graphics2D) graphics; 
  1. は常に(あなたは何をやっている知っている限り)アーチファクトを回避するためにpaintComponentにあなたが最初のものとしてsuperメソッドを呼び出します。
  2. GraphicsオブジェクトをGraphics2Dより高度なオブジェクトにキャストすることはいつでも安全です。
  3. ローカル変数に幅と高さを保存して、paintComponent()内のコール数を減らしました。今

、この非常に単純なループを使用して線を描画:

あなたのループだけの減少である
for (int i = 0; i < n; i++) 
    grafika.drawLine(w * i/n, 0, w * i/n, h); 

。必要に応じてそれを変更する必要があるかもしれませんが、y座標は完全なコンポーネントの高さを引き伸ばすことに注意してください。

enter image description here

次にあなたがdrawLine方法で持っているポイントからPolygonを作成します。

Polygon poly = new Polygon(); 
poly.addPoint(w * 1/3, 0); 
poly.addPoint(0, h * 1/3); 
poly.addPoint(w * 1/3, h); 
poly.addPoint(w, h * 1/3); 

最後に、それを描き、この形状を埋めるために色を設定し、形状のアウトラインの色を設定しますそして再びそれを描く:

grafika.setColor(getBackground()); 
grafika.fill(poly); // draws the inside 
grafika.setColor(kolor); 
grafika.draw(poly); // draws the outline 

enter image description here

うまくいけば、これは不正行為とみなされていない

..

また、このライン

add(komponent = new MyComponent()); 

は、私の意見でトラブルのソースのように見えます。より良いヘルプが早く投稿してください2.

+0

そのような解決方法を示してくれてありがとう、それは私にとっても役に立ちます。 –

+0

@Kuba_or_JJ答えが役に立ったら、それをアップヴォートすることができます。 – user1803551

+0

私はそれをしましたが、私の評判は15以下であり、私の投票は見えません。 –