2012-02-21 8 views
6

में डेटा और पाठ के लिए निर्देशांक तल घूर्णन मैं की जरूरत है:
1.) मूल कदम और इतना है कि एक्स-मूल्यों दाहिनी ओर और y- मानों प्रगति भी निर्देशांक तल बारी बारी से नई मूल से ऊपर की ओर प्रगति (जो की जरूरत है नीचे दिए गए कोड में आंतरिक, नीले आयताकार के निचले बाएं कोने के लिए)। यह मुझे नीचे दिए गए कोड में x, y समन्वय जोड़े पर अंक प्लॉट करने में सक्षम करेगा।
2.) साजिश डेटा प्लॉट के वाई-अक्ष पर टिक अंकों के लिए लेबल घुमाएगी।जावा

नीचे दिया गया कोड इस समस्या को सेट करता है। यह दो समस्याओं को छोड़कर काम करता है:
1.) डेटा बिंदु ऊपरी बाएं कोने के साथ प्लॉट किए जा रहे हैं क्योंकि उत्पत्ति और वाई-मान नीचे
2.) वाई-अक्ष पर टिक चिह्नों के लिए लेबल स्क्रीन पर नहीं खींचे जा रहे हैं

क्या कोई मुझे दिखा सकता है कि नीचे दिए गए कोड को कैसे ठीक किया जाए ताकि यह इन दो समस्याओं को हल कर सके और उपरोक्त पहला अनुच्छेद क्या बताता है?

कोड निम्न दो जावा फाइलों में है:

DataGUI.java

import java.awt.*; 
import java.util.ArrayList; 
import javax.swing.*; 

class DataGUI extends JFrame{ 
DataGUI() { 
    super("X,Y Plot"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setPreferredSize(new Dimension(800, 400)); 
    this.pack(); 
    this.setSize(new Dimension(800, 600)); 
    this.setLocationRelativeTo(null); 


    setLayout(new GridLayout()); 
    ArrayList<Double> myDiffs = new ArrayList<Double>(); 
      myDiffs.add(25.0); 
      myDiffs.add(9.0); 
      myDiffs.add(7.0); 
      myDiffs.add(16.0); 
      myDiffs.add(15.0); 
      myDiffs.add(6.0); 
      myDiffs.add(2.0); 
      myDiffs.add(8.0); 
      myDiffs.add(2.0); 
      myDiffs.add(27.0); 
      myDiffs.add(14.0); 
      myDiffs.add(12.0); 
      myDiffs.add(19.0); 
      myDiffs.add(10.0); 
      myDiffs.add(11.0); 
      myDiffs.add(8.0); 
      myDiffs.add(19.0); 
      myDiffs.add(2.0); 
      myDiffs.add(16.0); 
      myDiffs.add(5.0); 
      myDiffs.add(18.0); 
      myDiffs.add(23.0); 
      myDiffs.add(9.0); 
      myDiffs.add(4.0); 
      myDiffs.add(8.0); 
      myDiffs.add(9.0); 
      myDiffs.add(3.0); 
      myDiffs.add(3.0); 
      myDiffs.add(9.0); 
      myDiffs.add(13.0); 
      myDiffs.add(17.0); 
      myDiffs.add(7.0); 
      myDiffs.add(0.0); 
      myDiffs.add(2.0); 
      myDiffs.add(3.0); 
      myDiffs.add(33.0); 
      myDiffs.add(23.0); 
      myDiffs.add(26.0); 
      myDiffs.add(12.0); 
      myDiffs.add(12.0); 
      myDiffs.add(19.0); 
      myDiffs.add(14.0); 
      myDiffs.add(9.0); 
      myDiffs.add(26.0); 
      myDiffs.add(24.0); 
      myDiffs.add(13.0); 
      myDiffs.add(19.0); 
      myDiffs.add(2.0); 
      myDiffs.add(7.0); 
      myDiffs.add(28.0); 
      myDiffs.add(15.0); 
      myDiffs.add(2.0); 
      myDiffs.add(5.0); 
      myDiffs.add(17.0); 
      myDiffs.add(2.0); 
      myDiffs.add(16.0); 
      myDiffs.add(19.0); 
      myDiffs.add(2.0); 
      myDiffs.add(31.0); 
    DataPanel myPP = new DataPanel(myDiffs,this.getHeight(),this.getWidth()); 
    this.add(myPP); 
    this.setVisible(true);// Display the panel. 
} 
public static void main(String[] args){ 
    DataGUI myDataGUI = new DataGUI(); 
    myDataGUI.setVisible(true); 
} 
} 

DataPanel.java (नोट: मैं trashgod के सुझावों को शामिल करने के लिए नीचे दिए गए कोड संपादित, लेकिन यह अभी भी करता है काम नहीं।)

import java.awt.*; 
import java.awt.geom.AffineTransform; 
import javax.swing.*; 
import java.text.DecimalFormat; 
import java.text.NumberFormat; 
import java.util.*; 

class DataPanel extends JPanel { 
Insets ins; // holds the panel's insets 
ArrayList<Double> myDiffs; 
double maxDiff = Double.NEGATIVE_INFINITY; 
double minDiff = Double.POSITIVE_INFINITY; 
double maxPlot; 

DataPanel(ArrayList<Double> Diffs, int h, int w){ 
    setOpaque(true);// Ensure that panel is opaque. 
    setPreferredSize(new Dimension(w, h)); 
    setMinimumSize(new Dimension(w, h)); 
    setMaximumSize(new Dimension(w, h)); 
    myDiffs = Diffs; 
    repaint(); 
    this.setVisible(true); 
} 

protected void paintComponent(Graphics g){// Override paintComponent() method. 
    super.paintComponent(g); 
    //get data about plotting environment and about text 
    int height = getHeight(); 
    int width = getWidth(); 
    ins = getInsets(); 
    Graphics2D g2d = (Graphics2D)g; 
    FontMetrics fontMetrics = g2d.getFontMetrics(); 
    String xString = ("x-axis label"); 
    int xStrWidth = fontMetrics.stringWidth(xString); 
    int xStrHeight = fontMetrics.getHeight(); 
    String yString = "y-axis label"; 
    int yStrWidth = fontMetrics.stringWidth(yString); 
    int yStrHeight = fontMetrics.getHeight(); 
    String titleString ="Title of Graphic"; 
    int titleStrWidth = fontMetrics.stringWidth(titleString); 
    int titleStrHeight = fontMetrics.getHeight(); 
    int leftMargin = ins.left; 
    //set parameters for inner rectangle 
    int hPad=10; 
    int vPad = 6; 
    int testLeftStartPlotWindow = ins.left+5+(3*yStrHeight); 
    int testInnerWidth = width-testLeftStartPlotWindow-ins.right-hPad; 
    getMaxMinDiffs(); 
    getMaxPlotVal(); 
    double increment = 5.0; 
    int numTicks = (int)(maxPlot/increment);//will use numTicks for: remainder, leftStartPlotWindow, innerRectangle+labels+tickmarks 
    int remainder = testInnerWidth%numTicks; 
    int leftStartPlotWindow = testLeftStartPlotWindow-remainder; 
    System.out.println("remainder is: "+remainder); 
    int bottomPad = (3*xStrHeight)-vPad; 
    int blueTop = ins.bottom+(vPad/2)+titleStrHeight; 
    int blueHeight = height-bottomPad-blueTop; 
    int blueWidth = blueHeight; 
    int blueBottom = blueHeight+blueTop; 

    //plot outer rectangle 
    g.setColor(Color.red); 
    int redWidth = width-leftMargin-1; 
    g.drawRect(leftMargin, ins.bottom, redWidth, height-ins.bottom-1); 
    //write top label 
    g.setColor(Color.black); 
    g.drawString(titleString, leftStartPlotWindow+((blueWidth/2)-(titleStrWidth/2)), titleStrHeight); 
    // fill, then plot, inner rectangle 
    g.setColor(Color.white); 
    g.fillRect(leftStartPlotWindow, blueTop, blueWidth, blueHeight); 
    g.setColor(Color.blue); 
    g.drawRect(leftStartPlotWindow, blueTop, blueWidth, blueHeight); 
    //scale the diffs to fit window 
    double Scalar = blueWidth/maxPlot; 
    ArrayList<Double> scaledDiffs = new ArrayList<Double>(); 
    for(int e = 0;e<myDiffs.size();e++){scaledDiffs.add(myDiffs.get(e)*Scalar);} 
    //plot the scaled Diffs 
    AffineTransform at = g2d.getTransform();//save the graphics context's transform 
    g2d.translate(leftStartPlotWindow, blueTop);//translate origin to bottom-left corner of blue rectangle 
    g2d.scale(1, -1);//invert the y-axis 
    for(int w = 0;w<scaledDiffs.size();w++){ 
     if(w>0){ 
      double prior = scaledDiffs.get(w-1); 
      int priorInt = (int)prior; 
      double current = scaledDiffs.get(w); 
      int currentInt = (int)current; 
      g2d.drawOval(priorInt, currentInt, 4, 4); 
     } 
    } 
    g2d.setTransform(at);//restore the transform for conventional rendering 
    //write x-axis label 
    g.setColor(Color.red); 
    g.drawString(xString, leftStartPlotWindow+((blueWidth/2)-(xStrWidth/2)), height-ins.bottom-vPad); 
    //write y-axis label 
    g2d.rotate(Math.toRadians(-90), 0, 0);//rotate text 90 degrees counter-clockwise 
    g.drawString(yString, -(height/2)-(yStrWidth/2), yStrHeight); 
    g2d.rotate(Math.toRadians(+90), 0, 0);//rotate text 90 degrees clockwise 
    // draw tick marks on x-axis 
    NumberFormat formatter = new DecimalFormat("#0.0"); 
    double k = (double)blueWidth/(double)numTicks; 
    double iteration = 0; 
    for(int h=0;h<=numTicks;h++){ 
     int xval = (int)(h*k); 
     g.setColor(Color.red); 
     g.drawLine(leftStartPlotWindow+xval, blueBottom+2, leftStartPlotWindow+xval, blueBottom+(xStrHeight/2));//draw tick marks 
     g.drawString(formatter.format(iteration),leftStartPlotWindow+xval-(fontMetrics.stringWidth(Double.toString(iteration))/2),blueBottom+(xStrHeight/2)+13); 
     iteration+=increment; 
    } 
    // draw tick marks on y-axis 
    iteration = 0; 
    for(int h=0;h<=numTicks;h++){ 
     int yval = (int)(h*k); 
     g.setColor(Color.red); 
     g.drawLine(leftStartPlotWindow-2, blueBottom-yval, leftStartPlotWindow-(yStrHeight/2), blueBottom-yval);//draw tick marks 
     g2d.rotate(Math.toRadians(-90), 0, 0);//rotate text 90 degrees counter-clockwise 
     g.drawString(formatter.format(iteration),leftStartPlotWindow-2,blueBottom-(fontMetrics.stringWidth(Double.toString(iteration))/2)); 
     g2d.rotate(Math.toRadians(+90), 0, 0);//rotate text 90 degrees clockwise 
     iteration+=increment; 
    } 
} 
void getMaxMinDiffs(){// get max and min of Diffs 
    for(int u = 0;u<myDiffs.size();u++){ 
     if(myDiffs.get(u)>maxDiff){maxDiff = myDiffs.get(u);} 
     if(myDiffs.get(u)<minDiff){minDiff = myDiffs.get(u);} 
    } 
} 
void getMaxPlotVal(){ 
    maxPlot = maxDiff; 
    maxPlot += 1;//make sure maxPlot is bigger than the max data value 
    while(maxPlot%5!=0){maxPlot+=1;}//make sure maxPlot is a multiple of 5 
} 
} 

इसके अलावा, हमेशा की तरह, लेख के लिए लिंक या विषय पर ट्यूटोरियल बहुत सराहना की है।

+0

हाथ में समस्या से संबंधित नहीं है, लेकिन डेटा ArrayList को जोड़ा गया एक डेटा फ़ाइल में आयोजित किया जाना चाहिए, नहीं कार्यक्रम में हार्ड-कोडेड। –

+2

@ होवरक्राफ्ट पूर्ण ईल्स, हाँ, मुझे पता है। वास्तव में, डेटा कुछ सरणी संचालन का परिणाम है और उपरोक्त डेटासेट से काफी लंबा है। हालांकि, मैंने इसे जिस तरीके से ऊपर किया था, उसे लोड किया ताकि यह इस साइट पर लोगों के लिए अधिक आसानी से पुन: उत्पन्न हो सके। – CodeMed

+1

'सूची myDiffs = new ArrayList (Arrays.asList (25.0, 9.0, 7.0, ...)) पर विचार करें। – trashgod

उत्तर

2

कुछ अपूर्ण उत्तर के लिए क्षमा चाहते हैं, लेकिन यह आपके गियर को बदल सकता है। जावा चीजों को आपके द्वारा वर्णित तरीके से खींचता है: यह स्क्रीन के ऊपरी बाएं कोने को 0, 0 मानता है और एक्स को दाईं ओर बढ़ता है और y नीचे की ओर बढ़ता है। आप

g2d.drawOval(blueWidth - priorInt, blueHeight - currentInt, 4, 4); 

में लाइन है कि कहा गया है

g2d.drawOval(priorInt, currentInt, 4, 4); 

करते हैं तो यह आपकी पहली जारी करने के लिए सही परिणाम चाहिए। हालांकि, उस समस्या के लिए आपको दूसरी समस्या पर थोड़ी अधिक जानकारी चाहिए। क्या वे सिर्फ स्क्रीन से बाहर हैं या किसी और चीज से खींचे जा रहे हैं? अगर यह मामला है तो आप सही परिणाम प्राप्त कर सकते हैं या नहीं, यह देखने के लिए चारों ओर फिसलने की कोशिश करें।

+0

सुझाव के लिए धन्यवाद। मैंने परिवर्तन किया और कोड चलाने की कोशिश की, लेकिन यह नीले आयताकार के बाहर एक प्लॉट करता है, जो दिखाता है कि यह डेटा बिंदुओं को सही ढंग से फिर से ढूंढ नहीं रहा है। आप अपने आप को देख सकते हैं कि, यदि आप अपने परिवर्तन के साथ कोड चलाते हैं, तो नीले आयत के बाएं किनारे के बाईं ओर एक डेटा पॉइंट होता है, जो स्क्रीन के लगभग 70% तरीके से होता है। इसका मतलब है कि हर बिंदु को गलत तरीके से प्लॉट किया जा रहा है, भले ही वे नीले रंग के बक्से के अंदर हों। टिक मार्क लेबल्स के लिए, उन्हें एक्स-अक्ष लेबल के समान ही रखा जाना चाहिए। मदद करने की कोशिश करने के लिए +1। – CodeMed

+0

g2d.drawOval (preInt, -currentInt, 4, 4) का उपयोग करने का प्रयास करें; मैं अंधा चल रहा हूं क्योंकि मैं उस कोड को नहीं चला रहा हूं जिसका मैं सुझाव दे रहा हूं। इसके बारे में खेद है, लेकिन मैं एक जेपीनल रैपर = पी लिखने के लिए आलसी हूँ। –

+0

एकमात्र चीज जो मैं आपके वाई अक्ष लेबल ड्राइंग कोड के साथ गलत देख सकता हूं वह यह है कि आप बस अपने लाल बॉक्स के अंदर आरेखण कर सकते हैं। 'G2d.setColor (Color.red)' 'g2d.setColor (Color.green) ' –

10

एक दृष्टिकोण SineTest में दिखाया गया है। रूपरेखा में,

  1. सहेजें ग्राफिक्स संदर्भ के बदलने।

    Graphics2D g2d = (Graphics2D) g; 
    AffineTransform at = g2d.getTransform(); 
    
  2. उत्पत्ति को केंद्र में अनुवाद करें।

    g2d.translate(w/2, h/2); 
    
  3. उलटें y धुरी।

    g2d.scale(1, -1); 
    
  4. कार्टेशियन निर्देशांक का उपयोग करके प्रस्तुत करें।

  5. पारंपरिक प्रतिपादन के लिए परिवर्तन को पुनर्स्थापित करें।

    g2d.setTransform(at); 
    

enter image description here

+0

+1 को बदलने का प्रयास करें। मैं सावधानी से आपके सुझाव के माध्यम से जाना होगा। कृपया समझें कि मैं अनुवाद, उलटा, एफ़िन ट्रांसफॉर्म, और थोड़ी देर के लिए जरूरी की तलाश कर रहा हूं, और वे हमेशा मुझे भ्रमित कर रहे हैं। जब मैं इसे एक आवेदन के लिए समझता हूं, तो मुझे लगता है कि कुछ हफ्ते बाद मैं उलझन में हूं जब अगले आवेदन की आवश्यकता उत्पन्न होती है। क्या आप उपरोक्त मेरी DataPanel.java फ़ाइल में कार्य कोड में अपने सुझावों को शामिल करने के इच्छुक हैं? यदि आप ऐसा करने के इच्छुक थे, तो मैं जावा एपीआई के साथ अपने कोड की सावधानीपूर्वक समीक्षा कर पाऊंगा और बेहतर ढंग से समझ सकता हूं कि यह कैसे काम करता है। – CodeMed

+0

उद्धृत उदाहरण पूर्ण है, और यह ग्राफिक्स ट्रांसफॉर्म-_order counts_ की एक आवश्यक विशेषता को दर्शाता है। आपकी रेंज के निशान गायब हो जाते हैं क्योंकि उन्हें देखने से बाहर घुमाया गया है। अधिक [यहां] (http://stackoverflow.com/q/3843105/230513)। – trashgod

+0

मैंने आपके कोड और आपके द्वारा उद्धृत संदर्भों की समीक्षा की। मैंने आपके सुझाए गए परिवर्तन भी किए हैं, लेकिन अब यह सभी बिंदुओं में से एक को साजिश नहीं दे रहा है। मैंने आपके मूल पोस्टिंग में संशोधित कोड दिखाया है, जिसमें आपके सुझाव शामिल हैं, ताकि वर्तमान में शेष समस्या को फिर से बनाया जा सके। क्या आप मुझे दिखाने के लिए तैयार हैं कि ऊपर दिए गए मेरे संशोधित कोड को कैसे ठीक किया जाए ताकि यह मेरी मूल पोस्टिंग कहता है कि मैं ऐसा करने की कोशिश कर रहा हूं? धन्यवाद। – CodeMed