ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Java質問&情報提供サイトコミュのフレームの背景をグラデーションに

  • mixiチェック
  • このエントリーをはてなブックマークに追加
はじめまして。
現在javaの基礎を学校で勉強中で、フレームを使った課題をやっています。

JFrameでフレームを作り、

class MyPanel extends JPanel implements Runnable

というクラスでパネルを作ってその中の
MyPanel()メソッドで背景を設定しているのですが…
この背景をグラデーションさせるためにはどうすればいいでしょうか?

平行して画面を書き換えるRun()メソッドを使っていて、
Ballクラスではボールをフレーム上で動かすように作っています。
なのでそれと同じようにカラー用にMyColorクラスを作って、

●MyPanelメソッドの中で
mycolor = new MyColor(10, 1, 5, 200);
mycolor.change();
setBackground(new Color(mycolor.getX(), mycolor.getX(), mycolor.getX()));

●MyColorクラス
class MyColor {
private int x;
private int vx;
private int start;
private int end;

public MyColor(int x, int vx, int s, int e) {
this.x = x;
this.vx = vx;
start = s;
end = e;
}

public void change() {
x = x + vx;

if (x < start || x > end) {
vx = -vx;
}

}

public int getX() {
return x;
}

}
上記のようにしてみたのですがコンパイルできるのに
実行しても何もおきません^^;
どのようにすればいいか、教えてください。

コメント(6)

すみません、シングルスレッド等まだ習っていないので
読んでみたのですが何のことかわかりません…ι

これに上記のカラーのグラデーションを加えるには、
MyPanel内にメソッドを足せばいいのでしょうか??


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

class LineArt {
public static void main(String[] args) {
JFrame frame = new JFrame();

frame.setTitle("Line Art");
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyPanel panel = new MyPanel();
frame.getContentPane().add(panel);

frame.setVisible(true);
}
}

class MyPanel extends JPanel implements Runnable {

private Ball ball1;
private Ball ball2;

public MyPanel() {

setBackground(Color.black);

ball1 = new Ball(80,100,7,8,0,0,630,450);
ball2 = new Ball(160,160,8,7,0,0,630,450);

Thread refresh = new Thread(this);
refresh.start();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

ball1.forward();
ball2.forward();
g.setColor(Color.blue);
g.drawLine(ball1.getX(), ball1.getY(), ball2.getX(), ball2.getY());

}

public void run() {
while(true) {
repaint();
try {
Thread.sleep(20);
}
catch(Exception e) {
}
}
}
}

class Ball {
private int x;
private int y;
private int vx;
private int vy;
private int left;
private int right;
private int top;
private int bottom;

public Ball(int x, int y, int vx, int vy, int l, int t, int r, int b) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
right = r;
left = l;
top = t;
bottom = b;
}

public void forward() {
x = x + vx;
y = y + vy;

if (x < left || x > right) {
vx = -vx;
}
if (y < top || y > bottom) {
vy = -vy;
}
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}
}
はい、そうです〜。
色々ありがとうございますm(_ _"m)
それをどのメソッドとかクラスにいれたらいいのかが
わかりません…ι
できました・・・
本当にありがとうございました!
この問題は自分にはレベルが高すぎたみたいです。
もっと基礎からしっかり勉強したいと思います。
ありがとうございましたm(_ _"m)

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

Java質問&情報提供サイト 更新情報

Java質問&情報提供サイトのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。