ASP.NETで棒グラフの色を配列で指定したい

このQ&Aのポイント
  • ASP.NETの棒グラフで複数の色を配列で指定したい場合、リストや配列を使って色の情報を管理することができます。
  • まず、色の情報を管理するためのリストや配列を作成します。
  • その後、ループを使用して指定した位置に配列から色を取り出し、棒グラフを描画する処理を行います。エラーが発生する場合は、キャストに関する問題が考えられますので、正しいキャストを行ってください。
回答を見る
  • ベストアンサー

色の指定で配列を使いたい

ASP.NETで棒グラフを描いています。 3本の棒があったとして、今までは、 ------------------------------------------------ g.FillRectangle(New SolidBrush(Color.Blue), x, y, width, height) g.FillRectangle(New SolidBrush(Color.Green), x, y, width, height) g.FillRectangle(New SolidBrush(Color.Red), x, y, width, height) ------------------------------------------------ このように書いていたのですが、 色の部分を配列にしたいと考えています。 ------------------------------------------------ Dim BrCol As New ArrayList BrCol.Add("Blue") BrCol.Add("Green") BrCol.Add("Red") For i = 0 to 2 g.FillRectangle(New SolidBrush(BrCol(0)), x, y, width, height) Next ------------------------------------------------ これではエラーになります("指定されたキャストは有効ではありません。") どのように書けばよいでしょうか? よろしくお願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • todo36
  • ベストアンサー率58% (728/1234)
回答No.1

Dim BrCol() As Color = {Color.Blue, Color.Green, Color.Red}

mirurin
質問者

お礼

できました!ありがとうございます。 もっと勉強します。

関連するQ&A

  • ペイントソフトでキャンバス内にだけにかけるようにしたいです。

    どうしたらいいのかわからいので、何かヒントでiいので教えてください。 import java.applet.*; import java.awt.*; import java.awt.event.*; /* <APPLET CODE ="Mous" WIDTH = 500 HEIGHT = 300> </APPLET> */ public class Mous extends Applet { Graphics g; int point_x, point_y; Button color_black,color_red,color_blue,clear; public void init() { g = getGraphics(); setLayout(null); addMouseListener(new MouseAdapter () { public void mousePressed(MouseEvent e){ point_x = e.getX(); point_y = e.getY(); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { g.drawLine(point_x,point_y,e.getX(),e.getY()); point_x = e.getX(); point_y = e.getY(); } }); color_black = new Button("黒"); color_black.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { g.setColor(Color.black); } }); color_red = new Button("赤"); color_red.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { g.setColor(Color.red); } }); color_blue = new Button("青"); color_blue.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { g.setColor(Color.blue); } }); clear = new Button("クリアー"); clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { g.clearRect(0,0,500,300); } }); add(color_black); add(color_red); add(color_blue); add(clear); } }

  • 初心者です。 コンパイルのエラー

    import java.awt.*; import javax.swing.*; public class R11Sample1 extends JFrame { Rect r1 = new Rect(Color.red, 100, 100, 80, 60); Rect r2 = new Rect(new Color (0.5f, 1f, 0f, 0.7f), 150, 120, 60, 90); Oval = new Oval(Color.blue, 60, 50, 10, 10); JPanel panel = new JPanel() { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; r1.draw(g2); r2.draw(g2); } }; public R11Sample1() { setSize(400, 350); setDefaultCloseOperation(EXIT_ON_CLOSE); getContentPane().add(panel); } public static void main(String[] args) { new R11Sample1().setVisible (true); } } class Rect { Paint pat; int xpos, ypos, width, height; public Rect(Paint p, int x, int y, int w, int h) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillRect(xpos-width/2, ypos-height/2, width, height); } } class Oval { Paint pat; int xpos, ypos, radius; public Oval(Paint p, int x, int y, int width, int height) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillOval(xpos-width/2, ypos-height/2, width, height); } } これでコンパイルすると、 Identifierがありません といわれました。 どこを直せばいいのでしょうか。 また、全体的に間違ったところがあったら教えてください。

    • ベストアンサー
    • Java
  • カラーダイアログの指定色で塗りつぶしの四角形を描きたい

    VB.NET2003環境なのですが、 Dim g1 As Graphics = PictureBox1.CreateGraphics() g1.FillRectangle(Brushes.Red, 0, 0, 100, 100) g1.Dispose() の方法で四角形を描いているのですが、 この状態だとBrushes.Redの赤で描きますが、 これをColorDialogで指定した色を指定したいのですが、 どうしたら良いですか? g1.FillRectangle(ColorDialog1.Color, 0, 0, 100, 100) このようなことなのですが、これだとエラーになってしまいます。 ご教示頂けたら幸いです。

  • Swing実装での図形の追加と色の指定追加

    以下の作成した図形描写javaプログラムに図形の変更ボタン(円や直線)、色選択(例:赤、青、緑の三種)をおこなうことの出きるようにすればどうすれば良いでしょうか? import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; abstract class Figure { protected int x,y,width,height; protected Color color; public Figure(int x,int y,int w,int h,Color c) { this.x = x; this.y = y; width = w; height = h; color = c; } public void setSize(int w,int h) { width = w; height = h; } public void setLocation(int x,int y) { this.x = x; this.y = y; } abstract public void reshape(int x1,int y1,int x2,int y2); abstract public void paint(Graphics g); } class RectangleFigure extends Figure { public RectangleFigure(int x,int y,int w,int h,Color c) { super(x,y,w,h,c); } public void reshape(int x1,int y1,int x2,int y2) { int newx = Math.min(x1,x2); int newy = Math.min(y1,y2); int neww = Math.abs(x1 - x2); int newh = Math.abs(y1 - y2); setLocation(newx,newy); setSize(neww,newh); } public void paint(Graphics g) { g.setColor(color); g.drawRect(x,y,width,height); } } class DrawApplication { protected Vector<Figure> figures; /* Generics */ protected Figure drawingFigure; protected Color currentColor; protected DrawPanel drawPanel; public DrawApplication() { figures = new Vector<Figure>(); /* Generics */ drawingFigure = null; currentColor = Color.red; } public void setDrawPanel(DrawPanel c) { System.out.print("セットされました"); drawPanel = c; } public int getNumberOfFigures() { return figures.size(); } public Figure getFigure(int index) { return (Figure)figures.elementAt(index); } public void createFigure(int x,int y) { Figure f = new RectangleFigure(x,y,0,0,currentColor); figures.addElement(f); drawingFigure = f; drawPanel.repaint(); } public void reshapeFigure(int x1,int y1,int x2,int y2) { if (drawingFigure != null) { drawingFigure.reshape(x1,y1,x2,y2); drawPanel.repaint(); } } } class DrawPanel extends JPanel { protected DrawApplication drawApplication; public DrawPanel(DrawApplication app) { setBackground(Color.white); drawApplication = app; app.setDrawPanel(this); } public void paintComponent(Graphics g) { super.paintComponent(g); Figure f = new RectangleFigure(0,0,0,0,drawApplication.currentColor); for(int i=0;i<drawApplication.getNumberOfFigures();i++){ f = drawApplication.getFigure(i); f.paint(g); } } } class DrawMouseListener implements MouseListener,MouseMotionListener { protected DrawApplication drawApplication; protected int dragStartX,dragStartY; public DrawMouseListener(DrawApplication a) { drawApplication = a; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { dragStartX = e.getX(); dragStartY = e.getY(); drawApplication.createFigure(dragStartX,dragStartY); } public void mouseReleased(MouseEvent e) { drawApplication.reshapeFigure(dragStartX,dragStartY,e.getX(),e.getY()); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { drawApplication.reshapeFigure(dragStartX,dragStartY,e.getX(),e.getY()); } public void mouseMoved(MouseEvent e) { } } class DrawMain { public static void main(String argv[]) { JFrame f = new JFrame("Draw"); DrawApplication app = new DrawApplication(); JPanel c =new DrawPanel(app); c.addMouseListener(new DrawMouseListener(app)); c.addMouseMotionListener(new DrawMouseListener(app)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(c,BorderLayout.CENTER); f.setSize(400,300); f.setVisible(true); } }

  • 連想配列(ハッシュ)のファイルへの書き込み方

    連想配列(ハッシュ)のファイルへの書き込み方と、 そのファイルからの読み込み方で困っています。 たとえば以下のような連想配列(ハッシュ)があって %colorFile = ( 'red'=>'5', 'green'=>'10', 'blue'=>'3' ); この情報をサーバーの color.dat ファイルに書き込んで保存し、 変更があったときにファイルからデータを読み込むには どのようにしたらいいのでしょうか? 連想配列(ハッシュ)のまま保存すると color.datの中身が green10blue3red5 のようになってしまい、 読み込んだときに連想配列(ハッシュ)に復元できません…。 よろしくお願いします。

    • ベストアンサー
    • Perl
  • マインスイーパーもどきをつくりたいのですが・・・

    周辺の地雷の数を表示させたいのですがなかなかうまくいきません・・・ どうすればいいか教えてください。 import java.applet.Applet; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class jirai extends JApplet implements ActionListener{ int Width,Masu; //格子の幅(ピクセル)、格子の数 int x,y,ix,iy; int i,j; int n,m; int l; int count = 0; int JIRAI[][] = new int[10][10]; JPanel pan1, pan2; JButton s_btn = new JButton("start"); JTextField txt = new JTextField(23); public void init(){ setSize(360,400); Container con=getContentPane(); pan1=new JPanel(); pan2=new JPanel(); con.add(pan1); con.add(pan2=new JPanel(),"South"); pan1.addMouseListener(new MyMouseListener()); pan1.add(txt); pan1.setBackground(Color.yellow); pan1.setForeground(Color.red); pan1.setSize(300,400); pan1.setBorder(BorderFactory.createLineBorder(Color.red)); pan2.add(s_btn); s_btn.setBackground(Color.pink); s_btn.addActionListener(this); pan2.setBorder(BorderFactory.createLineBorder(Color.blue)); pan2.setBackground(Color.red); } public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("start")){ Graphics g=pan1.getGraphics(); Masu=8; Width=30; g.setColor(Color.black); for(x=Width*2;x<=Width*10;x+=Width) g.drawLine(x,Width*2,x,Width*10); //30ピクセル間隔でY方向の線を描く for(y=Width*2;y<=Width*10;y+=Width) g.drawLine(Width*2,y,Width*10,y); //30ピクセル間隔でX方向の線を描く for(i=1;i<8;i++){ for(j=1;j<8;j++){ JIRAI[i][j] = 0; } } for(n=0;n<15;n++){ do{ for(i=1;i<8;i++){ for(j=1;j<8;j++){ m=(int)(Math.random()*8); l=(int)(Math.random()*8); } } }while(JIRAI[m][l]!=0); JIRAI[m][l] = 1; } for (int dx=(ix-1)-1;dx<(ix-1)+1;dx++){ for(int dy=(iy-1)-1;dy<(iy-1)+1;dy++){ } } g.dispose(); txt.setBackground(Color.pink); txt.setForeground(Color.black); txt.setFont( new Font("Serif",Font.ITALIC,18)); txt.setText("地雷の数は15個です!") ; showStatus(" 地雷の数は15個です!"); //アプレット下の表示 } } class MyMouseListener extends MouseAdapter{ public void mouseClicked(MouseEvent me){ //マウスがクリックされた x=me.getX(); //マウスのクリック位置:x座標 y=me.getY(); //マウスのクリック位置:y座標 ix=(int)(x/Width); //0~11? アプレットの大きさで変わる iy=(int)(y/Width); //0~11? アプレットの大きさで変わる if(2<=iy && iy<=Masu+1 && 2<=ix && ix<=Masu+1){ //(2,2)~(9,9) ix=ix-1; iy=iy-1; Graphics g=pan1.getGraphics(); //クリックした升目の地雷の有無を判定して処理 if(JIRAI[ix-1][iy-1] == 1){ g.setColor(Color.red); g.fillOval(30*(ix-1)+60,30*(iy-1)+60,30,30); JOptionPane.showMessageDialog(null,"ゲーム-オーバー!",null,JOptionPane.ERROR_MESSAGE); } if(JIRAI[ix-1][iy-1] == 0){ g.setColor(Color.blue); g.fillOval(30*(ix-1)+60,30*(iy-1)+60,30,30); g.setColor(Color.black); g.drawString(""+check,30*(ix-1)+70,30*(iy-1)+80); } g.setColor(Color.yellow); g.fillRect(60,305,100,50); g.setColor(Color.black); g.setFont( new Font("Serif",Font.ITALIC,18)); g.drawString("("+ix+","+iy+")",60,320); } } } }

  • アプレットを再描画させたい

    アプレットを作りましたが、ウィンドウの大きさを変えた時にも描いたオブジェクトをrepaintを使って再描画させるのは どのようにすればよいのでしょうか。 _______________________________ public class QQ_Oekaki extends Applet implements ActionListener { Graphics g; Color col; int w; int h; Button btnBlue; Button btnYellow; public void init() { g = getGraphics(); btnBlue = new Button("Blue"); this.add(btnBlue); btnBlue.addActionListener(this); btnBlue.setFont(new Font("SansSerif", Font.ITALIC, 10)); btnBlue.setForeground(Color.blue); btnYellow = new Button("Yellow"); this.add(btnYellow); btnYellow.addActionListener(this); btnYellow.setFont(new Font("SansSerif", Font.ITALIC, 10)); btnYellow.setForeground(Color.yellow); this.addMouseMotionListener ( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { int x = e.getX(); int y = e.getY(); g.setColor(Color.green); g.setColor(col); g.fillRect(x,y,5,5); } } ); } public void actionPerformed(ActionEvent ae) { if (ae.getActionCommand()=="Yellow") { col = Color.yellow; } if (ae.getActionCommand()=="Blue") { col = Color.BLUE; } } }

  • 配列

    ・画像のヒストグラムを作成するとき、 for(y=0; y<height; y++){ for(x=0; x<width; x++){ hist[ InputData[y][x] ]++ } } と書かれているのですが、hist[ InputData[y][x] ]++ の部分はどうゆう意味なのですか? よろしくお願いします.

  • Graphicsクラスを使って描画履歴を残して、しかも色も選択しながら描画を行いたい

    以下のようなドロープログラムを作ってみたのですが 描画色を変更して再描画すると前の描画履歴が消えてしまいます 描画色を変更しても以前の描画履歴の消えないようにするには どのようにすればいいのでしょうか。 文字数制限によりプログラムをアップできませんでしたので一部を記述させていただきます。 グローバルなカラーオブジェクトを用意し class Globe{ static mycol=Color.white; } 二つのラベルを用意して Label red=new Label("■"); Label green=new Label("■"); ラベルそれぞれにマウスリスナーを登録して red.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ GLobe.mycol=Color.red; } }); green.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ GLobe.mycol=Color.green; } }); マウスドラッグによる座標取得を行い public void mouseDragged(MouseEvent e){ x=e.getX(); y=e.getY(); repaint(); } 二つの色を使い分けて描画できるようにしました。 public void update(Graphics g){ setForeground(Globe.mycol); g.fillOval(x-3,y-3,6,6); }

    • ベストアンサー
    • Java
  • 文字化け

    phpソースだけなら正しく表示できるのですが、 htmlソースを加えるとphpソースの部分が文字化けし、画像が表示できません。どうしてなのでしょうか? 画像を扱わないプログラムは、文字化けしないのですが、GDで画像を扱うと文字化けしてしまいます。 文字化けする↓ <html><head><title>sample</title></head> <?php $width=$_GET{'wid'}; $height=$_GET{'hei'}; $red=$_GET{'red'};$green=$_GET{'gre'};$blue=$_GET{'blu'}; $text=$_GET{'tex'};$size=$_GET{'siz'}; $red1=$_GET{'red1'};$green1=$_GET{'gre1'};$blue1=$_GET{'blu1'}; $angle=$_GET{'ang'};$x1=$_GET{'x1'};$y1=$_GET{'y1'}; header("Content-type: image/jpeg"); $im = imagecreate($width,$height); $bground = imagecolorallocate($im, $red,$green,$blue); $text_c = imagecolorallocate($im,$red1,$green1,$blue1); imagettftext($im, $size, -$angle,$x1,$y1, $text_c,"Century",$text); imagepng($im); ?> </form></body></html>

    • ベストアンサー
    • PHP

専門家に質問してみよう