tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
0
ZUGRIFFE
175
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    forsti222 forsti222 ist offline Mitglied Bronze
    Registriert seit
    Aug 2007
    Beiträge
    44
    Grüß euch
    Brauch wieder mal eure Hilfe, muss für die Uni eine Rekursionsaufgabe machen. Im Anhang befindet sich ein Bild wie das ganze aussehen soll
    habe es nun probiert und bitte da schonmal um Verbesserungen! Funktioniert auch sehr gut, aber mein Problem ist, dass ich das ganze ohne doppelte Linien zeichnen soll... Also immer nur alles einmal und verstehe nicht ganz wie das gehen soll?
    Wäre schon dankbar falls wer CodeVerbesserungen hat (falls was geht)
    danke euch!!

    Meine Klasse die ich bearbeitet habe:
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    
    package spinningSquare;
     
    class SpinningSquare {
     
        public static void main(String [] args){
            
            System.out.println("Rekursionstiefe: ");
            int recursion = Input.readInt();
            int length = 0;
            while (length <= 0){
                System.out.println("Basis-Laenge des 1. Dreiecks: ");
                length = Input.readInt();
            }
            int angle = 0;
            while (angle <= 0 || angle >=90){
                System.out.println("Winkel: ");
                angle = Input.readInt();
            }   
            
            draw(recursion, length, angle);
        }
        
        private static void draw(int recursion, double length, double angle){
            
                System.out.println("Spinning Square started");
                //start-position
                Turtle.setPos(150, 20);
                //Turtle.right(angle);
                
                recursion(recursion,length,angle);
                
                Turtle.showGraphics("Spinning Square");
                
                System.out.println("Spinning Square ended");
        }
        
        private static void recursion(int recursion,double length,double angle) {
                double angle_rad = angle * Math.PI / 180;
                double x = Math.tan(angle_rad)*length/(1+Math.tan(angle_rad));
                Turtle.forward(length);
                Turtle.left(90);
                for(int i =0;i<3;i++) {
                    Turtle.forward(length);
                    Turtle.left(90);
            }
                Turtle.forward(x);
                Turtle.left(angle);
                if(recursion>0) {
                    double new_length=Math.sqrt(Math.pow(x, 2)+Math.pow(length-x,2));
                    recursion(recursion-1,new_length,angle);
                }
        }
        
    }
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    
    package spinningSquare;
     
    import java.awt.Color;
     
    /**
     * @author Volker Christian (volker.christian@fh-hagenberg.at)
     * @version 1.0
     */
     
    public class Turtle {
        private static int[][][] picture = new int[500][800][3];
        static {
            erase();
            
        }
        
        private static double x = 0;
        private static double y = 0;
        private static double angle = 0;
        private static int stepWidth = 1;
        private static Color color = Color.BLACK;
        private static boolean isDrawing = true;
        
        protected static void putPixel(double xx, double yy) {
            
            int x = (int) Math.round(xx);
            int y = (int) Math.round(yy);
            
            int yMax = picture.length;
            
            if (yMax - y - 1 < 0 || 
                    yMax - y - 1 >= picture.length ||
                    x < 0 ||
                    x >= picture[0].length ||
                    !isDrawing) {
                return;
            }
            
            picture[yMax - y - 1][x][0] = color.getRed();
            picture[yMax - y - 1][x][1] = color.getGreen();
            picture[yMax - y - 1][x][2] = color.getBlue();
        }
        
        protected static void drawLine(int x1, int y1, int x2, int y2) {
            int x = x1, y = y1, d = 0, hx = x2 - x1, hy = y2 - y1;
            int xInc = 1;
            int yInc = 1;
            
            if (hx < 0) {
                xInc = -1;
                hx = -hx;
            }
            if (hy < 0) {
                yInc = -1;
                hy = -hy;
            }
            if (hy <= hx) {
                int c = 2 * hx;
                int k = 2 * hy;
                
                while (true) {
                    putPixel(x, y);
                    if (x == x2) {
                        break;
                    }
                    x += xInc;
                    d += k;
                    if (d > hx) {
                        y += yInc;
                        d -= c;
                    }
                }
            } else {
                int c = 2 * hy;
                int k = 2 * hx;
                
                while (true) {
                    putPixel(x, y);
                    if (y == y2) {
                        break;
                    }
                    y += yInc;
                    d += k;
                    if (d > hy) {
                        x += xInc;
                        d -= c;
                    }
                }
            }
        }
        
            
        protected static void drawPLine(double x1, double y1, double x2, double y2) {
            System.out.println("draw ("+x1+"/"+y1+") to ("+x2+"/"+y2+")");
            double x = x1, y = y1, d = 0;
            double hx = x2 - x1, hy = y2 - y1;
            double xInc = 1;
            double yInc = 1;
            
            if (hx < 0) {
                xInc = -1;
                hx = -hx;
            }
            if (hy < 0) {
                yInc = -1;
                hy = -hy;
            }
            
            
            if (hy <= hx) {
                double c = 2 * hx;
                double k = 2 * hy;
                
                while (true) {
                    putPixel(x, y);
                    if ((xInc > 0 && (x > x2)) || ((xInc <= 0) && x <= x2)) {
                        break;
                    }
                    x += xInc;
                    d += k;
                    if (d > hx) {
                        y += yInc;
                        d -= c;
                    }
                }
            } else {
                double c = 2 * hy;
                double k = 2 * hx;
                
                while (true) {
                    putPixel(x, y);
                    if ((yInc > 0 && (y > y2)) || ((yInc <= 0) &&  y<= 2)) {
                        break;
                    }
                    y += yInc;
                    d += k;
                    if (d > hy) {
                        x += xInc;
                        d -= c;
                    }
                }
            }
        }
        
        public static void forward(double n) {
            double xn = x + n * stepWidth * ((Math.cos((2.0 * Math.PI * angle) / 360.0))%360);
            double yn = y + n * stepWidth * ((Math.sin((2.0 * Math.PI * angle) / 360.0)%360));
            
            drawLine(
                    (int) Math.round(x), 
                    (int) Math.round(y), 
                    (int) Math.round(xn), 
                    (int) Math.round(yn));
                    
            //drawPLine(x, y, xn, yn);
                            
            x = xn;
            y = yn;
        
            Presenter.updateMovie(picture);
        }
        
        
        public static void setPos(int x, int y) {
            Turtle.x = x;
            Turtle.y = y;
        }
            
        protected static void setAngle(double angle) {
            Turtle.angle = angle;
        }
        
        
        protected static void stepWidth(int width) {
            Turtle.stepWidth = width;
        }
        
        
        public static void left(double angel) {
            //IO.writeLn("angle=" + angle);
            Turtle.angle = (Turtle.angle + angel)%360.0;
        }
        
        
        public static void right(double angel) {
            Turtle.angle = (Turtle.angle - angel)%360.0;
        }
        
        
        public static void setColor(Color color) {
            Turtle.color = color;
        }
        
        
        public static void showGraphics(String title) {
            Presenter.viewImage(title, picture);
        }
        
        protected static void showMovie(String title) {
            Presenter.viewMovie(title, picture);
        }
        
        
        protected static void penUp() {
            Turtle.isDrawing = false;
        }
        
        
        protected static void penDown() {
            Turtle.isDrawing = true;
        }
        
        protected static void erase() {
            for (int y = 0; y < picture.length; y++) {
                for (int x = 0; x < picture[0].length; x++) {
                    picture[y][x][0] = 255;
                    picture[y][x][1] = 255;
                    picture[y][x][2] = 255;
                }
            }
            
        }
    }
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    
    package spinningSquare;
     
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Frame;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.MemoryImageSource;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Vector;
     
     
    /**
     * This class provides some static methods to view different styles of content.
     * Every call to a method creates a frame viewing a simple representation of the
     * provided content.
     * 
     * @author Volker Christian (volker.christian@fh-hagenberg.at)
     * @version 1.0
     */
    public class Presenter {
        static private class HistogramFrame extends Frame {
            private static final long serialVersionUID = -1297885049512116682L;
     
            private Vector<Integer> histogramLengths = new Vector<Integer>();
     
            private Vector<int[]> histograms = new Vector<int[]>();
     
            private Vector<Color> histogramColors = new Vector<Color>();
     
            private int maxHeight = Integer.MIN_VALUE;
     
            private int maxWidth = Integer.MIN_VALUE;
     
            private Color backgroundColor = Color.WHITE;
     
            private HistogramCanvas histogramCanvas = null;
     
            private class HistogramFrameWindowAdapter extends WindowAdapter {
                private HistogramFrame histogramFrame = null;
     
                HistogramFrameWindowAdapter(HistogramFrame hf) {
                    this.histogramFrame = hf;
                }
     
                public void windowClosing(WindowEvent e) {
                    histogramFrame.closeFrame();
                }
            }
     
            private class HistogramCanvas extends Canvas {
                private static final long serialVersionUID = 5573564144397035012L;
     
                private Image offScreenImage = null;
     
                private Graphics offScreenGraphics = null;
     
                private int oldWidth = Integer.MIN_VALUE;
     
                private int oldHeight = Integer.MIN_VALUE;
     
                public void paintHistogram(Graphics g) {
                    Dimension d = this.getSize();
                    int textHeight = g.getFontMetrics().getHeight();
                    int textWidth = 10;
                    int histogrammXOffset = textWidth;
                    int histogrammYOffset = 0;
                    int histogrammWidth = d.width - textWidth;
                    int histogrammHeight = d.height - textHeight;
                    g.setColor(backgroundColor);
                    g.fillRect(0, 0, histogrammWidth, histogrammHeight);
     
                    double factorY = (histogrammHeight) / (double) maxHeight;
                    double factorX = (histogrammWidth) / (double) (maxWidth);
     
                    for (int h = 0; h < histograms.size(); h++) {
                        int red = histogramColors.get(h).getRed();
                        int green = histogramColors.get(h).getGreen();
                        int blue = histogramColors.get(h).getBlue();
                        Color lineColor = new Color(red, green, blue);
                        int[] histogram = histograms.elementAt(h);
                        int yO = histogrammYOffset;
                        int xO = histogrammXOffset;
                        for (int i = 0; i < histogramLengths.get(h).intValue(); i++) {
                            int xN = histogrammXOffset + (int) ((i + 1) * factorX);
                            int yN = histogrammYOffset + histogrammHeight
                                    - (int) (((double) histogram[i]) * factorY);
     
                            g.setColor(lineColor);
                            g.drawLine(xO, yO, xO, yN);
                            g.drawLine(xO, yN, xN, yN);
     
                            g.setColor((Color) histogramColors.get(h));
                            int[] xPoints = { xO, xO, xN, xN };
                            int[] yPoints = { histogrammHeight, yN, yN,
                                    histogrammHeight };
                            g.fillPolygon(xPoints, yPoints, 4);
     
                            xO = xN;
                            yO = yN;
                        }
                    }
                    if (histograms.size() > 0) {
                        g.setColor(Color.BLACK);
                        g.drawLine(histogrammXOffset, histogrammHeight,
                                histogrammXOffset + (int) (maxWidth * factorX),
                                histogrammHeight);
                        g.drawLine(histogrammXOffset, histogrammHeight,
                                histogrammXOffset, 0);
                        int fontHeight = g.getFontMetrics().getHeight();
                        int numberOfLabels = 6;
                        for (int i = 0; i <= numberOfLabels; i++) {
                            int dx = (int) (Math
                                    .round(((maxWidth - 1.0) / numberOfLabels) * i)
                                    * factorX + 0.5 * factorX);
                            String s = String.valueOf(Math.round(i
                                    * (maxWidth - 1.0) / numberOfLabels));
                            g.drawString(s, histogrammXOffset
                                    - g.getFontMetrics().stringWidth(s) + dx,
                                    histogrammHeight + fontHeight);
                            g.drawLine(histogrammXOffset + dx, histogrammHeight,
                                    histogrammXOffset + dx, histogrammHeight
                                            + fontHeight);
                        }
                    }
                }
     
                public void paintOffScreenHistogram(Graphics g, boolean forceRepaint) {
                    int width = getSize().width;
                    int height = getSize().height;
                    if (oldWidth != width || oldHeight != height) {
                        oldWidth = width;
                        oldHeight = height;
                        offScreenImage = createImage(width, height);
                        offScreenGraphics = offScreenImage.getGraphics();
                        forceRepaint = true;
                    }
     
                    if (forceRepaint) {
                        paintHistogram(offScreenGraphics);
                    }
     
                    width = getSize().width;
                    height = getSize().height;
                    if (oldWidth == width && oldHeight == height) {
                        g.drawImage(offScreenImage, 0, 0, this);
                    }
                }
     
                public void update(Graphics g) {
                    /*
                     * Called by repaint such an update-call should force a complete
                     * repaint
                     */
                    paintOffScreenHistogram(g, true);
                }
     
                public void paint(Graphics g) {
                    /*
                     * Called by the system * again visible * resize of the frame
                     * such an paint-call should lead to a selective forced repaint.
                     * 
                     * No complete repaint should happen if the frame hasn't changed
                     * size. In this case it is sufficient to map the already filled
                     * offscreen-image to the Graphics g
                     * 
                     * A complete repaint should happen if the frame has changed
                     * size
                     */
                    paintOffScreenHistogram(g, false);
                }
            }
     
            protected HistogramFrame(String title) {
                this.setResizable(false);
                this.setTitle("Histogram - " + title);
                this.addWindowListener(new HistogramFrameWindowAdapter(this));
                this.add(histogramCanvas = new HistogramCanvas());
                histogramCanvas.setPreferredSize(new Dimension(260, 150));
                // histogramCanvas.setPreferredSize(new Dimension(390, 225));
            }
     
            protected void addHistogram(int[] histogram, int length, int max,
                    Color color) {
                histograms.add(histogram);
                this.histogramLengths.add(new Integer(length));
                histogramColors.add(color);
                this.maxHeight = (this.maxHeight < max) ? max : this.maxHeight;
                this.maxWidth = (this.maxWidth < length) ? length : this.maxWidth;
                histogramCanvas.repaint();
            }
     
            private void closeFrame() {
                dispose();
            }
     
            protected void setBackgroundColor(Color c) {
                backgroundColor = c;
            }
        }
     
        /**
         * This method creates a frame and draws a couble of histograms in it.
         * 
         * @param title
         *            the title showing up in the title bar of the frame
         * @param histograms
         *            a two-dimensional array containing a couble of histograms. The
         *            first index selects a specific histogram, the second index
         *            numbers all values of a specific histogram selected by the
         *            first index.
         * @param colors
         *            the colors used for the histograms. The index selects a
         *            specific color for the corresponding specific histogram.
         */
        public static void viewHistogram(String title, int[][] histograms,
                Color[] colors, Color backGroundColor) {
            HistogramFrame histogramFrame = new HistogramFrame(title);
            histogramFrame.setBackground(backGroundColor);
            histogramFrame.setBackgroundColor(backGroundColor);
            histogramFrame.pack();
            histogramFrame.setVisible(true);
     
            for (int h = 0; h < histograms.length; h++) {
                int max = Integer.MIN_VALUE;
                int[] histogram = histograms[h];
                Color color = colors[h];
                for (int i = 0; i < histogram.length; i++) {
                    max = (max < histogram[i]) ? histogram[i] : max;
                }
                histogramFrame
                        .addHistogram(histogram, histogram.length, max, color);
            }
        }
     
        /**
         * This method creates a frame and draws a histogram in it.
         * 
         * @param title
         *            the title showing up in the title bar of the frame
         * @param histogram
         *            an integer array containing the histogram information
         */
        public static void viewHistogram(String title, int[] histogram) {
            viewHistogram(title, histogram, Color.BLACK, Color.WHITE);
        }
     
        /**
         * This method creates a frame and draws a histogram in it.
         * 
         * @param title
         *            the title showing up in the title bar of the frame
         * @param histogram
         *            an integer array containing the histogram information
         * @param foreGroundColor
         *            the color used for the histogram
         */
        public static void viewHistogram(String title, int[] histogram,
                Color foreGroundColor) {
            viewHistogram(title, histogram, foreGroundColor, Color.WHITE);
        }
     
        /**
         * This method creates a frame and draws a histogram in it.
         * 
         * @param title
         *            the title showing up in the title bar of the frame
         * @param histogram
         *            an integer array containing the histogram information
         * @param foreGroundColor
         *            the color used for the histogram
         * @param backGroundColor
         *            the background color of the histogram
         */
        public static void viewHistogram(String title, int[] histogram,
                Color foreGroundColor, Color backGroundColor) {
     
            int[][] histograms = new int[1][];
     
            histograms[0] = histogram;
     
            Color[] colors = new Color[1];
            colors[0] = foreGroundColor;
     
            viewHistogram(title, histograms, colors, backGroundColor);
        }
     
        /**
         * This method creates a frame and draws three histograms in it.
         * 
         * @param title
         *            the title showing up in the title bar of the frame
         * @param histogram1
         *            an integer array containing the first histogram information
         * @param color1
         *            the color used for the first histogram
         * @param histogram2
         *            an integer array containing the second histogram information
         * @param color2
         *            the color used for the second histogram
         * @param histogram3
         *            an integer array containing the third histogram information
         * @param color3
         *            the color used for the third histogram
         */
        public static void viewHistogram(String title, int[] histogram1,
                Color color1, int[] histogram2, Color color2, int[] histogram3,
                Color color3) {
            viewHistogram(title, histogram1, color1, histogram2, color2,
                    histogram3, color3, Color.WHITE);
        }
     
        /**
         * This method creates a frame and draws three histograms in it.
         * 
         * @param title
         *            the title showing up in the title bar of the frame
         * @param histogram1
         *            an integer array containing the first histogram information
         * @param color1
         *            the color used for the first histogram
         * @param histogram2
         *            an integer array containing the second histogram information
         * @param color2
         *            the color used for the second histogram
         * @param histogram3
         *            an integer array containing the third histogram information
         * @param color3
         *            the color used for the third histogram
         * @param backGroundColor
         *            the background color of the histograms
         */
        public static void viewHistogram(String title, int[] histogram1,
                Color color1, int[] histogram2, Color color2, int[] histogram3,
                Color color3, Color backGroundColor) {
     
            int[][] histograms = new int[3][];
            histograms[0] = histogram1;
            histograms[1] = histogram2;
            histograms[2] = histogram3;
     
            Color[] colors = new Color[3];
            colors[0] = color1;
            colors[1] = color2;
            colors[2] = color3;
     
            viewHistogram(title, histograms, colors, backGroundColor);
        }
     
        /* ---------------------- Image methods ------------------------ */
        static private class ImageFrame extends Frame {
            private static final long serialVersionUID = -3961613485615270343L;
     
            private ImageCanvas imageCanvas = null;
     
            private Image image = null;
     
            private class ImageFrameWindowAdapter extends WindowAdapter {
                private ImageFrame imageFrame = null;
     
                ImageFrameWindowAdapter(ImageFrame imageFrame) {
                    this.imageFrame = imageFrame;
                }
     
                public void windowClosing(WindowEvent e) {
                    imageFrame.closeFrame();
                }
            }
     
            private class ImageCanvas extends Canvas {
                private static final long serialVersionUID = 2481133924901847011L;
     
                public void paint(Graphics g) {
                    g.drawImage(image, 0, 0, this);
                }
            }
     
            public ImageFrame(Image image) {
                this.addWindowListener(new ImageFrameWindowAdapter(this));
                this.setLayout(new BorderLayout());
                this.image = image;
                imageCanvas = new ImageCanvas();
                this.add(imageCanvas);
            }
     
            public void setCanvasSize(int width, int height) {
                imageCanvas.setPreferredSize(new Dimension(width, height));
            }
     
            private void closeFrame() {
                dispose();
            }
        }
     
        /**
         * The method viewImage opens a frame and displays a RGB-Image in it.
         * 
         * @param title
         *            the title showing up in the title bar of the frame
         * @param rgbImage
         *            a three-dimensional array containing the RGB-data of the
         *            image. The first index is the y-coordinate into the image, the
         *            second index is the x-coordinate into the image and the third
         *            index selects a color-component: red = 0, green = 1, and blue =
         *            2
         */
        public static void viewImage(String title, int[][][] rgbImage) {
            int[] pixels = new int[rgbImage[0].length * rgbImage.length];
            int index = 0;
     
            for (int y = 0; y < rgbImage.length; y++) {
                for (int x = 0; x < rgbImage[0].length; x++) {
                    int color = 255 << 24;
                    for (int c = 0; c < 3; c++) {
                        color |= rgbImage[y][x][c] << (8 * (2 - c));
                    }
                    pixels[index++] = color;
                }
            }
            Image image = Toolkit.getDefaultToolkit().createImage(
                    new MemoryImageSource(rgbImage[0].length, rgbImage.length,
                            pixels, 0, rgbImage[0].length));
     
            ImageFrame imf = new ImageFrame(image);
            imf.setTitle("Image - " + title);
            /* ---------------------- Image methods ------------------------ */
            imf.setCanvasSize(rgbImage[0].length, rgbImage.length);
            imf.pack();
            imf.setResizable(false);
            imf.setVisible(true);
        }
     
        /**
         * The method viewImage opens a frame and displays a RGB-Image in it.
         * 
         * @param title
         *            the title showing up in the title bar of the frame
         * @param grayScaleImage
         *            a two-dimensional array containing the luminance-data of the
         *            image. The first index is the y-coordinate into the image, the
         *            second index is the x-coordinate into the image.
         */
        public static void viewImage(String title, int[][] grayScaleImage) {
            int[][][] rgbImage = new int[grayScaleImage.length][grayScaleImage[0].length][3];
     
            for (int y = 0; y < grayScaleImage.length; y++) {
                for (int x = 0; x < grayScaleImage[0].length; x++) {
                    rgbImage[y][x] = ImageGrabber.ImageGrabberUtilities
                            .convertYCbCrToRGB(
                                    (double) grayScaleImage[y][x] / 256.0, 0.5, 0.5);
                }
            }
            viewImage(title, rgbImage);
        }
     
        /* ---------------------- Movie methods ------------------------ */
        static class MovieFrame extends Frame {
            private static final long serialVersionUID = 2468836318900893949L;
     
            private MovieCanvas movieCanvas = null;
     
            private Image image = null;
            
            private int[][][] rgbImage = null;
            
            private int[][] grayScaleImage = null;
     
            private class MovieFrameWindowAdapter extends WindowAdapter {
                private MovieFrame movieFrame = null;
     
                MovieFrameWindowAdapter(MovieFrame movieFrame) {
                    this.movieFrame = movieFrame;
                }
     
                public void windowClosing(WindowEvent e) {
                    if (grayScaleImage != null) {
                        movieFrames.remove(grayScaleImage);
                    } else {
                        movieFrames.remove(rgbImage);
                    }
                    movieFrame.closeFrame();
                }
            }
     
            private class MovieCanvas extends Canvas {
                static final long serialVersionUID = -3517264303987741422L;
                
                public void update(Graphics g) {
                    paint(g);
                }
                
                public void paint(Graphics g) {
                    g.drawImage(image, 0, 0, this);
                }
            }
     
            public MovieFrame(String title, int[][][] rgbImage) {
                this.rgbImage = rgbImage;
                initFrame(title);
            }
            
            public MovieFrame(String title, int[][] grayScaleImage) {
                this.grayScaleImage = grayScaleImage;
                rgbImage = new int[grayScaleImage.length][grayScaleImage[0].length][3];
                initFrame(title);
            }
     
            private void initFrame(String title) {
                this.addWindowListener(new MovieFrameWindowAdapter(this));
                this.setLayout(new BorderLayout());
                this.image = createImage();
                movieCanvas = new MovieCanvas();
                this.add(movieCanvas);
                this.setTitle("Movie - " + title);
                this.setCanvasSize(rgbImage[0].length, rgbImage.length);
                this.pack();
                this.setResizable(false);
                this.setVisible(true);
            }
     
            private Image createImage() {
                if (grayScaleImage != null) {
                    for (int y = 0; y < grayScaleImage.length; y++) {
                        for (int x = 0; x < grayScaleImage[0].length; x++) {
                            rgbImage[y][x] = ImageGrabber.ImageGrabberUtilities
                            .convertYCbCrToRGB(
                                    (double) grayScaleImage[y][x] / 256.0, 0.5, 0.5);
                        }
                    }
                }
                
                int[] pixels = new int[rgbImage[0].length * rgbImage.length];
                int index = 0;
     
                for (int y = 0; y < rgbImage.length; y++) {
                    for (int x = 0; x < rgbImage[0].length; x++) {
                        int color = 255 << 24;
                        for (int c = 0; c < 3; c++) {
                            color |= rgbImage[y][x][c] << (8 * (2 - c));
                        }
                        pixels[index++] = color;
                    }
                }
                
                Image image = Toolkit.getDefaultToolkit().createImage(
                        new MemoryImageSource(rgbImage[0].length, rgbImage.length,
                                pixels, 0, rgbImage[0].length));
                
                return image;
            }
     
            public void updateCanvas() {
                this.image = createImage();
                movieCanvas.repaint();
            }
     
            public void setCanvasSize(int width, int height) {
                movieCanvas.setPreferredSize(new Dimension(width, height));
            }
     
            private void closeFrame() {
                dispose();
            }
        }
     
        static Map<Object, MovieFrame> movieFrames = new HashMap<Object, MovieFrame>();
        
        public static MovieFrame viewMovie(String title, int[][][] rgbImage) {
            MovieFrame mf = null;
            if (movieFrames.containsKey(rgbImage)) {
                mf = updateMovie(rgbImage);
            } else {
                mf = new MovieFrame(title, rgbImage);
                movieFrames.put(rgbImage, mf);
            }   
            return mf;
        }
        
        public static MovieFrame viewMovie(String title, int[][] grayScaleImage) {
            MovieFrame mf = null;
            if (movieFrames.containsKey(grayScaleImage)) {
                mf = updateMovie(grayScaleImage);
            } else {
                mf = new MovieFrame(title, grayScaleImage);
                movieFrames.put(grayScaleImage, mf);
            }
            return mf;
        }
        
        public static MovieFrame updateMovie(MovieFrame mf) {
            mf.updateCanvas();
            return mf;
        }
        
        public static MovieFrame updateMovie(int[][][] rgbImage) {
            MovieFrame mf = null;
            if (movieFrames.containsKey(rgbImage)) {
                mf = movieFrames.get(rgbImage);
                updateMovie(mf);
            }
            return mf;
        }
        
        public static MovieFrame updateMovie(int[][] grayScaleImage) {
            MovieFrame mf = null;
            if (movieFrames.containsKey(grayScaleImage)) {
                mf = movieFrames.get(grayScaleImage);
                updateMovie(mf);
            }
            return mf;
        }
    }
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    
    package spinningSquare;
     
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.image.ImageObserver;
    import java.awt.image.PixelGrabber;
    import java.io.File;
    import java.io.FileNotFoundException;
     
    /**
     * This class provides two static methods for grabbing pixel-information of
     * images and
     * 
     * The first one grabGrayScale(String path) converts the image to gray-scale and
     * returns the gray-scale value of the pixel in a two-dimensional array. The
     * second one, grabRGB(String path) retrieves all three color-components of the
     * pixels of the image and returns this information in a three-dimensional
     * array.
     * 
     * Two static methods for color-space conversion between the RGB and the YCrCb
     * colorspaces are also provided.
     * 
     * @author Volker Christian (volker.christian@fh-hagenberg.at)
     * @version 1.0
     */
    public class ImageGrabber {
        static class ImageGrabberUtilities {
            private ImageByteArrayObserver imageObserver;
            private Image image;
            private int width;
            private int height;
     
            private class ImageByteArrayObserver implements ImageObserver {
                private boolean errored = false;
                private ImageGrabberUtilities igu = null;
     
                public ImageByteArrayObserver(ImageGrabberUtilities mioi) {
                    igu = mioi;
                }
     
                public boolean imageUpdate(Image img, int infoflags, int x, int y,
                        int width, int height) {
     
                    if ((infoflags & (ERROR)) != 0) {
                        errored = true;
                    }
                    if ((infoflags & (WIDTH | HEIGHT)) != 0) {
                        if (igu != null) {
                            igu.sizeKnown();
                        }
                    }
                    boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0);
     
                    return !done;
                }
     
                public boolean isErrored() {
                    return errored;
                }
            }
     
            
            private ImageGrabberUtilities() {
            }
     
            
            private static int[] convertToRGB(int pixel) {
                int[] RGB = new int[3];
                // Alpha = (pixel >> 24) & 0xff;
                RGB[0] = (int) ((pixel >> 16) & 0xff);
                RGB[1] = (int) ((pixel >> 8) & 0xff);
                RGB[2] = (int) ((pixel) & 0xff);
     
                return RGB;
            }
     
            private static double[] convertToYCbCr(int pixel) {
                int[] rgb = convertToRGB(pixel);
                return convertRGBToYCbCr(rgb[0], rgb[1], rgb[2]);
            }
     
            /**
             * Transforms the YCbCr color-space into the RGB color-space
             * 
             * @param Y
             *            the luminance value
             * @param Cb
             *            the first croma value
             * @param Cr
             *            the second croma value
             * @return a integer array containing the three RGB components. The
             *         components have the following indices: red = 0; green = 1;
             *         blue = 2
             */
            protected static int[] convertYCbCrToRGB(double Y, double Cb, double Cr) {
                double R = Y + 701.0 / 500.0 * (Cr - 1.0 / 2.0);
                double G = Y - 25251.0 / 73375.0 * (Cb - 1.0 / 2.0) - 209599.0
                        / 293500.0 * (Cr - 1.0 / 2.0);
                double B = Y + 443.0 / 250.0 * (Cb - 1.0 / 2.0);
     
                int[] RGB = new int[3];
     
                RGB[0] = (int) (R * 256);
                RGB[1] = (int) (G * 256);
                RGB[2] = (int) (B * 256);
     
                return RGB;
            }
     
            /**
             * Transforms the RGB color-space into the YCrCb color-space
             * 
             * @param r
             *            the red component
             * @param g
             *            the green component
             * @param b
             *            the blue component
             * @return a integer array containing the three YCrCb components. The
             *         components have the following indices: Y = 0; Cr = 1; Cb = 2
             */
            protected static double[] convertRGBToYCbCr(int r, int g, int b) {
                double R = ((double) r) / 256.0;
                double G = ((double) g) / 256.0;
                double B = ((double) b) / 256.0;
     
                double Y = 0.299 * R + 0.587 * G + 0.114 * B;
                double Cb = (B - Y) / 1.772 + 0.5;
                double Cr = (R - Y) / 1.402 + 0.5;
     
                double[] YCbCr = new double[3];
     
                YCbCr[0] = Y;
                YCbCr[1] = Cb;
                YCbCr[2] = Cr;
     
                return YCbCr;
            }
     
            private int[] grabPixels() throws InterruptedException {
                int[] pixels = null;
     
                width = image.getWidth(imageObserver);
                height = image.getHeight(imageObserver);
     
                if (width != -1 && height != -1) {
                    pixels = new int[width * height];
     
                    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height,
                            pixels, 0, width);
     
                    pg.grabPixels();
     
                    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
                        if (imageObserver.isErrored()) {
                            throw new InterruptedException(
                                    "Error in grabbing Pixels");
                        } else {
                            throw new InterruptedException(
                                    "Grabbing Pixels aborted");
                        }
                    }
                }
     
                return pixels;
            }
     
            private int getWidth() {
                return width;
            }
     
            private int getHeight() {
                return height;
            }
     
            synchronized private void sizeKnown() {
                notify();
            }
     
            synchronized private int[] loadImage(String path)
                    throws InterruptedException, FileNotFoundException {
                int[] pixels = null;
     
                File f = new File(path);
     
                if (f.exists()) {
                    image = Toolkit.getDefaultToolkit().createImage(path);
                    imageObserver = new ImageByteArrayObserver(this);
                    Toolkit.getDefaultToolkit().prepareImage(image, -1, -1,
                            imageObserver);
                    wait();
                    pixels = grabPixels();
                } else {
                    String wd = "";
                    if (path.charAt(0) != System.getProperty("file.separator")
                            .charAt(0)) {
                        wd = System.getProperty("user.dir")
                                + System.getProperty("file.separator").charAt(0);
                    }
                    throw new FileNotFoundException("File " + wd + path
                            + " not found");
                }
     
                return pixels;
            }
        }
     
        /**
         * This method reads a image and provides its RGB color-components in an
         * three-dimensional array to the caller.
         * 
         * @param fileName
         *            the path to the image-source used for grabbing
         * @return a three-dimensional integer array of the RGB values of the
         *         pixels. The first index is the y-coordinate, the second index is
         *         the x-coordinate into the image, and the third index is the
         *         color-component. red = 0; green = 1; blue = 2.
         * @throws FileNotFoundException
         *             if the image specified in the path-argument does not exist.
         * @throws InterruptedException
         *             if the grabb-process is interrupted unexpectedly.
         */
        public static int[][][] grabRGB(String fileName) throws FileNotFoundException,
                InterruptedException {
            ImageGrabberUtilities igu = new ImageGrabberUtilities();
     
            int[] pixels = igu.loadImage(fileName);
     
            int w = igu.getWidth();
            int h = igu.getHeight();
     
            int[][][] rgb = new int[h][w][];
     
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    rgb[y][x] = ImageGrabberUtilities
                            .convertToRGB(pixels[y * w + x]);
                }
            }
     
            return rgb;
        }
     
        /**
         * This method reads a image, converts it into gray-scale and provides this
         * information in a two-dimensional array to the caller.
         * 
         * @param fileName
         *            the path to the image-source used for grabbing
         * @return a two-dimensional integer array of the gray-scale values of the
         *         pixels. The first index is the y-coordinate and the second index
         *         is the x-coordinate into the image
         * @throws FileNotFoundException
         *             if the image specified in the path-argument does not exist.
         * @throws InterruptedException
         *             if the grabb-process is interrupted unexpectedly.
         */
        public static int[][] grabGrayScale(String fileName)
                throws FileNotFoundException, InterruptedException {
            ImageGrabberUtilities igu = new ImageGrabberUtilities();
     
            int[] pixels = igu.loadImage(fileName);
     
            int w = igu.getWidth();
            int h = igu.getHeight();
     
            int[][] grayScale = new int[h][w];
     
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    grayScale[y][x] = (int) (256 * ImageGrabberUtilities
                            .convertToYCbCr(pixels[y * w + x])[0]);
                }
            }
     
            return grayScale;
        }
    }
    Miniaturansicht angehängter Grafiken Miniaturansicht angehängter Grafiken Pinwheel Tiling-bildschirmfoto-2012-01-22-um-21.37.13.png  
     

Ähnliche Themen

  1. Problem bei Tiling [C++/SDL]
    Von SFandler im Forum C/C++
    Antworten: 0
    Letzter Beitrag: 16.02.11, 15:24
  2. tiling effekt verringern?
    Von Duddle im Forum Photoshop
    Antworten: 1
    Letzter Beitrag: 13.08.01, 15:05