Autor |
Beitrag |
kaskus
Hält's aus hier
Beiträge: 13
WIN 2000; WIN XP
Delphi 7
|
Verfasst: Di 03.11.09 19:00
Guten Abend allerseits.
Ich würde von mir behaupten, dass ich grundlegende Java Kenntnisse behersche und auch Programmauschnitte, sei es C# oder Java mir herleiten kann, wenn ich den Quellcode sehe.
Haben jetzt eine Aufgabe bekommen, folgenden Code von Java in C# zu konvertieren.
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:
| /* * @(#)fractal.java - Fractal Program Mandelbrot Set * www.eckhard-roessel.de * Copyright (c) Eckhard Roessel. All Rights Reserved. * 06/30/2000 - 03/29/2000 */ import java.awt.*; import java.applet.*; import java.awt.event.*; /** * @version 1.1 * @author Eckhard Roessel * @modified 03/29/2001 * @modified 08/16/2006 tweaked by Duncan Mullier 8/2006 */ public class Fractal extends Applet implements MouseListener, MouseMotionListener { class HSB {//djm added, it makes it simpler to have this code in here than in the C# public float rChan,gChan,bChan; public HSB() { rChan=gChan=bChan=0; } public void fromHSB(float h,float s,float b) { float red = b; float green = b; float blue = b; if (s != 0) { float max = b; float dif = b * s / 255f; float min = b - dif;
float h2 = h * 360f / 255f;
if (h2 < 60f) { red = max; green = h2 * dif / 60f + min; blue = min; } else if (h2 < 120f) { red = -(h2 - 120f) * dif / 60f + min; green = max; blue = min; } else if (h2 < 180f) { red = min; green = max; blue = (h2 - 120f) * dif / 60f + min; } else if (h2 < 240f) { red = min; green = -(h2 - 240f) * dif / 60f + min; blue = max; } else if (h2 < 300f) { red = (h2 - 240f) * dif / 60f + min; green = min; blue = max; } else if (h2 <= 360f) { red = max; green = min; blue = -(h2 - 360f) * dif / 60 + min; } else { red = 0; green = 0; blue = 0; } }
rChan = Math.round(Math.min(Math.max(red, 0f), 255)); gChan = Math.round(Math.min(Math.max(green, 0), 255)); bChan = Math.round(Math.min(Math.max(blue, 0), 255)); } } private final int MAX = 256; // max iterations private final double SX = -2.025; // start value real private final double SY = -1.125; // start value imaginary private final double EX = 0.6; // end value real private final double EY = 1.125; // end value imaginary private static int x1, y1, xs, ys, xe, ye; private static double xstart, ystart, xende, yende, xzoom, yzoom; private static boolean action, rectangle, finished; private static float xy; private Image picture; private Graphics g1; private Cursor c1, c2; private HSB HSBcol=new HSB(); public void init() // all instances will be prepared { //HSBcol = new HSB(); setSize(640,480); finished = false; addMouseListener(this); addMouseMotionListener(this); c1 = new Cursor(Cursor.WAIT_CURSOR); c2 = new Cursor(Cursor.CROSSHAIR_CURSOR); x1 = getSize().width; y1 = getSize().height; xy = (float)x1 / (float)y1; picture = createImage(x1, y1); g1 = picture.getGraphics(); finished = true; }
public void destroy() // delete all instances { if (finished) { removeMouseListener(this); removeMouseMotionListener(this); picture = null; g1 = null; c1 = null; c2 = null; System.gc(); // garbage collection } }
public void start() { action = false; rectangle = false; initvalues(); xzoom = (xende - xstart) / (double)x1; yzoom = (yende - ystart) / (double)y1; mandelbrot(); }
public void stop() { } public void paint(Graphics g) { update(g); } public void update(Graphics g) { g.drawImage(picture, 0, 0, this); if (rectangle) { g.setColor(Color.white); if (xs < xe) { if (ys < ye) g.drawRect(xs, ys, (xe - xs), (ye - ys)); else g.drawRect(xs, ye, (xe - xs), (ys - ye)); } else { if (ys < ye) g.drawRect(xe, ys, (xs - xe), (ye - ys)); else g.drawRect(xe, ye, (xs - xe), (ys - ye)); } } } private void mandelbrot() // calculate all points { int x, y; float h, b, alt = 0.0f; action = false; setCursor(c1); showStatus("Mandelbrot-Set will be produced - please wait..."); for (x = 0; x < x1; x+=2) for (y = 0; y < y1; y++) { h = pointcolour(xstart + xzoom * (double)x, ystart + yzoom * (double)y); // color value if (h != alt) { b = 1.0f - h * h; // brightnes ///djm added ///HSBcol.fromHSB(h,0.8f,b); //convert hsb to rgb then make a Java Color ///Color col = new Color(0,HSBcol.rChan,HSBcol.gChan,HSBcol.bChan); ///g1.setColor(col); //djm end //djm added to convert to RGB from HSB g1.setColor(Color.getHSBColor(h, 0.8f, b)); //djm test Color col = Color.getHSBColor(h,0.8f,b); int red = col.getRed(); int green = col.getGreen(); int blue = col.getBlue(); //djm alt = h; } g1.drawLine(x, y, x + 1, y); } showStatus("Mandelbrot-Set ready - please select zoom area with pressed mouse."); setCursor(c2); action = true; } private float pointcolour(double xwert, double ywert) // color value from 0.0 to 1.0 by iterations { double r = 0.0, i = 0.0, m = 0.0; int j = 0; while ((j < MAX) && (m < 4.0)) { j++; m = r * r - i * i; i = 2.0 * r * i + ywert; r = m + xwert; } return (float)j / (float)MAX; } private void initvalues() // reset start values { xstart = SX; ystart = SY; xende = EX; yende = EY; if ((float)((xende - xstart) / (yende - ystart)) != xy ) xstart = xende - (yende - ystart) * (double)xy; }
public void mousePressed(MouseEvent e) { e.consume(); if (action) { xs = e.getX(); ys = e.getY(); } } public void mouseReleased(MouseEvent e) { int z, w; e.consume(); if (action) { xe = e.getX(); ye = e.getY(); if (xs > xe) { z = xs; xs = xe; xe = z; } if (ys > ye) { z = ys; ys = ye; ye = z; } w = (xe - xs); z = (ye - ys); if ((w < 2) && (z < 2)) initvalues(); else { if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy); else xe = (int)((float)xs + (float)z * xy); xende = xstart + xzoom * (double)xe; yende = ystart + yzoom * (double)ye; xstart += xzoom * (double)xs; ystart += yzoom * (double)ys; } xzoom = (xende - xstart) / (double)x1; yzoom = (yende - ystart) / (double)y1; mandelbrot(); rectangle = false; repaint(); } }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { } public void mouseDragged(MouseEvent e) { e.consume(); if (action) { xe = e.getX(); ye = e.getY(); rectangle = true; repaint(); } } public void mouseMoved(MouseEvent e) { }
public String getAppletInfo() { return "fractal.class - Mandelbrot Set a Java Applet by Eckhard Roessel 2000-2001"; } } |
Ich weis, dass ich vieles einfach so übernehmen kann. Jedoch hänge ich zur Zeit an den fett makierten Zeilen.
Bin noch nicht weiter gekommen und sehe auch keinen Sinn dahinter, weiter im Programm vorzugehen, wenn ich die einen Fehler noch nicht behoben habe....
Hierzu meine CS datei...
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:
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace FRACTAL { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
class HSB {
public float rChan, gChan, bChan; public HSB() { rChan = gChan = bChan = 0; } public void fromHSB(float h, float s, float b) { float red = b; float green = b; float blue = b; if (s != 0) { float max = b; float dif = b * s / 255f; float min = b - dif;
float h2 = h * 360f / 255f;
if (h2 < 60f) { red = max; green = h2 * dif / 60f + min; blue = min; } else if (h2 < 120f) { red = -(h2 - 120f) * dif / 60f + min; green = max; blue = min; } else if (h2 < 180f) { red = min; green = max; blue = (h2 - 120f) * dif / 60f + min; } else if (h2 < 240f) { red = min; green = -(h2 - 240f) * dif / 60f + min; blue = max; } else if (h2 < 300f) { red = (h2 - 240f) * dif / 60f + min; green = min; blue = max; } else if (h2 <= 360f) { red = max; green = min; blue = -(h2 - 360f) * dif / 60 + min; } else { red = 0; green = 0; blue = 0; } }
rChan = (float)Math.Round(Math.Min(Math.Max(red, 0f), 255)); gChan = (float)Math.Round(Math.Min(Math.Max(green, 0), 255)); bChan = (float)Math.Round(Math.Min(Math.Max(blue, 0), 255));
} }
private const int MAX = 256; private const double SX = -2.025; private const double SY = -1.125; private const double EX = 0.6; private const double EY = 1.125; private static int x1, y1, xs, ys, xe, ye; private static double xstart, ystart, xende, yende, xzoom, yzoom; private static boolean action, rectangle, finished; private static float xy; private Bitmap picture; private Graphics g1; private Cursor c1, c2; private HSB HSBcol=new HSB();
public void init() { HSBcol = new HSB(); finished = false; c1 = Cursors.WaitCursor; c2 = Cursors.Default; x1 = 300; y1 = 300; xy = (float)x1 / (float)y1; picture = B g1 = picture.getGraphics(); finished = true; }
public void destroy() { if (finished) { picture = null; g1 = null; c1 = null; c2 = null; } }
public void start() { action = false; rectangle = false; initvalues(); xzoom = (xende - xstart) / (double)x1; yzoom = (yende - ystart) / (double)y1; mandelbrot(); }
public void stop() { } public void paint(Graphics g) { update(g); }
public void update(Graphics g) { g.drawImage(picture, 0, 0, this); if (rectangle) { g.Color(Color.White); if (xs < xe) { if (ys < ye) g.DrawRectangle(xs, ys, (xe - xs), (ye - ys)); else g.DrawRectangle(xs, ye, (xe - xs), (ys - ye)); } else { if (ys < ye) g.DrawRectangle(xe, ys, (xs - xe), (ye - ys)); else g.DrawRectangle(xe, ye, (xs - xe), (ys - ye)); } } }
private void mandelbrot() { int x, y; float h, b, alt = 0.0f;
action = false; setCursor(c1); showStatus("Mandelbrot-Set will be produced - please wait..."); for (x = 0; x < x1; x += 2) for (y = 0; y < y1; y++) { h = pointcolour(xstart + xzoom * (double)x, ystart + yzoom * (double)y); if (h != alt) { b = 1.0f - h * h; g1.setColor(Color.getHSBColor(h, 0.8f, b)); Color col = Color.getHSBColor(h, 0.8f, b); int red = col.getRed(); int green = col.getGreen(); int blue = col.getBlue(); alt = h; } g1.drawLine(x, y, x + 1, y); } setCursor(c2); action = true; }
private float pointcolour(double xwert, double ywert) { double r = 0.0, i = 0.0, m = 0.0; int j = 0;
while ((j < MAX) && (m < 4.0)) { j++; m = r * r - i * i; i = 2.0 * r * i + ywert; r = m + xwert; } return (float)j / (float)MAX; }
private void initvalues() { xstart = SX; ystart = SY; xende = EX; yende = EY; if ((float)((xende - xstart) / (yende - ystart)) != xy) xstart = xende - (yende - ystart) * (double)xy; }
public void mousePressed(MouseEvent e) { e.consume(); if (action) { xs = e.X(); ys = e.Y(); } }
public void mouseReleased(MouseEvent e) { int z, w; e.consume(); if (action) { xe = e.getX(); ye = e.getY(); if (xs > xe) { z = xs; xs = xe; xe = z; } if (ys > ye) { z = ys; ys = ye; ye = z; } w = (xe - xs); z = (ye - ys); if ((w < 2) && (z < 2)) initvalues(); else { if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy); else xe = (int)((float)xs + (float)z * xy); xende = xstart + xzoom * (double)xe; yende = ystart + yzoom * (double)ye; xstart += xzoom * (double)xs; ystart += yzoom * (double)ys; } xzoom = (xende - xstart) / (double)x1; yzoom = (yende - ystart) / (double)y1; mandelbrot(); rectangle = false; repaint(); } }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { } public void mouseDragged(MouseEvent e) { e.consume(); if (action) { xe = e.getX(); ye = e.getY(); rectangle = true; Refresh(); } } public void mouseMoved(MouseEvent e) { }
} |
wäre jeder hilfe dankbar, wie ich hier am besten vorgehe bzw. irgendwelche hilfreiche sites etc.
Moderiert von Kha: B- durch Highlight-Tags ersetzt
|
|
Kha
Beiträge: 3803
Erhaltene Danke: 176
Arch Linux
Python, C, C++ (vim)
|
Verfasst: Di 03.11.09 19:17
Die Image-Variable kann vom Typ Image bleiben, zuweisen solltest du allerdings eine Bitmap-Instanz. Das Graphics-Objekt bekommst du dann über Graphics.FromImage.
Aber wie du die Größe des Formulars herausfindest, solltest du schon selbst lösen können .
_________________ >λ=
|
|
kaskus
Hält's aus hier
Beiträge: 13
WIN 2000; WIN XP
Delphi 7
|
Verfasst: Di 03.11.09 19:44
Danke der Antwort.
Bin mir nicht ganz sicher, aber ich glaube das Problem habe ich schon gelöst.
Wie ich die Breite und Höhe auslese, weis ich nicht... Denke eigentlich schon an sowas wei mit Hidth und Width.. Aber bekomm das dauernd als Fehler angezeigt.
Meine Lösung zu der Bitmap lautet:
C#-Quelltext 1: 2: 3:
| picture = new Bitmap(x1,y1); g1 = Graphics.FromImage(picture); finished = true; |
Hänge aber nun an folgendem Code Schnipsel:
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:
|
public void update(Graphics g) { Pen p1 = new Pen(Color.White); g.DrawRectangle(p1, 0, 0,0,0); if (rectangle) { g.Color(Color.White); if (xs < xe) { if (ys < ye) g.DrawRectangle(xs, ys, (xe - xs), (ye - ys)); else g.DrawRectangle(xs, ye, (xe - xs), (ys - ye)); } else { if (ys < ye) g.DrawRectangle(xe, ys, (xs - xe), (ye - ys)); else g.DrawRectangle(xe, ye, (xs - xe), (ys - ye)); } } } |
Hänge an diesem Programm schon mehrere Stunde über mehrere Tage verteilt, habe ein paar Schnipsel schon fertig und ein paar Fehlen mir halt noch! KOmm mir auch blöd vor, Code für Code hier nachzufragen, aber wer nicht fragt bleibt dumm sag ich mir...
g.Color(Color.White);
was macht genau diese Zeile?
Für mich in Java würde das auf Anhieb bedeuten, macht meine Grafik wies, aber lasst mich raten, ich liege falsch?
|
|
kaskus
Hält's aus hier
Beiträge: 13
WIN 2000; WIN XP
Delphi 7
|
Verfasst: Di 03.11.09 19:51
Auch das Problem mit Size und Wifhdht habe ich nun mit
C#-Quelltext 1: 2:
| x1 = this.Size.Height; y1 = this.Size.Width; |
gelöst !
Ist doch richtig, oder?!
|
|
|