Parameterübergabe an MyIcon()

Status
Nicht offen für weitere Antworten.

r74

Aktives Mitglied
Hallo Forum

Ich bin an meinem ersten Java-Projekt. Das Problem ist folgendes:


Code:
import javax.swing.*;
import java.awt.*;

public class Main {
	static void addInternalToDesktop( JDesktopPane desktop ) {
		JInternalFrame iframe = new JInternalFrame( "OUTPUT1", true, true, true, true );
		iframe.setLocation(100,100);
		iframe.setSize(250,250);
		iframe.add(new JScrollPane(new JLabel( new MyIcon() ))); //***************
		iframe.setVisible( true );
		desktop.add( iframe );
	}
	
	public static void main( String[] args ) {
		JFrame f = new JFrame();
		f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		JDesktopPane desktop = new JDesktopPane();
		f.add( new JScrollPane( desktop ) );
		f.setSize( 1000, 1000 );
		addInternalToDesktop( desktop );
		f.setVisible( true );
	}
}


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

public class MyIcon implements Icon {
	public void paintIcon( Component c, Graphics g, int x, int y) { //**************
		g.setColor( Color.red );
		g.fillOval( x, y, getIconWidth(), getIconHeight() );
	}

   public int getIconWidth() {
		return 200;
	}

	public int getIconHeight() {
		return 200;
	}
}

Die MyIcon-Klasse sollte nicht bloss ein bestimmtes Oval zeichnen. Wie kann ich
beispielsweise die Farbe als Parameter an MyIcon übergeben? (Betreffende Zeilen
sind mit * markiert).

Vielen Dank für eure Hilfe.
 

r74

Aktives Mitglied
@Wildcard: Ich hab so was schon versucht, aber leider erfolglos. Wie genau meinst du das?
 
S

SlaterB

Gast
public class MyIcon implements Icon {
private Color color;

public MyIcon(Color c) {
this.color = c;
}

public void paintIcon( Component c, Graphics g, int x, int y) {
g.setColor( this.color);
}
}


new MyIcon(Color.YELLOW)
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben